VBA - 页脚修改 - 取消链接到以前的页脚



我正在尝试创建一个宏,该宏创建一个具有不同方向和不同制表的新页面。

我有这个代码:

Selection.InsertBreak Type:=wdSectionBreakNextPage
If Selection.PageSetup.Orientation = wdOrientPortrait Then
MsgBox "OK"
Else
Selection.PageSetup.Orientation = wdOrientPortrait
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.HeaderFooter.LinkToPrevious = Not Selection.HeaderFooter. _
LinkToPrevious
Selection.ParagraphFormat.TabStops.ClearAll
Selection.ParagraphFormat.TabStops(CentimetersToPoints(18)).Position = _
CentimetersToPoints(18.5)
Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(9.5), _
Alignment:=wdAlignTabCenter, Leader:=wdTabLeaderSpaces
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End If
Selection.Style = ActiveDocument.Styles("Annexe1")

不幸的是,这会更改之前页面的表格。

有没有办法解决这个问题? 我已经尝试了GoTo功能,带有页面,部分,...但它没有起到作用。

谢谢!

当您在 VBA 代码中使用Selection时,它总是很棘手 - 它并不总是精确地模仿用户执行操作时发生的情况。当使用页眉和页脚

时,这尤其是SeekView

的问题。以下内容适用于Range对象(将范围视为不可见的选择,但您可以在 VBA 代码中有多个范围 - 只能有一个选择(。

它首先将当前所选内容分配给 Range 对象,然后插入分节符并比较页面方向,就像您所做的那样。

如果需要更改页面方向,则会将新节的页脚分配给Footer对象。然后将页脚的Range.ParagraphFormat分配给ParagraphFormat对象*。请注意如何使用这些设置来更改代码中的设置。最大的区别是,代码不会在页脚中放置任何选择:一切都是直接完成的。这不仅更快,更准确,还可以减少屏幕"闪烁"。

最后,选择原始范围(此后已超出原始选择(,因为我假设您希望在此时继续输入文本。如果没有,则可以删除此行。

Sub ChangePageOrientationAndTabs()
Dim ftr As word.HeaderFooter
Dim pf As word.ParagraphFormat
Dim rngSel As word.Range
Set rngSel = Selection.Range
rngSel.InsertBreak Type:=wdSectionBreakNextPage
If rngSel.PageSetup.Orientation = wdOrientPortrait Then
MsgBox "OK"
Else
rngSel.PageSetup.Orientation = wdOrientPortrait
Set ftr = ActiveDocument.Sections(rngSel.Sections(1).index).Footers(wdHeaderFooterPrimary)
ftr.LinkToPrevious = False
Set pf = ftr.Range.ParagraphFormat
pf.TabStops.ClearAll
pf.TabStops(CentimetersToPoints(18)).Position = _
CentimetersToPoints(18.5)
pf.TabStops.Add Position:=CentimetersToPoints(9.5), _
Alignment:=wdAlignTabCenter, Leader:=wdTabLeaderSpaces
End If
rngSel.Paragraphs(1).Range.style = ActiveDocument.styles("Annexe1")
rngSel.Select
End Sub

*这对我来说是一个难题,为什么将 Footer.Range 分配给Range对象适用于上一节的页脚,而页脚和段落格式是正确的......

相关内容

最新更新