Microsoft Office Tutorials and References
In Depth Information
Mail Merge: Creating a One-to-Many List
Private Sub WdApp_MailMergeAfterMerge(ByVal Doc As Document, _¶
ByVal DocResult As Document)¶
'Release the data¶
Set rs = Nothing¶
'Delete the last table and restore the bookmark¶
If Doc.Bookmarks.Exists(BookmarkName) Then¶
'Variable declaration¶
Dim rng As Word.Range¶
Set rng = Doc.Bookmarks(BookmarkName).Range¶
If rng.Tables.Count > 0 Then¶
End If¶
Doc.Bookmarks.Add Range:=rng, Name:=BookmarkName¶
End If¶
MsgBox "Merge process has finished!"¶
'Display the merge result document¶
If Not DocResult Is Nothing Then¶
DocResult.ActiveWindow.View.TableGridlines = False¶
End If¶
End Sub¶
' * * * * *¶
Private Sub WdApp_MailMergeBeforeRecordMerge( _¶
ByVal Doc As Document, Cancel As Boolean)¶
'Variable declaration¶
Dim bkm As Word.Bookmark¶
'If something is wrong, don't continue¶
'processing each record¶
If CancelMerge = True Then¶
Debug.Print "Cancelled. Record: " & CStr(recordIndex)¶
Cancel = True¶
Exit Sub¶
End If¶
'The file containing the data for the merge¶
'should only be opened once. Therefore,¶
'track when the merge has started¶
If BeforeMergeExecuted = False Then¶
BeforeMergeExecuted = True¶
MsgBox sMergeMessage, vbCritical + vbOKOnly¶
Set rs = New ADODB.Recordset¶
rs.CursorLocation = adUseClient¶
'Retrieve the entire recordset¶
'then get the individual records for each pupil¶
GetData rs¶
End If¶
If rs.RecordCount <= 0 Then¶
MsgBox "There is no data to process."¶
CancelMerge = True¶
Cancel = True¶
Exit Sub¶
Search JabSto ::

Custom Search