0

I am having an issue trying to adjust a macro to export as pdf rather than a .dox

    ' Find the last record of the Mail Merge data
    ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord
    lastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord

    ' Ask for user confirmation to start creating the documents
    If MsgBox(lastRecord & " documents will be created based on your Mail Merge template.", vbOKCancel) = vbOK Then
        ' Ask for the name of the Merge Field name to use for the document names
        docNameField = InputBox("Which Mergefield [name] should be used for document name?")

        ' Create document for each Mail Merge record (loop)
        For rec = ActiveDocument.MailMerge.DataSource.FirstRecord To lastRecord
            ActiveDocument.MailMerge.DataSource.ActiveRecord = rec

            ' Set document name for current record
            If Trim(docNameField) = "" Then
                strDocName = "document" & rec & ".docx"
            Else
                strDocName = ActiveDocument.MailMerge.DataSource.DataFields(docNameField).Value & ".docx"
            End If

            ' Execute Mail Merge action
            With ActiveDocument.MailMerge
                .Destination = wdSendToNewDocument
                .Execute
            End With

            ' Save generated document and close it after saving
            ActiveDocument.SaveAs FileName:=savePath & strDocName
            ActiveDocument.Close False

            ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
        Next rec

        ' Re-enable screen visuals
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True

    Else 'if no destination folder was selected
        'Re-enable screen visuals
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
        Exit Sub
    End If
End If

End Sub

I have tried to utilise activedocument.exportasfixedformat but cannot get this to work. Id appreciate any guidance.

Regards

2
  • Why not just end the End If before the Application.ScreenUpdating & DisplayAlerts so you don't have to type it twice? Short code generally (not always - in your case however) leads to improved readability. This doesn't answer your question which is why it's in the comment section instead as an answer. Commented Feb 7, 2018 at 15:35
  • ActiveDocument.SaveAs FileName:=savePath & strDocName is simply "saving as" which will cause Word to save in its default docx format. Where's the code that does the .ExportAsFixedFormat and what kind of errors does it throw? Commented Feb 7, 2018 at 15:53

1 Answer 1

2

As you said you should use ExportAsFixedFormat, something like this.

ActiveDocument.ExportAsFixedFormat _
 OutputFileName:=savePath & strDocName & ".pdf", _
 ExportFormat:=wdExportFormatPDF
Sign up to request clarification or add additional context in comments.

11 Comments

Hi Apologies I now have an additional problem. the file name is set by the mailmerge source but it can only find one of them. I get a runtime 5941 when selecting a mailmerge source field as the name for the file but this doesnt happen to every mail merger source
How your datasource look like? When you che k in the Word document can you see more records in the Mailing ribbon?
Hi yes i can see more than one records within the mailing ribbon. Its strange the program runs correctly with a specific mail merge source header but not the rest. I tested with a test document and data source and a similar issue happened
Do you input a valid fieldname when the msgbox appears? Like [fileName]? It should be a column name in square brackets, if it contains space swap it with an underscore _ eg.: Document File Name becomes [Document_File_Name].
And Delete the .docx append too pretty useless
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.