Macro no longer works to save as multiple pdfs now


New Member
May 18, 2015

This is my first time posting on this site as a last resort to fix some coding errors that popped up on a macro I have been working on. Recently my company upgraded from office 2010 to 2013 and a macro I have been using to take a mail merge doc and save each individual mail merge doc as a pdf with a unique name based on select fields from the mail merge doc began erroring out when the macro gets to the point of saving the document name. This code was working great on 2010 but the moment we began using 2013 it stopped working and I can not figure out why. Below is the code and anyone that could help point me in the right direction would be amazing!!

Sub Merge_to_pdf()
'merges one record at a time to the chosen output folder

Application.ScreenUpdating = False
Dim StrFolder As String, StrName As String, MainDoc As Document, i As Long
StrFolder = GetFolder
If StrFolder = "" Then Exit Sub
Set MainDoc = ActiveDocument
With MainDoc
For i = 1 To .MailMerge.DataSource.RecordCount
With .MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = i
.LastRecord = i
.ActiveRecord = i

If Trim(.DataFields("id")) = "" Then Exit For
StrName = .DataFields("id").Value & " - " & .DataFields("client").Value 'fix the name later
End With
.Execute Pause:=False
End With
' below is where the macro errors out at
With ActiveDocument
.ExportAsFixedFormat OutputFileName:=StrFolder & "\" & StrName & ".pdf", _
ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, _
Range:=wdExportAllDocument, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, _
KeepIRM:=True, DocStructureTags:=True, BitmapMissingFonts:=True, _
.Close SaveChanges:=False
End With
Next i
End With
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Welcome to MrExcel forums.

It would help if you stated the error message. Also you've posted in the Excel Questions forum and I believe your question is about Word.

The problem could simply be that the Word 2010 Save as PDF parameters (in the ExportAsFixedFormat statement) are incompatible with Word 2013. Therefore I would try recording a macro in Word 2013 to save a document as PDF and see if you can incorporate the generated code into your macro.

PS - when posting code please put it inside CODE tags, like this:

VBA code here
Upvote 0
Thanks for the Reply John_w!

Sorry about the code formatting, the error message is "run time error '-2147467259 (80004005)' " I havent been able to find much about this error online other than that its says it is not a valid file name and less about fixes. Yes, this is in Word instead of Excel.

Thanks for at least taking a look.
Upvote 0
Oh and I did a recorded macro and the formatting that came back seemed to Match the same formatting that I have in the code other than the name since I am using data fields from the mail merge. However this worked fine in 2010 and as far as I know there is no reason it shouldn't work in 2013. I'm stumped at this point and been staring at the code tweaking for about a week.
Upvote 0

Forum statistics

Latest member

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back