首页 > 解决方案 > 如何使用vba excel打印为pdf多个word文档

问题描述

我有一个名称列表,我需要将它们中的每一个合并到预设的 Word 文档中,然后以 PDF 格式导出/打印/保存每个“证书”。

我在一段时间内放置了更改 Word 文档的代码,更改后我调用了我正在调用的函数“Imrpimir PDF”。这是整个代码:

While ThisWorkbook.Sheets("Lista de Presença").Cells(i, 2) <> Empty
    If ThisWorkbook.Sheets("Lista de Presença").Cells(i, 4) = "Presente" Then

        participante = UCase(ActiveWorkbook.Worksheets("Lista de Presença").Cells(i, 2))

        Set wrdApp = CreateObject("Word.Application")
        wrdApp.Visible = True

        Set wrdDoc = wrdApp.Documents.Open(ThisWorkbook.Path & "\" & ActiveWorkbook.Worksheets("Evento").Cells(1, 7).Text & " " & ActiveWorkbook.Worksheets("Evento").Cells(1, 2).Text & "\" & "Modelo.docx")

        With wrdDoc
            .Application.Selection.Find.Text = "#NOME"
            .Application.Selection.Find.Execute
            .Application.Selection.Range = participante

            Imprime_PDF (participante)

            Do
                On Error Resume Next
                Set wrdApp = GetObject(, "Word.Application")
                If Not objWord Is Nothing Then
                    wrdApp.Quit False
                    Set wrdApp = Nothing
                End If
            Loop Until wrdApp Is Nothing
        End With

        Set wrdApp = Nothing
        Set wrdDoc = Nothing
    End If
    i = i + 1
Wend
End Sub

函数“Imprime_PDF”是:

Function Imprime_PDF(participante)
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
ThisWorkbook.Path & "\" & ActiveWorkbook.Worksheets("Evento").Cells(1, 7).Text & " " & ActiveWorkbook.Worksheets("Evento").Cells(1, 2).Text & "\" & ActiveWorkbook.Worksheets("Evento").Cells(1, 2).Text & " - Certificado de " & participante & ".pdf ", ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, _
Range:=wdExportAllDocument, _
IncludeDocProps:=True, _
CreateBookmarks:=wdExportCreateWordBookmarks, _
BitmapMissingFonts:=True
End Function

同时,此代码更改了所有 Word 文档,但仅将第一个文档另存为 pdf。

先感谢您!

标签: excelvbapdfms-wordexport

解决方案


推荐阅读