excel - VBA在Excel中为Word文档执行宏
问题描述
我有一个包含 Word 文档信息的 excel 计算。我想要的是打开word文档并将其自动保存为pdf - 在Excel中使用宏。
我已经尝试了以下方法:
Set WordApp = CreateObject("Word.Application")
With WordApp.Application
.Visible = True
.Documents.Open (LocationTemplate)
.ExportAsFixedFormat OutputFileName:= _
OfferPath, _
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
ChangeFileOpenDirectory _
DestinationPath
.Quit
End With
错误是什么?期待您的支持。
解决方案
错误是您.ExportAsFixedFormat
在 Word.Application 对象上执行。此方法对Word 文档有效。您的代码应该更像下面的代码。
请注意,我已经添加了变量声明WordApp
和WordDoc
代码来释放这些对象。
Dim WordApp as Object
Dim WordDoc as Object
Set WordApp = CreateObject("Word.Application")
With WordApp.Application
.Visible = True
Set WordDoc = .Documents.Open (LocationTemplate)
WordDoc.ExportAsFixedFormat OutputFileName:= _
OfferPath, _
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
.Quit 0 'To not save changes to the document, just close it
Set WordDoc = Nothing
End With
Set WordApp = Nothing
推荐阅读
- modelica - Modelica 模型在 OMEdit 中成功模拟,但不是作为 FMU
- c# - 公开我的导出 Excel 方法并从其他类 C# WinForms 调用它
- javascript - 有没有办法从另一个 JS 文件中读取 JS 代码作为 JS 文件的一部分?(该应用程序使用 NodeJS)
- python - Python Webdriver Manager:Python Webdriver Manager 上的 Linux 问题
- reactjs - 询问在 Reactjs 中使用分页和钩子创建复选框
- mainframe - 在 Rexx 中查找第 n 个出现的字符
- android - 如何使用 WindowInsets 启用和禁用全屏模式?
- linux - 在 Linux 上创建一个“~/.aws/credentials”文件
- python-3.x - Selenium python 评分星星点击
- javascript - 条件更改为 true 后,ng-show 不显示