我有一份自动生成的 PDF 报告。最终,我需要将其保存在 Excel 中,但如果我导出到 Excel,我会丢失包含我需要的数据的页眉/页脚。我可以导出到 Word,它会保留标题,但我需要从标题中取出文本,以便将其保存为纯文本文件,以便在 Excel 中访问。
每页都有一个页眉。有些页面的页脚中还包含数据。我正在寻找将页眉放在每个页面正文顶部的东西,如果页脚中有任何内容,则将页脚中的文本放在底部。
页眉和页脚中的文本包含在看似随机命名的形状中。如果页脚包含任何文本,每个页眉有三个形状,每个页脚有三个形状。
我还没有构建循环或尝试处理页脚,因为我还无法以可用的方式从页眉中获取文本。
我的大部分尝试都是从以下开始的:
activedocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Copy
并在以下某些迭代中以错误/失败结束:
activedocument.Sections(1).Range.PasteSpecial datatype:=wdPasteText
这将返回运行时错误“4198”:命令失败。其他数据类型返回转换为纯文本文件时丢失的文本框。常规粘贴会将文本放回到标题中。
我对 Word 设置中的 VBA 不太熟悉,因此非常感谢任何建议。谢谢!
section.Headers(wdHeaderFooterPrimary).Shapes
是 BOTH headers and footers
中形状的集合,尽管 Headers()
在代码中。section.Headers(wdHeaderFooterPrimary).Range.ShapeRange
。Option Explicit
' This is Word VBA code
Sub ExtractHeaderFooter()
Dim doc As Document
Dim section As section
Dim oShape As shape
Dim text As String, i As Long
Set doc = ActiveDocument
For Each section In doc.Sections
i = 1
For Each oShape In section.Headers(wdHeaderFooterPrimary).Range.ShapeRange
text = oShape.TextFrame.TextRange.text
Debug.Print "Header Shape " & i & " : " & text
Debug.Print oShape.Top, oShape.Left
i = i + 1
Next oShape
i = 1
For Each oShape In section.Footers(wdHeaderFooterPrimary).Range.ShapeRange
text = oShape.TextFrame.TextRange.text
Debug.Print "Fooer Shape " & i & " : " & text
Debug.Print oShape.Top, oShape.Left
i = i + 1
Next oShape
Next section
End Sub