Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #6  
Old 06-18-2015, 05:17 AM
svo svo is offline Appending a mail merge document to a static file Windows 7 64bit Appending a mail merge document to a static file Office 2003
Novice
Appending a mail merge document to a static file
 
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
 

Tags
includetext, mail merge



Similar Threads
Thread Thread Starter Forum Replies Last Post
File name and Doc settings are printing and appending to my Word docs. wranglerman4you Word 4 12-30-2013 12:45 AM
Appending a mail merge document to a static file check for duplicate before appending to new file rcVBA Word VBA 4 05-21-2013 11:40 AM
Appending a mail merge document to a static file Split multi-page mail merge document, then name file from letter info. BriMan83 Mail Merge 1 04-24-2013 11:35 PM
converting a word document to a data file for mail merge drsuis Mail Merge 4 02-21-2013 03:34 PM
Merge Fields and Static Text kbranden Mail Merge 1 06-15-2011 09:02 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:10 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft