'Mailmerge - Individual PDFs - Top 2 Rows NOT header

Sourced from https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html

I'm using the code as it stands for the individual PDFs on Word and I have 2 issues I can't resolve.

The code works (Praise Macropod!) however my excel file has 2 of the top rows before the header starts, so I cannot pull the .datafield from anything that isn't just the very first column as that top row is formatted as a merge and centre. This is a client document so I have to work with their data as given, I can't just remove those top 2 rows, I know that would solve it.

One idea I can't figure out is how to get the last line in this With:

  With .DataSource
    .FirstRecord = i
    .LastRecord = i
    .ActiveRecord = i
    If Trim(.DataFields("Last_Name")) = "" Then Exit For
    'StrFolder = .DataFields("Folder") & "\"
    StrName = .DataFields("Last_Name") & "_" & .DataFields("First_Name")

to pull in more than one field, I want it save as lastname_firstname.pdf but I tried a few things here

  • .DataFields("Last_Name") - this works.
  • .DataFields("Last_Name") & "_" & .DataFields("F2")- this doesn't work, well it does but then it errored out after a single one came out, save a single PDF file.

Is there a way to use the datafield as a sort of +1, IE I want A2_B2.pdf, can I write A2 & A2+1 in a way to get B2?

Other possibility is to reference the start point, in that case I don't know enough to add like the "Finish & Merge" button has the option From and does a range, that would help perhaps but I still need to sort out the datafields, that would have to work with that shift of sorts.

Thanks in advance



Solution 1:[1]

For the first 'issue' you might use something like:

Sub Merge_To_Individual_Files()
'Sourced from: https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
'Note: this code requires a reference to the Excel object model.
'See under the VBE's Tools|References.
Application.ScreenUpdating = False
Dim StrFolder As String, StrName As String, MainDoc As Document, i As Long, j As Long
Dim StrMMSrc As String, StrSht As String, StrTxt As String
Const StrNoChr As String = """*./\:?|"
With ActiveDocument.MailMerge.DataSource
  StrMMSrc = .Name: StrSht = Split(Split(.QueryString, "`")(1), "$")(0)
End With
Dim xlApp As New Excel.Application, xlWkBk As Excel.Workbook, xlWkSht As Excel.Worksheet
xlApp.Visible = False
Set xlWkBk = xlApp.Workbooks.Open(FileName:=StrMMSrc, ReadOnly:=True, AddToMru:=False)
Set xlWkSht = xlWkBk.Names(StrTbl).RefersTo
StrTxt = xlWkSht.Range("F2").Text
xlWkBk.Close: xlApp.Quit: Set xlWkSht = Nothing: Set xlWkBk = Nothing
Set MainDoc = ActiveDocument
...

I have no idea what your other 'issue' is about. In any event, you're supposed to limit questions to one per thread.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 macropod