Hi MrExcel wizards.
I have been searching for hours trying to find something to solve this specific problem. I have information in an Excel file, which I want to transfer to a word file. I have everything working perfectly. All i now need is to add "Page X of Y" at a specific location.
I have found this code, which works perfectly in word itself, but I cannot get it to work Excel:
I am aware that I need to tweek the code to make Excel understand what to do. So i have tried to do that:
I just want the code to insert "Page X of Y" at the current position of the cursor. I hope someone can help with this.
Best regards,
Morten
I have been searching for hours trying to find something to solve this specific problem. I have information in an Excel file, which I want to transfer to a word file. I have everything working perfectly. All i now need is to add "Page X of Y" at a specific location.
I have found this code, which works perfectly in word itself, but I cannot get it to work Excel:
Code:
Sub pageNumber()
ActiveDocument.Sections(ActiveDocument.Sections.Count) .Headers(wdHeaderFooterPrimary).Range.Select
With Selection
.Paragraphs(1).Alignment = wdAlignParagraphCenter
.TypeText Text:="Page "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= "PAGE ", PreserveFormatting:=True
.TypeText Text:=" of "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= "NUMPAGES ", PreserveFormatting:=True
End With
End Sub
I am aware that I need to tweek the code to make Excel understand what to do. So i have tried to do that:
Code:
Sub main()
Dim objWord As Word.Application
Dim objDoc As Word.Document
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()
objDoc.Activate
objWord.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With objWord.Selection
.TypeText Text:="Page "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="PAGE ", PreserveFormatting:=True
.TypeText Text:=" of "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="NUMPAGES ", PreserveFormatting:=True
End With
End Sub
Best regards,
Morten
Last edited by a moderator: