excel - 从 Excel 更新 MS Word 页脚表中的页码
问题描述
我正在使用 Excel VBA 代码从 excel 更新 Word 文档页脚表格信息。它的工作正常唯一的问题。我无法用 word 更新页码。请参考我正在使用的以下代码。下面也是我在 word 中的页脚表的图像。
使用此代码。此代码将帮助我将一些信息从 excel 更新到 MS word 页脚表。它工作得很好,但我需要你的帮助才能使页码充满活力。
Sub Update_Informe_word_2003()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdRng As Word.Range
Dim j As Integer
Dim datos(0 To 1, 0 To 30) As String '(columna,fila)
Dim ruta As String
Dim rngFooter As Word.Range
Dim tbl As Word.Table
Dim rngCell As Word.Range
Dim FileName As String
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
For i = 2 To Application.WorksheetFunction.CountA(Range("A:A"))
On Error GoTo nx:
If Range("C" & i).Value = "Form (FORM)" Then
logo = Range("s2").Value
ruta = Range("s4").Value & "\Form\Word\" & Range("B" & i).Value & ".doc"
FileName = VBA.FileSystem.Dir(ruta)
If FileName = VBA.Constants.vbNullString Then GoTo nx
Set wdDoc = wdApp.Documents.Open(ruta)
Set rngFooter = wdDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
rngFooter.Delete
With rngFooter
Set tbl = rngFooter.Tables.Add(rngFooter, 1, 3)
' tbl.Select
With tbl.Borders
.OutsideLineStyle = wdLineStyleSingle
End With
Set rngCell = tbl.Cell(1, 3).Range
rngCell.Text = "Doc #: " & Range("e" & i).Value & Chr(10) & "Rev. #: " & Range("H" & i).Value
rngCell.Font.Size = 7
rngCell.Font.Name = "Arial"
rngCell.Paragraphs.Alignment = wdAlignParagraphRight
Set rngCell = tbl.Cell(1, 1).Range
rngCell.Text = "Uncontrolled When Printed" & Chr(10) & "Page 1 of 3"
rngCell.Font.Size = 7
rngCell.Font.Name = "Arial"
Set rngCell = tbl.Cell(1, 2).Range
rngCell.Text = "VECTRUS COMPANY PROPRIETARY" & Chr(10) & "If Client Proprietary, Leave this Blank"
rngCell.Font.Size = 7
rngCell.Font.Name = "Arial"
rngCell.Font.Bold = True
End With
'Set rngheader = wdDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range
'rngheader.Delete
'Set tbl = rngheader.Tables.Add(rngheader, 1, 3)
'Set rngCell = tbl.Cell(1, 1).Range
'With rngCell
'.InlineShapes.AddPicture FileName:=logo, LinkToFile:=False, SaveWithDocument:=True
'End With
Dim FindWord As String
Dim result As String
rngFooter.Find.Execute FindText:="Doc #:", Forward:=True
If rngFooter.Find.Found = True Then rngFooter.Bold = True
Set rngFooter = wdDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
rngFooter.Find.Execute FindText:="Rev. #: ", Forward:=True
If rngFooter.Find.Found = True Then rngFooter.Bold = True
Set rngFooter = wdDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
rngFooter.Find.Execute FindText:="Uncontrolled When Printed", Forward:=True
If rngFooter.Find.Found = True Then rngFooter.Bold = True
Range("M" & i).Value = "Updated"
wdDoc.Save
wdDoc.Close
End If
nx:
Next
Call Update_Informe_Excel_2003
MsgBox ("Files updated")
End Sub
解决方案
既然你"Page 1 of 3"
在页脚打印一个字符串,页码自然不会更新。
当前页码和总页码存储在文档字段中,您可以使用以下代码插入:
Fields.Add oRange, wdFieldEmpty, "PAGE \* Arabic", True
Fields.Add oRange, wdFieldEmpty, "NUMPAGES ", True
在你的情况下,更换
Set rngCell = tbl.Cell(1, 1).Range
rngCell.Text = "Uncontrolled When Printed" & Chr(10) & "Page 1 of 3"
和
Set rngCell = tbl.Cell(1, 1).Range
rngCell.Collapse
rngCell.InsertAfter = "Uncontrolled When Printed" & Chr(10)
rngCell.Collapse 0
wdDoc.Fields.Add rngCell, wdFieldEmpty, "PAGE \* Arabic", True
rngCell.InsertAfter " of "
rngCell.Collapse 0
wdDoc.Fields.Add rngCell, wdFieldEmpty, "NUMPAGES ", True
要更新字段,请使用Ctrl+A
和Shift+F9
/或使用以下 VBA:
Dim oStory
For Each oStory In wdDoc.StoryRanges
oStory.Fields.Update
Next oStory
推荐阅读
- r - 重命名数据框中列中的字符变量 - R
- javascript - ES6 中的破坏和/或质量分配
- unit-testing - 使用更改源触发器对 Azure 函数进行单元测试
- python - 如何将一系列数字传递给函数?Python
- excel - 使用 VBA 比较整个 excel 文档并输出一个新文档,包含汇总/重复项总数
- javascript - 当第二个数组是第一个数组中的键时,根据另一个数组对一个数组进行排序。
- javascript - IOS safari 或 Chrome 通过 USB 读/写文件
- python - python无法导入时区但可以导入日期时间
- user-interface - 仅当鼠标位于 JTextField 中时按下 ENTER 键时才运行特定代码的侦听器?
- documentum - 如何使用 DQL 更新 Documentum 中的锁定记录?