首页 > 解决方案 > 使用 excel 从 Word 创建 pdf:document.exportasfixedformat 未创建文件的扩展名

问题描述

我正在使用 Excel vba 来修改 Word 文档,然后将其另存为 pdf。它运行顺利,它会创建一个 pdf 文件,但不会创建扩展名。所以基本上问题是文件不被识别为pdf(图标不是pdf图标)并且在打开时,它会要求应用程序用来打开文件。在此文件的“扩展名”下的 Windows 资源管理器中有一个空白。
用于转换为 pdf 的代码部分是:

Dim saved_successfull As Boolean
saved_successfull = False
Dim fenetre_saveas As Object
Set fenetre_saveas = Application.FileDialog(msoFileDialogSaveAs)

With fenetre_saveas                     'save as pdf
  .InitialFileName = nom_complet
  .Title = "Confirmer le répertoire où le pdf sera sauvegardé"
  .InitialView = msoFileDialogViewList
  .FilterIndex = 25
  If .Show <> 0 Then
    worddoc.ExportAsFixedFormat _
    OutputFileName:=nom_complet, _
    ExportFormat:=wdExportFormatPDF, Openafterexport:=False
    saved_successfull = True
  End If
End With

If saved_successfull = True Then
    worddoc.Close savechanges:=False                                    'fermeture du doc word
    WordApp.Quit
    MsgBox ("Saved successfull")
    Exit Sub
Else
    MsgBox ("Couldn't convert in pdf")
End If

Exit Sub

所以基本上没有产生错误信息。我收到消息框(“Saved succesfull”),文件以正确的名称保存在所需的路径中。但是文件的扩展名不存在。
所以它是一个pdf,但它不会被自动识别为一个。如果我使用pdf阅读器打开它,它将被阅读。任何想法,我错过了什么?
非常感谢您提前。

标签: excelvbams-wordpdf-generation

解决方案


线

.InitialFileName = nom_complet

包含变量“nom_complet”。该变量需要以“.pdf”结尾。虽然它是 pdf 类型的文件名,但如果在 ".initialFileName" 中作为变量传递,则必须以 ".pdf" 结尾。

好的,这很明显,但也许其他人会遇到这个问题,所以我将答案留在这里。


推荐阅读