3

I want to assign an existing document to a variable which I can open and activate later. There might be several documents open when running the code (including the document containing the VBA project) so I need make sure I'm activating the right one.

The below doesn't work because I need the filename and not the file-path in the final line, but I don't know how to extract this from the path in line 2...

Dim NewQuote1 as string
Set NewQuote1 ="C:\Library\doc1.docx"
Documents.Open fileName:=NewQuote1
...
Documents(NewQuote1).Activate
1

1 Answer 1

4

I'm not up on my Word VBA, but I'm pretty sure it'll be very similar to Excel.

So:

Sub Test()

    Dim wrdDoc As Document
    Dim NewQuote1 As String

    NewQuote1 = "C:\Library\doc1.docx"

    Set wrdDoc = Documents.Open(NewQuote1)

    'You can now reference the document using wrdDoc.
    wrdDoc.Activate

    MsgBox wrdDoc.Name

End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

that's very helpful. Thanks
Just noticed in your question that you talk about the document containing the VBA project - this document can always be referenced using ThisDocument, so ThisDocument.Activate, Msgbox ThisDocument.Name, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.