vba 用于编辑页眉和页脚的 WORD 2010 宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8131219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WORD 2010 Macro for Editing Headers & Footers
提问by Dan
I have only basic VBA experince and my prior Macro experence was primarily with WORD 2003. Recording Macros used to take GoToFooter (or Edit Footer) Menu Commands and allow subsequent editing. In WORD 2010, this (and many other) commands do not "record" to the Macro (yet when in Record mode, I do get into Edit Footer function).
我只有基本的 VBA 经验,我之前的宏经验主要是使用 WORD 2003。录制宏用于获取 GoToFooter(或编辑页脚)菜单命令并允许后续编辑。在 WORD 2010 中,此(以及许多其他)命令不会“记录”到宏(但在记录模式下,我确实进入了“编辑页脚”功能)。
A research of various VBS options shows several ways to create Footers and to make global Footer setting changes within Macro. However If I simply want to Revise the Company name within the Footer (for example), I can find no way to do this within a Macro subroutine.
对各种 VBS 选项的研究显示了创建页脚和在宏中更改全局页脚设置的几种方法。但是,如果我只是想修改页脚中的公司名称(例如),我找不到在宏子例程中执行此操作的方法。
This subroutine is one that I would call from the Main Macro that is stepping through each file in a Folder (& subfolders). I have the main Macro functioning.
这个子例程是我从主宏调用的子例程,它正在单步执行文件夹(和子文件夹)中的每个文件。我有主要的宏功能。
Does WORD 2010 Macro-VBA preclude simple Edit-Footer function?
WORD 2010 Macro-VBA 是否排除了简单的 Edit-Footer 功能?
Thanks in advance
提前致谢
So, thanks to Issun, here is my solution:
所以,感谢 Issun,这是我的解决方案:
`
Sub Sub_FTR_0()
'
ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
For i = 1 To ActiveDocument.Sections.Count
'REM: INSERT Code from RECORD MACRO recorded when editing one Footer correctly
Selection. [[xxx]], etc.
If i = ActiveDocument.Sections.Count Then GoTo Line1
ActiveDocument.ActiveWindow.ActivePane.View.NextHeaderFooter
Line1:
Next
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
`
回答by aevanko
Here is a way you can access the headers/footers via VBA. As you can see, it's rather complicated syntax to get to something so simple :p there
这是一种可以通过 VBA 访问页眉/页脚的方法。如您所见,获得如此简单的东西是相当复杂的语法 :p
Sub EditHeadersAndFooters()
Dim i As Long
For i = 1 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(i)
.Headers(wdHeaderFooterPrimary).Range.Text = "Foo"
.Footers(wdHeaderFooterPrimary).Range.Text = "Bar"
End With
Next
End Sub
Here is a link to example code on how to change the headers in every file in a folder. It takes a different approach and I have never tried it, but for your reference: http://www.vbaexpress.com/kb/getarticle.php?kb_id=45
这是有关如何更改文件夹中每个文件中的标题的示例代码的链接。它采用不同的方法,我从未尝试过,但供您参考:http: //www.vbaexpress.com/kb/getarticle.php?kb_id=45
回答by Gabriela M
This worked for me for all pages in the document.
这对我的文档中的所有页面都有效。
word.ActiveDocument.Sections(1).Headers(1).Range.Text = "Put the header here"
word.ActiveDocument.Sections(1).Headers(1).Range.Text = "把标题放在这里"