使用VBA在Word文档的页脚中编辑页码

时间:2018-10-22 09:47:16

标签: vba ms-word word-vba page-numbering

我需要将页码从“ 1”更改为“-1-”(不是简称-所以我不能简单地更改页码样式)。如何在VBA中做到这一点?

我尝试了下面的代码,但是第二个-不知何故。

Selection.HomeKey unit:=wdStory
 Selection.GoTo wdGoToPage, wdGoToNext, , "15 "
 If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
 ActiveWindow.Panes(2).Close
 End If
 If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then
 ActiveWindow.ActivePane.View.Type = wdPrintView
 End If
 ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
 If Selection.HeaderFooter.IsHeader = True Then
 ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
 Else
 ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.WholeStory
Selection.Font.Size = 14
Selection.Font.Name = "宋体"
Selection.InsertAfter " —"
Selection.InsertBefore "— "
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Selection.HomeKey unit:=wdStory
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.WholeStory
Selection.Font.Size = 14
Selection.Font.Name = "宋体"
Selection.InsertAfter " —"
Selection.InsertBefore "— "
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

问题中的代码似乎是使用宏记录器生成的。 cross_rows = [] for col in cross_cols: cross_rows.append(pd.concat([pd.crosstab(df[col], df[type_var],margins=True) for type_var in type_cols],axis=1,keys = type_cols,sort=True)) results = pd.concat(cross_rows, keys = cross_cols,sort=True) 不是很可靠或效率很高。通常,最好直接使用Word c d c 0.0 1.0 All 0.0 1.0 All a 1.0 2.0 1.0 3.0 1 0 1 All 2.0 1.0 3.0 2 1 3 0.0 NaN NaN NaN 1 1 2 b 1.0 2.0 1.0 3.0 2 0 2 All 2.0 1.0 3.0 2 1 3 0.0 NaN NaN NaN 0 1 1 对象,而不是SeekView对象。这样更快,更准确,并减少了“屏幕闪烁”。

下面的代码示例使用该原理:它为当前页面的Range获取Selection。在页脚的全部内容之前和之后插入带空格的长破折号。然后将格式应用于此内容。

请注意:对于Word,实际上最好更改页脚样式,如果此格式应在整个文档中应用于页脚。

Range