Class: WordMerge in Category Microsoft Word : Automation from Total Visual SourceBook

Use Microsoft Word Automation to combine data from a Microsoft Access data source (table, query, or SQL string) to a Word file for mail merge from VBA and VB6.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CWordMerge class.
Class_Initialize Initialize Initialize.
IsOpen Private Determine if Microsoft Word is already open.
Class_Terminate Terminate Clean up Word object.
WordMailMerge Method Use a Word mail merge document to create a new document merged with data from the specified data source.
LaunchNewDocument Private Launches the new Word document and initiates the mail merge operation.
GetFileExtension Private Get the extension of a file (the text after the last period).
' Example of CWordMerge
'
' To use this example, create a new module and paste this code into it.
' Then run the procedure by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

Private Sub Example_CWordMerge()
  ' Comments: Examples of using the CWordMerge class to perform MailMerge from Microsoft Access using VBA.
  '           Examples include using a table, query and SQL string as the data source, and output to Word documents, PDF files, and the printer with and without the user seeing Word.

  Const cstrSamplePath As String = "C:\Total Visual SourceBook 2013\Samples\"
  Const cstrWordDoc As String = cstrSamplePath & "WordMailMergeTemplate.docx"
  Const cstrWordDocNew As String = cstrSamplePath & "WordMailMergeResults"
  Const cstrDatabase As String = cstrSamplePath & "sample.accdb"
  Const cfPrint As Boolean = False
  Const cfShowWord As Boolean = True
  Const cfHideWord As Boolean = False

  Dim clsWordMerge As New CWordMerge
  Dim fOK As Boolean
  Dim strSQL As String
  Dim strNewDoc As String

  With clsWordMerge
    strNewDoc = cstrWordDocNew & "1"
    If MsgBox("Do you want to create a Word Merge document " & strNewDoc & " with the first two customer records?", vbQuestion + vbYesNo) = vbYes Then
      ' Create a new Word document merging the records from a SQL string for the first two records, and display the new document
      strSQL = "SELECT TOP 2 * FROM Customers"
      fOK = .WordMailMerge(cstrWordDoc, strNewDoc, cstrDatabase, strSQL, cfPrint, cfShowWord)
      If fOK Then
        MsgBox "Go to Word to see the results in " & strNewDoc, vbInformation
      Else
        MsgBox strNewDoc & " could not be created", vbCritical
      End If
    End If

    strNewDoc = cstrWordDocNew & "2"
    If MsgBox("Do you want to create a Word Merge document " & strNewDoc & " with just the USA customers?", vbQuestion + vbYesNo) = vbYes Then
      ' Create a new Word document merging the records from a query that only includes USA addresses, and display the new document
      fOK = .WordMailMerge(cstrWordDoc, strNewDoc, cstrDatabase, "qryCustomersUSA", cfPrint, cfShowWord)
      If fOK Then
        MsgBox "Go to Word to see the results in " & strNewDoc, vbInformation
      Else
        MsgBox strNewDoc & " could not be created", vbCritical
      End If
    End If

    ' Don't provide a new document name, so the results are shown, but not saved
    strNewDoc = ""
    If MsgBox("Do you want to create a Word Merge document with all the customer records without saving it as a new file?", vbQuestion + vbYesNo) = vbYes Then
      ' Create a new Word document merging all the records from the "Customers" table and display the new document
      fOK = .WordMailMerge(cstrWordDoc, strNewDoc, cstrDatabase, "Customers", cfPrint, cfShowWord)
      If fOK Then
        MsgBox "Go to Word to see the results", vbInformation
      Else
        MsgBox "Could not be created", vbCritical
      End If
    End If

    ' Provide the new name as a PDF to have a PDF file generated
    strNewDoc = cstrWordDocNew & ".pdf"
    If MsgBox("Do you want to create a Word Merge document in a PDF file with all the customer records?", vbQuestion + vbYesNo) = vbYes Then
      ' Create a new Word document merging all the records from the "Customers" table and create the PDF file without showing Word
      fOK = .WordMailMerge(cstrWordDoc, strNewDoc, cstrDatabase, "Customers", cfPrint, cfHideWord)
      If fOK Then
        MsgBox "Go to Adobe Reader to see the results: " & strNewDoc, vbInformation
      Else
        MsgBox "Could not be created", vbCritical
      End If
    End If

    strNewDoc = ""
    If MsgBox("Do you want to print a Word Merge document for the first customer record?", vbQuestion + vbYesNo) = vbYes Then
      ' Create a merge file with a SQL string for the first record and print it without showing the document to the user
      strSQL = "SELECT TOP 1 * FROM Customers"
      fOK = .WordMailMerge(cstrWordDoc, strNewDoc, cstrDatabase, strSQL, True, cfHideWord)
      If fOK Then
        MsgBox "Go to your printer to see the output" & strNewDoc, vbInformation
      Else
        MsgBox strNewDoc & " could not be created for printing", vbCritical
      End If
    End If

  End With

  Set clsWordMerge = Nothing

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS