WORD 2010宏编辑页眉和页脚

7

我的VBA经验很基础,之前主要使用的是WORD 2003的宏经验。录制宏通常使用GoToFooter(或Edit Footer)菜单命令,并允许随后进行编辑。在WORD 2010中,这个(和许多其他)命令不会“记录”到宏中(但是在记录模式下,我确实可以进入Edit Footer函数)。

各种VBS选项的研究显示了几种方法来创建页脚以及在宏中进行全局页脚设置更改。然而,如果我只想在页脚中修改公司名称(例如),我找不到在宏子程序中完成此操作的方法。

这个子程序是我从主要宏中调用的,主要宏正在逐个文件夹(包括子文件夹)中进行步骤。我的主要宏功能正常。

WORD 2010的宏-VBA是否排除了简单的Edit-Footer功能?

提前感谢。

因此,在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
`
2个回答

10

这是一种通过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

这里有一个链接,可以展示如何更改文件夹中每个文件的标题。它采用了不同的方法,我没有尝试过,但供您参考:http://www.vbaexpress.com/kb/getarticle.php?kb_id=45


谢谢! 我看到了这个语法,并认为它基本上完全用“FUBAR”材料[:)]替换了页眉和/或页脚...仍然不允许我在页脚内选择我的编辑(比如替换前三个单词或最后一个单词等)。 因此,每个文件都有一个独特的页码方案(非字段编码),因此完全替换不是我设想的选项,我已经想出了一个解决方法。 - Dan
谢谢(再次)。您的建议和提供的链接的结合使我得以修复。这是一个“简单”的例程,允许我进入页脚并编辑部分。子程序Sub_FTR_0() ' ' Sub_FTR_0 Macro ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter For i = 1 To ActiveDocument.Sections.Count '(REM) Add: FOOTER EDIT Selection.[cmds] If i = ActiveDocument.Sections.Count Then GoTo Line1 ActiveDocument.ActiveWindow.ActivePane.View.NextHeaderFooter Line1: Next ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument End Sub - Dan
Sub Sub_FTR_0() ' ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooterFor i = 1 To ActiveDocument.Sections.Count'(REM) 添加:页脚编辑 Selection.[cmds]If i = ActiveDocument.Sections.Count Then GoTo Line1ActiveDocument.ActiveWindow.ActivePane.View.NextHeaderFooterLine1: NextActiveWindow.ActivePane.View.SeekView = wdSeekMainDocumentEnd Sub - Dan
很高兴能帮到你。请在你的问题中发布新代码,这样其他用户就可以看到你的解决方案。如果我的答案有效,请点击左上角的勾号“接受”它。 :) - Gaijinhunter
1
如何设置对齐方式? - Md. Nashir Uddin

1
这对于文档中的所有页面都适用。
word.ActiveDocument.Sections(1).Headers(1).Range.Text = "在此处放置页眉"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接