简体   繁体   English

VBA在Excel中为Word文档执行宏

[英]VBA Execute Macros for Word Document in Excel

I have an excel calculation which contains information for a Word Document. 我有一个Excel计算,其中包含Word文档的信息。 What I want is to open the word document and save it as an pdf automatically - with a macro in Excel. 我想要的是打开Word文档并将其自动另存为pdf-在Excel中使用宏。

I already tried the following: 我已经尝试了以下方法:

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

What is the mistake? 怎么了 Looking forward to your support. 期待您的支持。

it seems that youre not selecting the open document 您似乎没有选择打开的文档

try something like this 尝试这样的事情

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

Good Luck 祝好运

Runtime Error 438: "Object doesn't support this property or method" 运行时错误438:“对象不支持此属性或方法”

Your problem stems from the fact that in the With block you are referring to WordApp.Application (which in itself is redundant and could be reduced to WordApp as it already represents a Word.Application object) and thus with the line .ExportAsFixedFormat [...] you are essentially doing: 您的问题源于以下事实:在With块中,您引用的是WordApp.Application (它本身是多余的,可以简化为WordApp因为它已经表示一个Word.Application对象),因此带有一行.ExportAsFixedFormat [...]实际上是在做:

Word.Application.ExportAsFixedFormat

This method doesn't exist on the Application object. 该方法在Application对象上不存在。 (Now read the error description again - notice anything?) (现在,请再次阅读错误说明-注意吗?)

If you instead of late-binding the Word.Application object via CreateObject() set a reference to the Word Object Model (Menu: Extras - References) you can do things like: 如果您不是通过CreateObject()后期绑定Word.Application对象,而是设置对Word对象模型的引用(菜单:Extras-References),则可以执行以下操作:

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

Which provides you with the (much needed) intellisense, as well as compile-time errors instead of runtime errors when you try to call wrong methods. 当您尝试调用错误的方法时,这为您提供了(非常需要的)智能感知以及编译时错误而不是运行时错误。

The mistake is that you execute .ExportAsFixedFormat on the Word.Application object. 错误是您对Word.Application对象执行.ExportAsFixedFormat This method is valid for a Word document . 此方法对Word文档有效。 Your code should be more like what follows. 您的代码应类似于以下内容。

Note that I've added the variable declarations for WordApp and WordDoc and also code to release these objects. 请注意,我已经为WordAppWordDoc添加了变量声明,并且还添加了用于释放这些对象的代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM