none
Access to Word 2010 Merge RRS feed

  • Question

  • Hi Guys,

    The following code is base in an Access 2010 module works ok but for the fact it opens the template .dot file as well as the merged output document. How do I stop the Word template opening at merge time?

    Kind Regards xjohn

    Function MergeIt()
       Dim objWord As Word.Document
       Dim objPath As String
        
       Set objWord = GetObject("Z:\Database\StdLetters\Community\1stVisit.dot", "Word.Document")
       ' Make Word visible.
       objWord.Application.Visible = True
       ' Set the mail merge data source as the  database.
       objWord.MailMerge.OpenDataSource _
          Name:="Z:\Database\BackEndData\Smile_Community_Family_be_2007.accdb", _
          LinkToSource:=True, _
          Connection:="Query qry_1stVisit_Letter", _
         SQLStatement:="SELECT * FROM [qry_1stVisit_Letter]"
       ' Execute the mail merge.
        objWord.MailMerge.Execute

        Set objWord = Nothing
      End Function

    Friday, January 22, 2016 10:22 AM

Answers

  • >>>You code works fine but still opens both the template and a new merged doc.

    I can close the template with code but I feel I should not have to do that. I shall pursue this further. <<<

    According to your description, if you do it manually, you could find that both the template and a new merged doc have been opened. So I suggest you close the template with code:

    objApp.Documents(1).SaveAs (strDir & "\" & strMergedDocName & ".doc")
    'close the merge template without saving
    objApp.Documents(2).Close wdDoNotSaveChanges 

    In addition if you have any feedbacks for Word, please feel free to submit them to User Voice:

    https://word.uservoice.com/

    Thanks for your understanding.

    • Marked as answer by XJohn Tuesday, February 2, 2016 9:54 AM
    Tuesday, February 2, 2016 8:06 AM

All replies

  • >>>How do I stop the Word template opening at merge time?

    According to your description, I suggest that you could modify your codes like below:

    Dim objApp As New Word.Application
    Dim objWord As Word.Document
    Dim objPath As String
    
    Set objWord = objApp.Documents.Add(Template:="Z:\Database\StdLetters\Community\1stVisit.dot")
    ' Make Word visible.
    objWord.Application.Visible = True
    ' Set the mail merge data source as the  database.
    objWord.MailMerge.OpenDataSource _
      Name:="Z:\Database\BackEndData\Smile_Community_Family_be_2007.accdb", _
      LinkToSource:=True, _
      Connection:="Query qry_1stVisit_Letter", _
      SQLStatement:="SELECT * FROM [qry_1stVisit_Letter]"
    ' Execute the mail merge.
    objWord.MailMerge.Execute
    
    Set objWord = Nothing
    Set objApp = Nothing
    

    For more information, click here to refer about How to use automation to run Word 2000 mail merge from Access 2000 and here to refer about Documents.Add Method (Word)

    Monday, January 25, 2016 8:28 AM
  • Cheers David I'll try that and come back to you

    regards

    xjohn


    xjohn

    Monday, January 25, 2016 9:05 AM
  • David

    You code works fine but still opens both the template and a new merged doc.

    I can close the template with code but I feel I should not have to do that. I shall pursue this further. 

    Kind Regards

    xjohn


    xjohn

    Tuesday, January 26, 2016 1:57 PM
  • >>>You code works fine but still opens both the template and a new merged doc.

    I can close the template with code but I feel I should not have to do that. I shall pursue this further. <<<

    According to your description, if you do it manually, you could find that both the template and a new merged doc have been opened. So I suggest you close the template with code:

    objApp.Documents(1).SaveAs (strDir & "\" & strMergedDocName & ".doc")
    'close the merge template without saving
    objApp.Documents(2).Close wdDoNotSaveChanges 

    In addition if you have any feedbacks for Word, please feel free to submit them to User Voice:

    https://word.uservoice.com/

    Thanks for your understanding.

    • Marked as answer by XJohn Tuesday, February 2, 2016 9:54 AM
    Tuesday, February 2, 2016 8:06 AM