View Single Post
 
Old 06-18-2015, 05:17 AM
svo svo is offline Windows 7 64bit Office 2003
Novice
 
Join Date: Jun 2015
Posts: 7
svo is on a distinguished road
Default [SOLVED] Appending a mail merge document to a static file

Hello there!

Since I said I will do a follow up post here that manaes to do what I wanted all along.

  • So to just make the whole situation a bit worse, the document changed a bit to being a static document with 12 pages. After these 12 pages there should follow a mail merge document with 3 x n pages. After that there was another static page.
  • The static document is build like this: first 12 pages, 1 empty page, last static page. On the empty page there is a book mark called "warehouseregister".
  • The mail merge document is layout-wise the same as the static document (page setup, headers, footer etc. all the same)
  • My templating engine calls a method "AfterTemplating" when it is done replacing all the placeholders with field values.

I used this call a VBA document macro to get into the process. When the call is made, I can be sure the template is filled with all the values from the templating engine. Now with VBA I
  1. open the mail merge document
  2. attach a data source
  3. run the mail merging
  4. save the document
  5. use the bookmark to setup an INCLUDETEXT field that pulls in the mail merged document
  6. reset the section breaks so that the page numbering is not screwed
And here the shortened VBA that handles all this (I do some more stuff like actually generating the datasource on the fly):
Code:
Public Sub AfterTemplating()
    Dim SourceDocument As Document
    Dim warehouseListTemplate As Document
    

    ' For jumping windows I need a reference
    Set SourceDocument = Documents(ActiveDocument.Name)
    
    ' opening the warehouse tmplate
    Set warehouseTemplate = Documents.Open(FileName:="warehouseTemplate.doc")
    ' Set it as the ActiveWindow and ActiveDocument
    warehouseTemplate.Activate
    ' Add the DataSource which I beforehand have generated
    warehouseTemplate.MailMerge.OpenDataSource Name:="warehouses.xls"
    ' start the mail merge and get the resulting mail merged document
    warehouseTemplate.MailMerge.Execute
    ' save the mail merged document
    ActiveDocument.SaveAs ("warehousesMerged.doc")
    ' Do not alter the template and close it
    Documents(warehouseTemplate.doc").Close SaveChanges:=False
    ' close the mail merged document
    Documents("warehousesMerged.doc").Close
    ' Switch ActiveWindow and ActiveDocument to the original document 
    Documents(SourceDocument).Select
    ' Goto to the Bookmark at the page before last page
    Selection.GoTo What:=wdGoToBookmark, Name:="warehouseregister"
    
    ' Select the bookmark and add a INCLUDEFIELD with the mail merged document
    Selection.Range.Select
    Selection.Fields.Add Range:=Selection.Range, Type:=WdFieldType.wdFieldIncludeText, Text:=Chr(34) & "warehousesMerged" & Chr(34), PreserveFormatting:=False
    ' Update the fields so the included file gets displayed
    ActiveDocument.Fields.Update
    
    ' Delete the Section breaks so that my page numbering in the footer is not screwed
    DeleteSectionBreaks
End Sub

Sub DeleSectionBreaks()
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "^b"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False
        .MatchFuzzy = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Reply With Quote