在VBA中导出为PDF

时间:2019-12-15 04:00:25

标签: excel vba pdf

我想创建一个自动的证书发行过程,使我可以“另存为”具有新标题的现有文档,并为其创建PDF。

我面临的问题是在PDF导出例程中,我在下面对此进行了评论。

Sub GerarCertificado()

Dim Word As Word.Application
Dim DOC As Word.Document
Dim aux As Long

For aux = Range("F8").Value To Range("F11").Value

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

Set DOC = Word.Documents.Open("C:\Users\vitor\Downloads\Certificados\Declaracao.doc")

With DOC

.Application.Selection.Find.Text = "#NOME"
.Application.Selection.Find.Execute
.Application.Selection.Range = Sheets("Gerar Certificado").Cells(aux, 1)

.Application.Selection.Find.Text = "#PALESTRA"
.Application.Selection.Find.Execute
.Application.Selection.Range = Sheets("Gerar Certificado").Cells(aux, 2)

.Application.Selection.Find.Text = "#HORAS"
.Application.Selection.Find.Execute
.Application.Selection.Range = Sheets("Gerar Certificado").Cells(aux, 3)

.Application.Selection.Find.Text = "#DIA"
.Application.Selection.Find.Execute
.Application.Selection.Range = Sheets("Gerar Certificado").Cells(aux, 4)

.Application.Selection.Find.Text = "#DATA"
.Application.Selection.Find.Execute
.Application.Selection.Range = Sheets("Gerar Certificado").Cells(aux, 5)

.SaveAs ("C:\Users\vitor\Downloads\Certificados\Certificados\" & Sheets("Gerar Certificado").Cells(aux, 1) & ".doc")

'Here starts the error
.ExportAsFixedFormat OutputFileName:=_
    "C:\Users\vitor\Downloads\Certificados\Certificados\" & Sheets("Gerar Certificado").Cells(aux, 1) & ".pdf",_
    ExportFormat:=wdExportFormatPDF,_
    OpenAfterExport:=True, OptimizeFor:=wdExportOptimizeForPrint, Range:=_
    wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent,_
    IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:=_
    wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:=_
    True, UseISO19005_1:=False

.Close

Word.Quit
Set DOC = Nothing
Set Word = Nothing

End With

Next aux

MsgBox "Certificados gerados com sucesso!", vbInformation

End Sub

有人可以提供帮助或改进它以获得更好的解决方案吗?我真的很感激。谢谢。

2 个答案:

答案 0 :(得分:0)

我认为您需要在行的每个下划线之前放置一个空格。像这样:

现在

.ExportAsFixedFormat OutputFileName:=_

像这样改变

.ExportAsFixedFormat OutputFileName:= _

答案 1 :(得分:0)

是的,谢谢。贝娄是纠正。

.ExportAsFixedFormat OutputFileName:= _
"C:\Users\vitor\Downloads\Certificados\Certificados\" & Sheets("Gerar Certificado").Cells(aux, 1) & ".pdf", _
ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=True, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _
IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:= _
True, UseISO19005_1:=False