使用VBA在Word中添加页码 [英] Add page number in Word using VBA

查看:2494
本文介绍了使用VBA在Word中添加页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在找几个小时来做​​一件最简单的事情(但是对于MS来说却从来都不是一件简单的事情……):如何使用VBA以编程方式添加到我的Word页脚页面#"中?互联网上有无数种不同的方式,但没有一种在起作用.只是几个例子

此代码在Fields.Add处失败:

I am looking for hours for one of the simplest things to do (but with MS things are never simple...): How can I programmatically add in my Word footer 'Page #', using VBA ?
There are zillions of different ways on the internet but none is working. Just a couple of examples

This code fails at Fields.Add:

Sub pageNumber()
    ActiveDocument.Sections(ActiveDocument.Sections.Count) _
        .Headers(wdHeaderFooterPrimary).Range.Select
    With Selection
        .Paragraphs(1).Alignment = wdAlignParagraphCenter
        .TypeText Text:="Page "
        .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "PAGE ", PreserveFormatting:=True
        .TypeText Text:=" of "
        .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "NUMPAGES ", PreserveFormatting:=True
    End With
End Sub

此代码不允许我在前面添加"page"之类的字词

This code doesn't allow me to add a word like 'page' before:

With ActiveDocument.Sections(1) 
 .Footers(wdHeaderFooterPrimary).PageNumbers.Add _ 
 PageNumberAlignment:=wdAlignPageNumberLeft, _ 
 FirstPage:=True 
End With

还有其他提示吗?
谢谢.

Any additional hint ?
Thanks.

推荐答案

好的,下面的代码终于可以工作了:

OK, the following code finally works:

With objWord.ActiveDocument.Sections(Section)
    .Footers(wdHeaderFooterPrimary).Range.Text = vbTab & "Page "
    .Footers(wdHeaderFooterPrimary).PageNumbers.Add FirstPage:=True
End With

这篇关于使用VBA在Word中添加页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆