+ Reply to Thread
Results 1 to 3 of 3

Can I declare a Variable as a Word document object? (Actually I did, but it didn't work!

  1. #1
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Can I declare a Variable as a Word document object? (Actually I did, but it didn't work!

    Hello
    It appears that there is a documents “collection” object of open documents. I appear to be able to refer to an open document via its item number ( Index ) or name, like
    Documents.Item("MyWordFile.docx")
    Or for short
    Documents("MyWordFile.docx")

    So I could, for example Activate that opened document with
    Documents("MyWordFile.docx").Activate

    I can do very similar to that in Excel VBA,
    or
    I can use a variable for my Workbook object like this:_..
    Dim Wb As Workbook
    _Set Wb = Workbooks("MyExcelFile.xlsx")
    _..then I can refer to the Workbook via that variable.
    So for example, having done that declaration I can do like:
    _Wb.Activate.


    I can’t seem to find any documentation on how to do something similar in Word VBA, pseudo like:
    Dim Doc As Document
    _Set Doc = Documents("MyWordFile.docx")

    Does anyone know if I can do that?

    Maybe this is all a bit of a different World to the VBA Excel one that I know about.
    or
    am I just a Dim Doc.?
    Thanks
    Alan

    P.s. What I actually want to do is determine the Active document at the start of a code in such a way that I can then safely refer to it later and not get mixed up if other documents start getting opened, etc.
    This would be one way, that seems to work: _..
    Dim DocNme As String
    _Let DocNme = ActiveDocument.Name
    _.. then later I can do like refer to that with
    _Documents.(DocNme)

    _.. I am puzzled that I cannot seem to refer to a Word document object as I can to an Excel Workbook object.
    Last edited by Doc.AElstein; 02-15-2017 at 03:26 PM. Reason: Reason for Editing: ??? - Well, I thought , "someone has to do it ! " :)
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Can I declare a Variable as a Word document object? (Actually I did, but it didn't w

    Why not use ActiveDocument?

    Also, all of this code works for me
    Please Login or Register  to view this content.
    PS Of course that code is being run in Word, if you are automating Word from elsewhere you'll probably need to add a reference to the instance of the Word application you are working with before Documents, eg wrdApp.Documents.
    If posting code please use code tags, see here.

  3. #3
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Oh dear, what a dim doc i was.....

    Ah,.... it does work.
    I must have done something stupid , and then because I found no documentation anywhere on it, I assumed I could not do it

    Solved: The answer was that I was a Dim Doc

    Thanks Norie for the quick reply.

    Do you know of any documentation on this ?
    I saw things like this:_..
    https://msdn.microsoft.com/en-us/lib.../ff822963.aspx
    _.. but I had found nothing showing like
    Dim Doc As Document
    _Set Doc = Documents("MyWordFile.docx")

    Thanks again, sorry for my “Dim”ness

    Alan


    Please Login or Register  to view this content.









    EDIT: Any one else seen any documantation on this. ???
    I guess I am not familiar with Word VBA, I cannot find much on this...
    Last edited by Doc.AElstein; 02-16-2017 at 01:13 PM. Reason: Still looking for doco on this and listening to .. http://listenonrepeat.com/watch/?v=Hce74cEAAaE#The_Troggs_-_Wild_Thing

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Paste Excel object into Word document in landscape?
    By zijin_cheng in forum Excel General
    Replies: 4
    Last Post: 12-10-2014, 07:30 PM
  2. How can I get individual cells to work like a word document?
    By Jimbo0416 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-12-2014, 07:54 AM
  3. [SOLVED] Set word variable to a document that’s already open
    By Trebor76 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-23-2013, 06:01 PM
  4. [SOLVED] how do I import variable data into a word document
    By GaryPick in forum Excel General
    Replies: 1
    Last Post: 03-30-2006, 10:55 AM
  5. Variable as name of object does not work - Why?
    By -JEFF- in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-26-2005, 10:05 AM
  6. Replies: 2
    Last Post: 07-11-2005, 12:05 PM
  7. [SOLVED] Assign a variable to a word document
    By Anson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-06-2005, 05:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1