我已经解决了一些事情。它比我想象的要大。我确信它可以帮助您开始实现您想要达到的目标。
Experts-exchange.com 的解决方案提供了一些帮助
”VBA 在 Word 页脚中插入修改后的第 x 页(共 y 页) https://www.experts-exchange.com/questions/23467589/VBA-to-insert-a-modified-Page-x-of-y-in-a-Word-Footer.html#discussion“。我在代码中提到过它,我用它来将测试转换为字段。
正如您在其他问题中提到的“如何在不影响页脚/页眉的情况下启用页码 https://stackoverflow.com/a/45353009/1306012“我遵循使用带有空边框的表格的方法。它们允许您非常精确地放置内容。
这就是为什么下面的代码将插入一个包含三列的表格:
___________________ ________________________ ___________
|_Your footer text__|_Center part if needed__|_Page X/Y__|
下面找到代码。主要方法InsertFooter
你会想从你的代码中调用。它会做你想做的事:
Sub InsertFooter()
Dim footer As HeaderFooter
Dim footerRange As range
Dim documentSection As Section
Dim currentView As View
Dim footerTable As table
Dim pictureShape As Shape
On Error GoTo MyExit
' Disable updating to prevent flickering
Application.ScreenUpdating = False
For Each documentSection In ActiveDocument.Sections
For Each footer In documentSection.Footers
If footer.Index = wdHeaderFooterPrimary Then
Set footerRange = footer.range
' add table to footer
Set footerTable = AddTableToFooter(footerRange)
' Make table border transparent
SetTableTransparentBorder footerTable
' Insert page X out of Y into third column in table
InsertPageNumbersIntoTable footerTable
' Insert file path
InsertFilePathIntoTable footerTable
' Add picture to footer
AddPictureToFooter footerRange, "C:\Pictures\happy.jpg", 3
End If
Next footer
Next documentSection
MyExit:
' Enable updating again
Application.ScreenUpdating = True
Application.ScreenRefresh
End Sub
Sub AddPictureToFooter(range As range, filePath As String, pictureHeightInCm As Single)
Set pictureShape = range.InlineShapes.AddPicture(FileName:=filePath, LinkToFile:=False, SaveWithDocument:=True).ConvertToShape
pictureShape.WrapFormat.Type = wdWrapFront
pictureShape.height = CentimetersToPoints(pictureHeightInCm)
pictureShape.Top = 0
End Sub
Sub InsertPageNumbersIntoTable(tableToChange As table)
' Attention no error handling done!
' inserts "Page {page} of {pages}" into the third column of a table
Dim cellRange As range
Set cellRange = tableToChange.Cell(1, 3).range
cellRange.InsertAfter "Page { PAGE } of { NUMPAGES }"
TextToFields cellRange
End Sub
' Credits go to
' https://www.experts-exchange.com/questions/23467589/VBA-to-insert-a-modified-Page-x-of-y-in-a-Word-Footer.html#discussion
Sub TextToFields(rng1 As range)
Dim c As range
Dim fld As Field
Dim f As Integer
Dim rng2 As range
Dim lFldStarts() As Long
Set rng2 = rng1.Duplicate
rng1.Document.ActiveWindow.View.ShowFieldCodes = True
For Each c In rng1.Characters
DoEvents
Select Case c.Text
Case "{"
ReDim Preserve lFldStarts(f)
lFldStarts(f) = c.Start
f = f + 1
Case "}"
f = f - 1
If f = 0 Then
rng2.Start = lFldStarts(f)
rng2.End = c.End
rng2.Characters.Last.Delete '{
rng2.Characters.First.Delete '}
Set fld = rng2.Fields.Add(rng2, , , False)
Set rng2 = fld.Code
TextToFields fld.Code
End If
Case Else
End Select
Next c
rng2.Expand wdStory
rng2.Fields.Update
rng1.Document.ActiveWindow.View.ShowFieldCodes = False
End Sub
Sub InsertFilePathIntoTable(tableToChange As table)
' Attention no error handling done!
' inserts "Page {page} of {pages}" into the third column of a table
Dim cellRange As range
Set cellRange = tableToChange.Cell(1, 1).range
cellRange.InsertAfter "{ FILENAME \p }"
TextToFields cellRange
End Sub
Sub SetTableTransparentBorder(tableToChange As table)
tableToChange.Borders(wdBorderTop).LineStyle = wdLineStyleNone
tableToChange.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
tableToChange.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
tableToChange.Borders(wdBorderRight).LineStyle = wdLineStyleNone
tableToChange.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
tableToChange.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
tableToChange.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
End Sub
Function AddTableToFooter(footerRange As range) As table
Dim footerTable As table
Set footerTable = ActiveDocument.Tables.Add(range:=footerRange, NumRows:=1, NumColumns:=3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed)
' Algin third column to right
footerTable.Cell(1, 3).range.ParagraphFormat.Alignment = wdAlignParagraphRight
Set AddTableToFooter = footerTable
End Function