使用VBA在Excel中使用Acrobat PDFMaker生成脚本PDF

时间:2019-05-08 11:49:24

标签: excel vba acrobat

我正在尝试在Excel中编写Acrobat PDFMaker脚本,以从Excel文档中自动生成PDF(我需要维护超链接)。我找到了一些代码,但是我不断收到“运行时错误424:需要对象”。我觉得这与某些变量的分配方式有关,但我不确定。

' ----------------------------------------------------
' ConvertToPDFWithLinks - convert the current document
' to a PDF file, in the same folder as the document
' ----------------------------------------------------
Sub ConvertToPDFWithLinks()
Dim pdfname, i, a
Dim pmkr As AdobePDFMakerForOffice.PDFMaker
Dim stng As AdobePDFMakerForOffice.ISettings
If Not ActiveDocument.Saved Then
  MsgBox "You must save the document before converting it to PDF", vbOKOnly, ""
  Exit Sub
End If
Set pmkr = Nothing ' locate PDFMaker object
For Each a In Application.COMAddIns
  If InStr(UCase(a.Description), "PDFMAKER") > 0 Then
    Set pmkr = a.Object
    Exit For
  End If
Next
If pmkr Is Nothing Then
  MsgBox "Cannot Find PDFMaker add-in", vbOKOnly, ""
  Exit Sub
End If
pdfname = ActiveDocument.FullName ' construct output name
i = InStrRev(pdfname, ".")
pdfname = IIf(i = 0, pdfname, Left(pdfname, i - 1)) & ".pdf"
' delete PDF file if it exists
If Dir(pdfname) <> "" Then Kill pdfname
pmkr.GetCurrentConversionSettings stng
stng.AddBookmarks = True
stng.AddLinks = True
stng.AddTags = True
stng.ConvertAllPages = True
stng.CreateFootnoteLinks = True
stng.CreateXrefLinks = True
stng.OutputPDFFileName = pdfname
stng.PromptForPDFFilename = False
stng.ShouldShowProgressDialog = True
stng.ViewPDFFile = False
pmkr.CreatePDFEx stng, 0 ' perform conversion
If Dir(pdfname) = "" Then ' see if conversion failed
  MsgBox "Could not create " & pdfname, vbOKOnly, "Conversion failed"
End If
End Sub

0 个答案:

没有答案