1
votes

I am trying to use excel data to perform various tasks on a word document. However, this will be used by other people, so I wanted to copy all contents from the word doc (and formatting and tables) to a new word doc and edit the new one to prevent others from accidentally saving over the first one (template one). I also want to close the template (first) doc and keep the second (new) one open.

I am new to VBA and am having trouble understanding how to handle this. After a bunch of googling, I've compiled this (not working) code:

Dim WordApp As Object
Dim WordDoc As Word.Document
Dim NewWordDoc As Word.Document
'Dim other stuff'

'-------------------------------------------------------------------
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open("C:\Users\Windows\Documents\CRC Labels 
test.doc")
Set NewWordDoc = WordApp.Documents.Add 'I think this gives an error'
'------------------------------------------------------------------------
'Line that actives WordDoc'
Selection.WholeStory 'Select whole document
Selection.Expand wdParagraph 'Expands your selection to current paragraph
Selection.Copy 'Copy your selection
'Line that closes WordDoc'
'Line that activates NewWordDoc'
Selection.PasteAndFormat wdPasteDefault 'Pastes in the content
'------------------------------------------------------------------------
'Do stuff with NewWordDoc'


'------------------------------------------------------------------------
Set WordDoc = Nothing
Set WordApp = Nothing

Any other ideas of handling this situation? Thank you for your response.

1
The template is saved as a doc. It’s the one that’s named CRC labels test.doc. What’s the advantage of saving as a template? Also, I used add for the NEW file I’m trying to make (I don’t wanna save that file, just copy everything over, do my VBA code, and pop it up onto the screen and allow users to manually edit and print if they want.James Carter

1 Answers

1
votes

When you find yourself using an existing document as the basis for creating new documents it's time to consider saving that document as a Word Template. This is a particular type of file (*.dotx or *.dotm) that may contain

  • boiler-plate text
  • Building blocks with additional boiler-plate to be inserted as required
  • styles (formatting command sets)
  • customized keyboard shortcuts
  • Ribbon customizations
  • VBA code (macros, in *.dotm, only)

that will be inherited and/or shared by all documents generated from the template.

In order to create a new document from a template, use the Documents.Add method, specifying the path and file name:

Dim wdDoc as Word.Document
Set wdDoc = Documents.Add(path & filename) 
'From outside Word: Set wdDoc = wdApplication.Documents.Add

This will create a new document from the template, copying the complete template content without affecting the template, itself.

Note: You can also use a "plain document" (*.docx) with the Documents.Add method and it will copy the content. It will not, however, link back to that document so it can't share macros, for instance.