#1
|
|||
|
|||
Macro to Add Text and Page Number to Top of Each Page Within Text
I need to insert a bit of text and the page number into the body of the document at the exact spot where the top of each page starts (in the body, not the header), so it would look like **P[page number]. I need the macro to start at the last page and work towards the first page, so that the newly added text does not change where the original page number would have started. When it is done, I want a document that contains these textual page notations, even if the macro shifted the page numbering when it added the new text. I do not care if the newly added text splits a word. Any ideas? Thanks!
|
#2
|
||||
|
||||
Is there a reason you can't simply insert the required text, plus a PAGE field into the page header or footer?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
I certainly could do it manualy, but I have about 8,000 pages to do, with another 500 pages twice a year, and was hoping to automate the process.
|
#4
|
|||
|
|||
Oh, and the final document can't have a header or footer.
|
#5
|
||||
|
||||
Surprise yourself - access the page header and insert the text and a PAGE field (or create one by pressing Ctrl-F9 and typing PAGE between the field braces, then select the field and press F9. Then go back to the body of the document and have a look at what's in the header on a couple of pages. Go to the end of the document and add another page. Take a look at the page header.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
After the page notations are added, it's going to be converted into a text document, and the reader has to be able to see where the page numbers belong, so I need then to be in the body rather than the header.
|
#7
|
||||
|
||||
For what purpose cannot a document have a header or footer?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Quote:
|
#9
|
|||
|
|||
. I need to convert 8,000 pages of word docs into txt files for archival purposes. I need the reader of the txt file to be able to determine where the original page numbers belong.
|
#10
|
||||
|
||||
Try the following macro:
Code:
Sub ApplyPageNos() Application.ScreenUpdating = False Dim Rng As Range, i As Long, StrTxt As String StrTxt = "Page: " With ActiveDocument .Fields.Unlink For i = (.ComputeStatistics(wdStatisticPages) - 1) To 1 Step -1 Set Rng = .GoTo(What:=wdGoToPage, Name:=i) Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page") With Rng .End = .End - 1 If .Characters.Last.Text <> Chr(13) Then .Characters.Last.InsertAfter vbCr End If .InsertAfter vbCr & StrTxt & i + 1 End With Next .Range.InsertBefore vbCr & StrTxt & "1" End With Set Rng = Nothing Application.ScreenUpdating = True End Sub In reality, probably the safest way to do what you're after would be to convert the documents, with page #s in the headers, to PDF, then extract the content from there.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
Thanks
Thanks, I appreciate it. I will play around with that and see what works best.
|
Tags |
macro, page numbering |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
2 page document printing problem, text from page 1 in layout of page 2 when printed | laurawether45 | Word | 1 | 08-02-2012 07:03 AM |
Page number Macro | kimsi | Word | 3 | 11-15-2011 11:54 PM |
How to Restore Page Margin Page Number Styles in Word 2010 | cheech1981 | Word | 5 | 11-15-2011 04:16 AM |
Add text to page numbers | alpruett | Word | 0 | 07-07-2010 09:40 AM |
One Page With Dynamic PAge Number | 0x7C4 | Word | 0 | 08-17-2009 11:03 AM |