View Single Post
 
Old 07-30-2019, 07:45 AM
guiltyvictim guiltyvictim is offline Windows 7 64bit Office 2010
Novice
 
Join Date: Jul 2019
Posts: 2
guiltyvictim is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Not necessarily. My old friend Greg Maxey has an interesting custom mail merge application at Enhanced Merge (Merge Many to One) which employs content controls rather than merge fields and will work with Excel and Access data sources.
Thank you for the suggestion! It's a pretty amazing feat that someone's built that in macros!!

Unfortunately after downloading and playing with it I've noticed that it outputs individual word documents from the merge, and not a single document with all the records, which is what I need.


The good news is, after looking around for what can be done with VBA, I've come up with a solution that works for my purpose, and I'll share it just in case it can help with anyone else:

Since merged documents have individual sections, with their independent headers and footers, I know I can populate the reference number in the header of each bid.

The next problem is I didn't know how to reference the content controls per section, but turns out it's easily done, by using Section.Range.ContentControls.

So the solution is:

- Put the reference number merge field inside the header of your pre-merged word template.
- Use the following VBA code:

Code:
For Each Sec In ActiveDocument.Sections
    For Each CCtl In Sec.Range.ContentControls
        If CCtl.Title = "ref_no" Then
            CCtl.Range.Text = Sec.Headers(wdHeaderFooterPrimary).Range.Text
        End If
    Next
Next
The code basically loops through each section, then loops through each content control within that section, checking whether they're the content control we're look for. Then it just puts whatever's in the header into the content control.

The limitation is probably obvious, but this approach basically means you can't have anything else within your header. Although at the same time you can probably do something clever like using a delimiter to truncate unwanted header content, whilst putting an invisible (white on white, font size 1) merge field into the header as the very first thing.

Hope it's helpful to anyone else!
Reply With Quote