【问题标题】:Creating a pdf from a Word with excel: document.exportasfixedformat not creating the extension of the file使用 excel 从 Word 创建 pdf:document.exportasfixedformat 未创建文件的扩展名
【发布时间】:2019-12-26 20:22:58
【问题描述】:

我正在使用 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阅读器打开它,它将被阅读。 任何想法,我错过了什么?
非常感谢您。

【问题讨论】:

    标签: excel vba ms-word pdf-generation


    【解决方案1】:

    线

    .InitialFileName = nom_complet

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

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

    【讨论】:

      【解决方案2】:

      将 .pdf 作为文件名的后缀添加到语音标记中,即 -

      输出文件名:="nom_complet.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.pdf", _
          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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多