注册 登录
编程论坛 VB6论坛

怎样添加页脚

chen3523 发布于 2019-11-30 16:33, 1861 次点击
向大侠求助:用VB调试word文档,文字修改存盘成功了。现在不懂页脚怎样写程序,问度娘终没正果。
Public Sub Ca涵()
  Set WordApp = CreateObject("Word.Application")
 '   WordApp.Visible = True
  Set Word = WordApp.Documents.Open(App.Path & "\存放夹\分部_涵洞.doc")
    Word.Tables(1).Cell(2, 1) = Str项目名
    Word.Tables(1).Cell(4, 1) = Str分部名 & "分部工程验收报告"
    Word.Tables(1).Cell(5, 1) = Str简 & "[" & Year年 & "]分验" & Num分部序号 & "号"
    Word.Tables(1).Cell(10, 2) = Str单项名
    Word.Tables(1).Cell(11, 2) = Str单位名
    Word.Tables(1).Cell(12, 2) = Str分部名
    Word.Tables(1).Cell(14, 1) = Str分部名 & "分部工程验收组"
    Word.Tables(1).Cell(15, 1) = Ymd年月日


Dim r As Range
'给文档的第一节页脚添加页码
Set r = Word.Sections(1).Footers.Range     '运行显示“实时错误'438'对象不支持该属性或方法”
With r
    .InsertAfter "第"
    .Font.Size = 20
    .Collapse Direction:=wdCollapseEnd
    '插入页码域
    .Fields.Add Range:=r, Type:=wdFieldEmpty, Text:= _
        "PAGE  \* Arabic ", PreserveFormatting:=True
    .Expand unit:=wdWord
    .InsertAfter "页 "
     
    .InsertAfter "共"
     .Collapse Direction:=wdCollapseEnd
     '插入页数域
    .Fields.Add Range:=r, Type:=wdFieldEmpty, Text:= _
        "NUMPAGES  \* Arabic ", PreserveFormatting:=True
    .Expand unit:=wdWord
    .InsertAfter "页"
 
    .InsertAfter "'" & Str分部名 & "_" & Str简 & "  "
    .ParagraphFormat.Alignment = wdAlignParagraphRight
End With
'隐藏页眉的横线
Word.Sections(1).Headers(wdHeaderFooterPrimary).Range.Borders(wdBorderBottom).Visible = False

    Word.SaveAs (Print打印夹 & "\分部工程\涵分部\涵洞(" & "_" & Str分部名 & "_" & Str简 & ").doc")      '另存  
    Word.Close
    WordApp.Quit
    Set Word = Nothing
    Set WordApp = Nothing
End Sub
2 回复
#2
chen35232019-12-01 09:17
希望得到版主的指教
#3
chen35232019-12-19 22:58
我自己玩了几次,借助VBA终于完成任务,发上群,供大家学习
Public Sub Ca涵()
  Set WordApp = CreateObject("Word.Application")
 '   WordApp.Visible = True
  Set Word = WordApp.Documents.Open(App.Path & "\存放夹\分部_涵洞.doc")
    Word.Tables(1).Cell(2, 1) = Str项目名
    Word.Tables(1).Cell(4, 1) = Str分部名 & "分部工程验收报告"
    Word.Tables(1).Cell(5, 1) = Str简 & "[" & Year年 & "]分验" & Num分部序号 & "号"
    Word.Tables(1).Cell(10, 2) = Str单项名
    Word.Tables(1).Cell(11, 2) = Str单位名
    Word.Tables(1).Cell(12, 2) = Str分部名
    Word.Tables(1).Cell(14, 1) = Str分部名 & "分部工程验收组"
    Word.Tables(1).Cell(15, 1) = Ymd年月日

   Word.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = Str分部名 & "工程验收"          '设置页眉
    Word.Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers.Add                      '设置页脚
    Word.SaveAs (Print打印夹 & "\分部工程\涵分部\涵洞(" & "_" & Str分部名 & "_" & Str简 & ").doc")      '另存  
    Word.Close
    WordApp.Quit
    Set Word = Nothing
    Set WordApp = Nothing
End Sub
1