0

I need to change page number from "1" to "—— 1 ——" (Not the short - so I can't simple change the page number style). How can I do this in VBA?

I tried the following code but the second —— is out of position somehow.

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

braX
  • 11,506
  • 5
  • 20
  • 33
WTFIsGoingOn
  • 347
  • 1
  • 3
  • 7

1 Answers1

0

It appears the code in the question was generated using the macro recorder. SeekView is not very reliable or efficient. Generally, it's better to work directly with a Word Range object, rather than Selection. This is faster, more accurate and reduces "screen flicker".

The code sample below uses that principle: it gets the Range for the current page's Footer. The long dash character with spaces is inserted before and after the entire content of the footer. The formatting is then applied to this content.

Note that for Word it would actually be better to change the Footer style if this formatting should be applied to the footer throughout the document.

Sub InsertContentBeforeAndAfterPageNumber()
    Dim rngFooter As Word.Range
    Dim sCharToInsert As String

    Selection.GoTo wdGoToPage, wdGoToNext, , "15"
    sCharToInsert = "—"
    Set rngFooter = Selection.Sections(1).Footers(wdHeaderFooterPrimary).Range
    With rngFooter
      .InsertBefore sCharToInsert & " "
      .InsertAfter " " & sCharToInsert
      .Font.Size = 14
      .Font.Name = "宋体"
    End With
End Sub
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • I don't know VBA. The code I posted was found online. Thanks for your code. But it seems to archive the same result? It also adds a horizontal line in the header for some reason. – WTFIsGoingOn Oct 23 '18 at 00:40
  • I see no horizontal line when I run the code in the Answer. The code assumes a page number is already present in the Footer, analog to the code you posted. We can only work with the information you provide! Please note that Stack Overflow targets professional and enthusiast *programmers* -- a basic knowledge is assumed. This is not a free code-writing nor a tutorial service. – Cindy Meister Oct 23 '18 at 04:45
  • Selection.GoTo wdGoToPage, wdGoToNext, , "15" not use – JasonYun Jan 09 '20 at 13:37