vba - 创建的Word文档不会转换为PDF

标签 vba compiler-errors pdf-generation

生成的Word文档不会转换为PDF

我生成了一个Word文档,该文档从excel中填充了其中的书签,然后尝试导出为PDF。即使添加Microsoft Word Library 16.0后,也会不断出现错误。我在这里做错了什么?

显式期权

Sub GenerateTerminationLetter()

    Dim objWord As Object, docWord As Object
    Dim wb As Workbook
    Dim xlName As Name
    Dim Path, SavePath, TempPath, FileName3 As String
    Dim EmpFileName As String


        Set wb = ThisWorkbook

    ' ******************************* Primary Letter Template Location ***********************
    Sheets("FilePath").Select
        TempPath = Sheets("FilePath").Range("C16").Value
            If Right(TempPath, 1) <> "\" Then
                    TempPath = TempPath & "\"
                Else
            End If
        Path = TempPath & "Termination Letter (Redundancy A023 FPP) (NEW - With Whistle Blowing Statement).docx"

    '*******************************Populate Bookmarks ***************************************
    On Error GoTo ErrorHandler
     'Create a new Word Session
                Set objWord = CreateObject("Word.Application")
     'Open document in word
                Set docWord = objWord.Documents.Add(Path)
     'Loop through names in the activeworkbook
    For Each xlName In wb.Names
         'if xlName's name exists in the document then put the value at the bookmark
        If docWord.Bookmarks.Exists(xlName.Name) Then
            docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName.Value)
        End If
    Next xlName
    Sheets("Temp").Visible = xlVeryHidden

    '******************************* Activate word and display document **********************

    With objWord
        .Visible = True
        .Activate
    End With
    'Save Termination Letter
    FileName3 = Sheets("R-Copy").Range("D7").Value
    '******************************* Export as PDF ********************************************
    docWord.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=EmpFolder & "\" & "Termination Letter_" & FileName3, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    ExportFormat:=wdExportFormatPDF


    objWord.Quit
    'Release the Word object to save memory and exit macro

    ErrorExit:
                Set objWord = Nothing
                Exit Sub

    'Error Handling routine


    ErrorHandler:

       If Err Then
          MsgBox "Error No: " & Err.Number & "; There is a problem. Contact Administrator"
             If Not objWord Is Nothing Then objWord.Quit False
                Resume ErrorExit
       End If
    End Sub

错误448:联系管理员

最佳答案

哪条线触发了错误?我假设它的exportAsFixedFormat行。错误448是找不到命名参数,并且看来Type不是允许的参数之一。您可能需要ExportFormat:= wdExportFormatPDF,看起来好像已经包含了它,但是Type不是允许的参数,并且会导致错误。这是该方法的文档:https://docs.microsoft.com/en-us/office/vba/api/word.document.exportasfixedformat

看来您正在使用的其他一些参数也不是很正确,因为它们引用的是xl而不是wd类型,并且属性名称不太一致。尝试:

docWord.ExportAsFixedFormat _
    OutputFileName:=EmpFolder & "\" & "Termination Letter_" & FileName3, _
    IncludeDocProps:=True, _
    ExportFormat:=wdExportFormatPDF

另外,我不相信您在任何地方设置EmpFolder,所以它是一个空变量,这可能会使该方法失败或导致该方法将文件保存在错误的位置。

让我知道这是否适合您。

关于vba - 创建的Word文档不会转换为PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57862212/

相关文章:

excel - 将数字添加到唯一但无序的数字列表 - 工作表更改

vba - 将 .xfdf 保存为 .pdf

arrays - 有没有更快的方法来比较 VBA 中动态数组之间的数据?

c - 需要左值作为增量运算符

java - PDF 中不保留双倍空格

php - FPDF 错误 : Could not include font metric file

macos - 我如何让我的 "terminal"命令在 vba 的正确文件夹中运行(在 OSX 上)

c++ - 错误C2679二进制 '<<' : no operator found which takes a right-hand operand of type 'T'

xcode - 11415编译出错吗?

c# - 如何在每个创建的页面中自动重复 pdf 签名