Hi,
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, _
UseISO19005_1:=False
.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
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, _
UseISO19005_1:=False
.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