Copying contents of Word doc to newly created Word doc from excel VBA

前端 未结 1 713
执念已碎
执念已碎 2020-12-20 05:14

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 (an

1条回答
  •  借酒劲吻你
    2020-12-20 05:32

    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.

    0 讨论(0)
提交回复
热议问题