Microsoft Word VBA插入Excel中的页脚 [英] Microsoft Word VBA to insert footer from excel

查看:132
本文介绍了Microsoft Word VBA插入Excel中的页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在尝试从Excel(VBA)在Word文档中插入页脚.我希望页脚是这样的:

Am trying to insert footer in Word Document from Excel (VBA). I want the footer to be like this:

页脚左,右脚: CustomText页面1 of 5

下面是用excel编写的vba代码:

Below is the vba code written in excel:

Sub FooterTextwithpageNum()
    Dim wb As Workbook
    Dim objWord As Object
    Dim FooterTemp As Object
    
    Set objWord = GetObject(, "Word.Application")
    objWord.Visible = True
    Set FooterTemp = objWord.ActiveDocument 
  
    FooterTemp.Sections(1).Footers(1).Range.Text = "This is Custom Text"

    FooterTemp.Sections(1).Footers(1).PageNumbers.Add FirstPage:=True
End Sub

代码执行后得到如下结果:

After the code executes am getting result as below:

页脚左,右脚:这是自定义文本1

不是将页码作为Y的X页,而是将数字1,2等.有人可以帮助我将页码作为Y的X页吗?如下图所示:

Instead of getting page numbers as Page X of Y its just numerals 1,2 etc. Can anybody please help me in getting page numbers as Page X of Y? Like below image:

推荐答案

这是我如何使用Word的内置制表位: .InsertAfter vbTab

Here is how I got it to work using Word's built in tab stops: .InsertAfter vbTab

将其放置两次以使其左&右页脚.

Put it twice to have a left & right footer.

整个Excel VBA页脚代码:

Entire Excel VBA footer code:

    ' FOOTER
    With wrdDoc.sections(1).Footers(wdHeaderFooterPrimary).Range
        .InsertAfter Text:="Printed: "
        .Fields.Add .Characters.Last, wdFieldEmpty, "DATE \@ ""MM/DD//YYYY""", False
        .InsertAfter vbTab
        .InsertAfter vbTab
        .InsertAfter Text:="Page "
        .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="PAGE", PreserveFormatting:=False
        .InsertAfter Text:=" of "
        .Fields.Add Range:=.Characters.Last, Type:=wdFieldEmpty, Text:="NUMPAGES", PreserveFormatting:=False
    End With

结果:

使用Office 2016(Office 365).

Using Office 2016 (Office 365).

这篇关于Microsoft Word VBA插入Excel中的页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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