+ 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
    '_- Hidden - Scroll to the right in the Code Window '_-
    MS-Off Ver
    Office 2007 2010 PC but Not mac. XP and Vista mostly
    Posts
    3,417

    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
    MS-Off Ver
    Microsoft Office 365
    Posts
    17,488

    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
    '_- Hidden - Scroll to the right in the Code Window '_-
    MS-Off Ver
    Office 2007 2010 PC but Not mac. XP and Vista mostly
    Posts
    3,417

    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)

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