2015-04-30 84 views
1

我已经得到了插入下面的代码,自定义页脚word文档如何使用VBA增加MS Word页脚中的值?

Sub addfootaspage() 
Dim i As Integer 
k = 1 
With ActiveDocument.Sections(1) 
.Footers(wdHeaderFooterPrimary).Range.Text = "<a id=""page_" & k & """/>" 
    End With 
End Sub 

在但这代码插入的所有页面的页脚(<a id="page_1"/>)。

如何增加每个页面的值。如<a id="page_1"/>, <a id="page_2"/>...<a id="page_N"/>

回答

0

你忘了增加k。试试这个:

Sub addfootaspage() 
    Dim i As Integer 
    k = 1 
    With ActiveDocument.Sections(1) 
    .Footers(wdHeaderFooterPrimary).Range.Text = "<a id=""page_" & k & """/>" 
    k = k + 1 
    End With 
End Sub 
+0

它在所有页面中插入相同的值。 – Pandu

0

尝试使用循环

for k = 1 to docWord.ComputeStatistics(wdStatisticPages) 
    With ActiveDocument.Sections(1) 
     .Footers(wdHeaderFooterPrimary).Range.Text = "<a id=""page_" & k & """/>" 
    End With 
next 
+0

它在所有页面中插入相同的值。 – Pandu

+0

以外的问题,我有一个更多的疑问,如何找到页码,并用脚注区域中的一些文本替换? – Pandu

+0

是的,我可以看到,Word不喜欢这样的事情。会猜到它会更容易。但我会继续看看需要做什么 – Sam

1

IMO做,页脚应该是相同的每一页,除非你告诉它比早先的页面页脚不同。另外,字段等“智能”内容可能会在每个页面上发生变化。

我建议尝试添加页码字段,并观察它是如何工作的。它应该自动计数。

我用Word记录了一个宏如何在页脚中插入页面字段。我得到这个:

' Select the footer 
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Select 

' Insert an field that shows the page number 
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _ 
    "PAGE \* Arabic ", PreserveFormatting:=True 

' Leave footer (needs print layout) 
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 

这在我的Word中工作...(2013)像这样,它自动计算在页脚。

+0

谢谢。它插入页码,已经有它插入页码。 – Pandu

+0

我如何找到页脚页码([0-9] +),并用word中的取代 – Pandu

+0

我真的相信,基本上,您无法在每个页面上手动输入不同的页脚。如果您需要在每个页面上设置不同的页脚,请在每页的末尾设置分节符。然后,您将可以使用您的第一个代码,因为每个页脚上都可以看到递增的“k”。 –