#1
|
|||
|
|||
Doc to PDF mail merge
Hi guys
I have a mail merge document with all the letters separated by a section break. I'm wondering if it would be easy to write a macro that finds a particular word in each letter/section and uses that as a string for the file name (e.g. it searches for the words 'strictly private' and then uses the line beneath it which is the client's name), and then saves each section as a separate PDF file? Thanks a lot in advance James |
#2
|
||||
|
||||
See Merge and Split which has an option to do that.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Quote:
Thanks! I'm trying to avoid add-ins as it's for my workplace. I'll post a code I was working on, maybe someone can help adjust it to work. |
#4
|
|||
|
|||
So here's a macro I was working on:
So it starts by asking what folder I want to save the PDFs to, and then it copies each section to a new document, uses the line UNDER 'strictly private' as a string for the filename + today's date, and then saves. However I don't want it to copy each letter new document, but I'm not sure how to achieve this. Sub PDFBySection() 'Select folder' Dim sFolder As String ' Open the select folder prompt With Application.FileDialog(msoFileDialogFolderPicker) .InitialFileName = "U:" .Title = "Select a folder to save to" If .Show = -1 Then ' if OK is pressed sFolder = .SelectedItems(1) End If End With If sFolder <> "" Then ' if a file was chosen Selection.HomeKey Unit:=wdStory Application.Browser.Target = wdBrowseSection 'Copy letter to new document' For i = 1 To ((ActiveDocument.Sections.Count) - 1) ActiveDocument.Bookmarks("\Section").Range.Copy Documents.Add Selection.Paste 'Removes the break that is copied at the end of the section, if any. Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend Selection.Delete Unit:=wdCharacter, Count:=1 'Grabs client name' Selection.HomeKey Unit:=wdStory With Selection.Find .Forward = True .ClearFormatting .MatchWholeWord = True .MatchCase = False .Wrap = wdFindContinue .Execute FindText:="Strictly Private" End With Selection.MoveDown Unit:=wdLine, Count:=1 Selection.HomeKey Unit:=wdLine Selection.EndKey Unit:=wdLine, Extend:=wdExtend Selection.Copy 'Get current date for filename' Dim xDate As String On Error Resume Next xDate = Format((Year(Now() + 1) Mod 100), "20##") & _ Format((Month(Now() + 1) Mod 100), "0#") & _ Format((Day(Now()) Mod 100), "0#") 'Create a string for file name from line 6 where client's name is' Dim strTemp As String Dim MyData As DataObject Set MyData = New DataObject MyData.GetFromClipboard 'String to put date first then client name second for file name' strTemp = xDate & " " & MyData.GetText(1) 'Save as PDF' ActiveDocument.SaveAs FileName:=(sFolder) & "" & (strTemp) & ".PDF", FileFormat:=wdFormatPDF ActiveDocument.Close savechanges:=wdDoNotSaveChanges 'Move the selection to the next section in the document Application.Browser.Next Next i End If End Sub |
#5
|
||||
|
||||
My add-ins are in use by government, military and industry around the world. Some users even donate for using them. I am surprised that your company will let you use VBA to screw up their systems and not allow the use of add-ins.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#6
|
|||
|
|||
Quote:
That doesn't really help, but yeah, they're only simple macros for basic tasks, nothing that can actually screw anything up |
#7
|
|||
|
|||
Quote:
|
#8
|
||||
|
||||
See Send Mailmerge Output to Individual Files in the Mailmerge Tips and Tricks 'Sticky' thread at the top of this forum:
https://www.msofficeforums.com/mail-...ps-tricks.html
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Quote:
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Mail Merge with Fields extracted from ROWS instead of COLUMNS or transpose of typical mail merge | djrexy | Mail Merge | 1 | 05-20-2020 04:04 PM |
Update & Unlink Specific Merge Field in Word Doc from Mail Merge - Excel VBA | RMerckling | Mail Merge | 16 | 05-17-2018 05:19 PM |
How do I mail merge records into table columns with more than one merge field? | tech123 | Mail Merge | 1 | 04-26-2017 07:13 PM |
Mail Merge is Deleting objects in my header and footer during the merge | bgranzow | Mail Merge | 9 | 06-05-2015 05:03 AM |
Mail merge how to link mail merge field value to a column heading | dsummers | Mail Merge | 1 | 05-08-2014 02:59 PM |