【问题标题】:Add footers in Word From Excel VBA从 Excel VBA 在 Word 中添加页脚
【发布时间】:2020-12-24 04:46:25
【问题描述】:

我正在尝试使用 Vba 在 Excel 工作簿中的 Word 文件中插入一些文本。这是我拥有的代码的一部分:

        fileRow = Range("AE101").Value
        fileLocation = Range("AE" & filaActa).Value         
        Set WordDoc = CreateObject("Word.Application")
        WordDoc.DisplayAlerts = False
        WordDoc.Visible = True
        Set WordApp = WordDoc.Documents.Open(Filename:=File, ReadOnly:=False)

这是我尝试的最后一个代码,但我不断收到相同的错误:

With WordApp.Sections(1).Footers(wdHeaderFooterPrimary).Range
        .InsertAfter Text:="Printed: "
        .Fields.Add .Characters.Last, wdFieldEmpty, "This is a Footer", False
        .InsertAfter vbTab
        .InsertAfter vbTab
        .InsertAfter Text:="Page "
        .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="PAGE", PreserveFormatting:=False
        .InsertAfter Text:=" of "
        .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="NUMPAGES", PreserveFormatting:=False
End With

当我运行它时,我得到了错误; "运行时错误 '5941' 请求的集合成员不存在"

我已经尝试在这两个解决方案中做同样的事情:

Microsoft Word VBA to insert footer from excel

Find and Replace footer text in Word with an Excel VBA Macro

但我找不到问题所在。我能做些什么?我希望你能帮助我。

【问题讨论】:

    标签: excel vba ms-word header footer


    【解决方案1】:

    由于您没有在代码中显示变量声明,因此不清楚您使用的是早期绑定还是后期绑定。鉴于您链接到的示例,我猜您正在使用后期绑定。

    使用后期绑定时,您不能使用 Word 对象库中的常量,即 wdHeaderFooterPrimarywdFieldEmpty,因为 Excel 不知道它们代表什么。您要么必须自己声明这些常量,要么使用它们的基础值。这很可能是导致您的错误的原因。

    您可以通过在代码模块顶部添加Option Explicit 来避免这些错误。当您有未声明的变量时,这将阻止您的代码编译。要自动添加它,请打开 VBE 并转到工具 |选项。在“选项”对话框中,确保选中“要求变量声明”。

    后期绑定也意味着您错误地使用了对象变量,因为您设置了WordDoc = ApplicationWordApp = Document。所以你的第一段代码应该是:

        fileRow = Range("AE101").Value
        fileLocation = Range("AE" & filaActa).Value         
        Set WordApp = CreateObject("Word.Application")
        WordApp.DisplayAlerts = False
        WordApp.Visible = True
        Set WordDoc = WordApp.Documents.Open(Filename:=File, ReadOnly:=False)
    

    正如 Charles 在插入字段时指出的那样,您需要使用有效的字段代码。借用您链接到的第一个示例,您添加页脚的代码需要:

    Const wdHeaderFooterPrimary as Long = 1
    Const wdFieldEmpty as Long = -1     
    With WordDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
        .InsertAfter Text:="Printed: "
        .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="DATE \@ ""MM/DD//YYYY""", PreserveFormatting:=False
        .InsertAfter vbTab
        .InsertAfter vbTab
        .InsertAfter Text:="Page "
        .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="PAGE", PreserveFormatting:=False
        .InsertAfter Text:=" of "
        .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="NUMPAGES", PreserveFormatting:=False
    End With
    

    【讨论】:

      【解决方案2】:

      出于某种原因,您正在插入一个字段并将该字段命名为“这是页脚”。这不起作用并生成错误消息。 This is what the field codes look like and the first one is not a field and will generate an error.

      With WordApp.Sections(1).Footers(wdHeaderFooterPrimary).Range
          .InsertAfter Text:="Printed: "
          .Fields.Add .Characters.Last, wdFieldEmpty, "PrintDate", False
          .InsertAfter vbTab
          .InsertAfter vbTab
          .InsertAfter Text:="Page "
          .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="PAGE", PreserveFormatting:=False
          .InsertAfter Text:=" of "
          .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="NUMPAGES", PreserveFormatting:=False
      End With
      

      您可能需要一个简单的日期字段,而不是 PrintDate,因为 PrintDate 将在文档打印之后才有效。日期字段通常会在打印前更新。请参阅Using Date Fields in Microsoft Word,了解有关可用的各种日期字段以及如何格式化它们的更多信息。

      如果这是您正在创建的新文档,最好使用已包含所需页脚的模板来完成。

      其他选项包括在模板中包含自动图文集或页脚构建块并调用它。 Using vba to insert a building block.

      【讨论】:

      • 如果您阅读问题中的代码,您会看到WordApp 实际上指向一个文档。所以WordApp.ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range会报错。
      • 谢谢蒂莫西,你是对的。我正在修改回复,以免误导其他人。
      猜你喜欢
      • 2017-06-12
      • 1970-01-01
      • 2019-06-04
      • 2019-01-07
      • 2018-08-17
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      相关资源
      最近更新 更多