Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Word 2007: Macro in template not available in mail merged document

Options
  • 10-06-2010 5:26pm
    #1
    Registered Users Posts: 7,541 ✭✭✭


    Hey all.

    I have the following issue in Word 2007. I have two files, template.dot - template for the mail merge and data.csv - data for the mail merge. I created the merged document programmatically as follows:

    Sub performMailMerge
    Dim strDataSource
    Dim objWord
    Dim docWord
    Dim wrdApp

    Set wrdApp = CreateObject("Word.Application")

    strDataSource = outputFile

    Set objWord = CreateObject("Word.Application")
    Set docWord = objWord.Documents.Open(templateFile)

    docWord.MailMerge.OpenDataSource strDataSource
    docWord.MailMerge.Destination = 0

    docWord.MailMerge.Execute False
    docWord.Close False

    objWord.Visible = True
    objWord.Activate

    Set objWord = Nothing
    Set docWord = Nothing

    End Sub

    This works fine, but the document created does not contain the macros in the template.dot file. If I create a new document (unmerged) from the template the macros are available.

    How can I create the merged document that also retains the macros from template.dot? I'd prefer not to go near normal.dot to do this.

    Thanks


Comments

  • Registered Users Posts: 7,541 ✭✭✭irlrobins


    Ugh, as always I spent nearly a day on a problem and then as soon as I post about it on boards 10 mins later I work out the solution. FFS..

    Anyway, the solution:
    Change Set docWord = objWord.Documents.Open(templateFile)
    to
    Set docWord = objWord.Documents.Add(templateFile)


Advertisement