【问题标题】:SaveAs2 dialog for Word does not work (wrong file format)Word 的 SaveAs2 对话框不起作用(文件格式错误)
【发布时间】:2019-04-21 18:23:35
【问题描述】:

我在 Excel 中有一个带有 VBA 代码的 Excel 链接 Word 文档。在代码序列的最后,我想将 Word 文档保存在指定的文件夹中,文件格式为“Word 文档”(又名 doc 或 docx),然后保存为 PDF 文档。

我试图通过这样做,但我得到的输出文件没有“任何”格式。在文件名中指定文件格式也无济于事。

此外,尝试将文件导出为 PDF 也不起作用(过程无效...)

有什么建议吗?

最好, 弗兰齐

Sub WordErstellen(rechnungsnummer As Variant, firma As Variant, name As     Variant, datum As Variant)

Dim WordApp As Object
Dim dateiname, pfad As Variant

path_word = "C:\mypath\Word\"
path_pdf = "C:\mypath\PDF\"

myfilename = rechnungsnummer & "_" & firma & "_" & name & "_" & datum

Set WordApp = CreateObject("Word.Application")
Set doc = WordApp.Documents.Add("C:\mypath\mytemplate.docm")
WordApp.Visible = True

doc.Activate

'Save

doc.SaveAs2 Filename:=path_word & myfilename, FileFormat:=wdFormatDocument, _
LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=15

doc.ExportAsFixedFormat OutputFileName:=path_pdf & myfilename, _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False


End Sub

我试图通过这样做,但我得到的输出文件没有“任何”格式。在文件名中指定文件格式也无济于事。

此外,尝试将文件导出为 PDF 也不起作用(过程无效...)

【问题讨论】:

  • 如果你是后期绑定,那么你必须定义常量wdExportFormatPDFwdExportAllDocumentwdExportDocumentContentwdExportCreateNoBookmarks。打开 MS Word 并在 VBE 中的即时窗口中,键入例如 ?wdExportFormatPDF 以获取它的值。为他人做同样的事情
  • 例如wdExportFormatPDF的值为17。所以你在最顶部声明​​Const wdExportFormatPDF As Integer = 17。对别人做同样的事情。在最顶部添加Option Explicit ;)
  • 注意:在代码模块顶部使用 Option Explicit 会通过引发编译时错误来提醒您注意此类问题(未定义变量)。

标签: excel vba ms-word


【解决方案1】:

试试:

doc.SaveAs2 path_word & myfilename & ".docx", 12, False, "", True
doc.SaveAs2 path_word & myfilename & ".pdf", 17, False, "", True

PS:你不需要:

doc.Activate

【讨论】:

    猜你喜欢
    • 2016-11-25
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多