` Printed Icetips Article

Icetips Article



OLE/ActiveX: MS-Word mail-merge macros
2004-07-02 -- Chris Rybitski
 
Newsgroups: softvelocity.clarion.language

Arnor,

FWIW, here is a VB example of using Word automation.  It's a late-bound
example, but the early-bound code is still there, just commented.  HTH!


'======================================================
Public Sub DoMailMerge(ByVal psMergeDoc As String, ByVal psNewDoc As String,
ByVal psDataFile As String, ByVal pbAutoStart As Boolean, Optional ByVal
pbSuppressBlankLines As Boolean = True)
On Error Resume Next

'Early binding on MSWord is depreciated
'
'    Dim loWordApp   As Word.Application
'    Dim loWordDoc   As Word.Document
'    Dim loMrgDoc    As Word.Document

    Dim loWordApp   As Object
    Dim loWordDoc   As Object
    Dim loMrgDoc    As Object


    'Set loWordApp = New Word.Application
    Set loWordApp = CreateObject("Word.Application")

    Set loWordDoc = loWordApp.Documents.Open(psMergeDoc)

    With loWordDoc.MailMerge

        .MainDocumentType = 0 ' - ENUM WdMailMergeMainDocType.wdFormLetters
        .OpenDataSource psDataFile, 4 ' - ENUM WdOpenFormat.wdOpenFormatText
        '.SuppressBlankLines = True
        .SuppressBlankLines = pbSuppressBlankLines
        .Destination = 0 ' - ENUM WdMailMergeDestination.wdSendToNewDocument
        .Execute Pause:=False

    End With

    For Each loMrgDoc In loWordApp.Documents

        If loMrgDoc.Name = loWordDoc.Name Then
            loWordDoc.Close SaveChanges:=False
        Else
            loMrgDoc.SaveAs (psNewDoc)

            If pbAutoStart Then
                loMrgDoc.Close SaveChanges:=True
            End If

        End If

        Set loMrgDoc = Nothing

    Next

    Set loMrgDoc = Nothing
    Set loWordDoc = Nothing

    If pbAutoStart Then
        loWordApp.Visible = True
        loWordApp.Documents.Open psNewDoc
    Else
        loWordApp.Quit
    End If

    Set loWordApp = Nothing

End Sub
'======================================================



Printed April 28, 2024, 7:45 am
This article has been viewed/printed 35119 times.
Google search has resulted in 139 hits on this article since January 25, 2004.