导出为PDF VBA

时间:2018-02-07 15:27:02

标签: vba excel-vba excel

我在尝试调整宏以导出为pdf而不是.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

我曾尝试使用activedocument.exportasfixedformat但无法使其正常工作。我很欣赏任何指导。

此致

1 个答案:

答案 0 :(得分:2)

如你所说,你应该使用ExportAsFixedFormat,就像这样。

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