Azhar Iqbal
Azhar Iqbal

Reputation: 33

Exporting as PDF VBA

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

Upvotes: 0

Views: 559

Answers (1)

Mátray Márk
Mátray Márk

Reputation: 466

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

ActiveDocument.ExportAsFixedFormat _
 OutputFileName:=savePath & strDocName & ".pdf", _
 ExportFormat:=wdExportFormatPDF

Upvotes: 2

Related Questions