gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:10:11 25 4
gpt4 key购买 nike

生成的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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com