Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

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

  • 10-06-2010 04:26PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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