Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA在Excel中为Word文档执行宏_Excel_Vba_Ms Word - Fatal编程技术网

VBA在Excel中为Word文档执行宏

VBA在Excel中为Word文档执行宏,excel,vba,ms-word,Excel,Vba,Ms Word,我有一个excel计算,其中包含Word文档的信息。我想要的是打开word文档并将其自动保存为pdf格式——在Excel中使用宏 我已经尝试了以下方法: Set WordApp = CreateObject("Word.Application") With WordApp.Application .Visible = True .Documents.Open (LocationTemplate) .ExportAsFixedFormat OutputFileName:

我有一个excel计算,其中包含Word文档的信息。我想要的是打开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

错在哪里?期待您的支持。

您似乎没有选择打开的文档

试试这样的

Set WordApp = CreateObject("Word.Application")
With WordApp.Application
  .Visible = True
  .Documents.Open (LocationTemplate)

  .Activate

  ActiveDocument.ExportAsFixedFormat 
    .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
好运

运行时错误438:“对象不支持此属性或方法” 您的问题源于这样一个事实,即在
With
块中,您指的是
WordApp.Application
(它本身是冗余的,可以简化为
WordApp
,因为它已经表示
Word.Application
对象),因此使用了行
.ExportAsFixedFormat[…]
您实际上在做:

Word.Application.ExportAsFixedFormat

应用程序
对象上不存在此方法。(现在再次阅读错误描述-注意到了吗?)

如果您不是通过
CreateObject()
延迟绑定
Word.Application
对象,而是设置对Word对象模型的引用(菜单:Extras-References),则可以执行以下操作:

Dim wordApp As Word.Application
Set wordApp = New Word.Application
With wordApp.Documents.Open LocationTemplate
    .ExportAsFixedFormat [...]
End With

当您尝试调用错误的方法时,它为您提供了(急需的)智能感知,以及编译时错误,而不是运行时错误。

错误在于您在Word.Application对象上执行了
.ExportAsFixedFormat
。此方法对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

请告诉我们更多关于你的问题:你有错误吗?它是否以意想不到的方式工作?(我们无法检查您的代码,只要您不向我们提供-因为您的代码段由于缺少声明而无法为我编译。)打开Word文档后,我遇到了运行时错误438。黄色标记从.ExportAsFixedFormat开始。请您将这些信息编辑到您的问题中,我认为这对解决您的问题至关重要。另外,看看我的答案,看看它是否对你有帮助。不幸的是,在你的答案的帮助下,我没有找到最终的解决方案。尽管如此,我仍在努力修复。第二种方法是Word包含一个
AutoOpen
宏,因此可以将文件保存为PDF格式。因此我需要excel计算的路径。是否有可能将路径(可能作为变量)从excel传输到word?使用
AutoOpen
也不会成功。首先需要修复代码中的问题。您的
.ExportAsFixedFormat
方法引用了错误的对象。正如在和回答中提到的。您是否尝试编译/运行您的代码?您的建议将显示与OP的代码片段相同的行为(即错误438)
.ExportAsFixedFormat
WordApp
对象上不存在。是的,我已尝试并工作。在wordapp中不存在,但在activedocument中是。。。这就是你激活文档的原因。我指的是行
ActiveDocument.ExportAsFixedFormat
(应该可以),后面是
。ExportAsFixedFormat OutputFileName:=\u
[…],我很确定这行行不通。(因为它正在调用
Word.Application
上的
ExportAsFixedFormat
)谢谢您的回复。我将
Wordapp
更改为
Word.Application
而不是对象。然而,我得到了一个编译错误,因为
.ExportAsFixedFormat
不存在。是否有可能添加它?通常我的问题不仅与
.ExportAsFixedFormat
有关。我想用Excel中的宏访问word文档。@Luiscrado现在我得到了您想要实现的目标。方法
ExportAsFixedFormat
可用于类型
Word.Document
的每个对象。您不需要使用
ActiveDocument
。(因此无需首先调用
。激活
)。如果像
set doc=.Documents.Open(LocationTemplate)
那样设置要打开的文档的引用,则可以使用
doc.ExportAsFixedFormat