Search code examples
vbams-officems-wordword-2007

Assign a New Word Document to a Variable


I'm currently running a VBA macro in Word. Part of it requires creating a new blank document to run some of the work in. Right now, I just have Documents.Add DocumentType:=wdNewBlankDocument which I then refer to through Windows("Document1").Activate.

However, the problem is that if I want to run the macro more than once, it won't work because whenever it creates a new blank document, it will become "Document2/3. etc.". I basically need to assign a blank document to a variable so I can call it reliably. I know how to do this with documents that are already saved (ones that have a filename/filepath), but not sure what it would take for a new/blank document.

I'm currently working in Word 2007.


Solution

  • Absolutely no problem:

    Dim oDoc as Word.Document
    Set oDoc = Documents.Add(DocumentType:=wdNewBlankDocument)