Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting pages in word document

Status
Not open for further replies.

kalle82

Technical User
Apr 2, 2009
163
SE
Hi!

Question 1: Is there a way to count the number of pages in a word document?

Question 2: The number i get from the counting of pages can i use that to put a text on that page´s header?

So like this..
Start counting... Document has 4 pages.
Add correct header to correct page!
Add the text "Beslut" to First page header
Add the text "Beslut" to Second page header
Add the text "Bilaga 1" to Third page header
Add the text "Bilaga 2" to Fourth page header

I have some problems constructing this funtcion..
But i believe that i must create a variable containing the pagenumber... Then i need to loop through every page to add the new header...
 
Okay ive figured a bit out..

I already placed a bookmark in the template header which gets used with no problem. But when i dynamicly try to update it with info sent from each function i get an error message.

This wont work, It says that there is no such bookmark but it sure is?!?

This is the code im using

ActiveDocument.Sections (ActiveDocument.Sections.Count).Headers(1).Range.Bookmarks(bokmark2).Range.Text = "Förfrågan500
 
i get an error message
Which error message ?
Which line of code highlighted when in debug mode ?

Anyway, I'd replace this:
Bookmarks(bokmark2).
with this:
Bookmarks([!]"[/!]bokmark2[!]"[/!]).

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Okay everything "seems" to work when im using this code, no error message, but nothing happens no text gets added? How can i control this code so that i can control whats being typed in the different header of the pages?

ActiveDocument.Sections(ActiveDocument.Sections.Count).Headers(1).Range.Bookmarks.Add ("bokmarket")

ActiveDocument.Sections(ActiveDocument.Sections.Count).Headers(1).Range.Bookmarks("bokmarket").Range.Text = "Förfrågan500"
 
Okay new problem..

So im trying to add different header on each page, using this code.

But its like page 2-4 have the same header... How can i do this?

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
'First page is always correct so to page 2
ActiveWindow.ActivePane.View.NextHeaderFooter
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="Beslut"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=TextBox6
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=TextBox9
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=TextBox3
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
'No problem at all, moving to page 3, Cant move to page 3??

'besvärshänvisningen
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
ActiveWindow.ActivePane.View.NextHeaderFooter
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="Bilaga 1"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=TextBox6
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=TextBox9
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=TextBox3
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
'cant move to page 4 either its like page 2-4 are the same header..
If CheckBox4.Value = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
ActiveWindow.ActivePane.View.NextHeaderFooter
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:="Bilaga 2"
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=TextBox6
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=TextBox9
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=TextBox3
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Else
End If

End Sub
 
Ive found out that i need to divide the document into sections, to be able to change all the headers as i want...

Ive added sections to the diffrent builds

Selection.TypeText Text:=Chr$(167)

But i want to build my section setup along with my building of the document.

How do i build a section into my document?
 
Use either the Sections.Add or the InsertBreak methods.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi kalle82,

You don't need vba for this!

A 'Next Page' Section break, followed by:
. unlinking the 2nd Section's header from the 1st Section's header
. adding a simple PAGE field in the 2nd Section's header (prefixed by 'Bilaga '; and
. adding 'Beslut' to the 1st Section's header,
is all you need.

Alternatively, you could use a formula field in the header coded as:
{={PAGE}-2 \# "'Bilaga '0;'Beslut';'Beslut'"}

Note: The field brace pairs (ie '{ }') for the above example are created via Ctrl-F9 - you can't simply type them or copy & paste them from this message.

Cheers


[MS MVP - Word]
 
Hmm seems to work fine but i got stuck. I clean the document between the runs, how do i search for and replace wdSectionBreakNextPage what sthe wildcard for wdSectionBreakNextPage ive tried "^m" and "^l"... does not work.. :(


With Selection.Find
.Text = "wdSectionBreakNextPage"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With

Thanks for the answer macro!!
 
Have you tried ^b ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I ahve three diffrent builds is there anyway i can get different formulas for different builds?
 
I cant get it to work it says syntax error?

={PAGE}-2 \# "'Bilaga '0;'Beslut';'Beslut'
 
This was helluva problem...!!!!!! I really cant get it to work...

I have three diffrent sets of bookmarks i place in the document..

Im thinking of using three formulas like macropod told me.

I get syntax error when trying.

But how should i write if i want
first page =beslut
second page = beslut
Third = bilaga 1
Fourth = bilaga 2

That should cover it if i get such a formula to work?

Do i ahve to have the pagenumber in my topright corner for the formula to work..? Cause i didnt have that?

 
it did not work with ^b....

Im really stuck on this!
 
Hi Kalle82,

As I said in my previous post, the field code is:
{={PAGE}-2 \# "'Bilaga '0;'Beslut';'Beslut'"}
and:
Note: The field brace pairs (ie '{ }') for the above example are created via Ctrl-F9 - you can't simply type them or copy & paste them from this message.
That's BOTH pairs of field braces.

Cheers

[MS MVP - Word]
 
This is weird i did put the whole code inside the field braces.. and now it does not do anything, i tried ty view it in page preview but nothing and when i go back it is not there at all?

So my problems are right now, the formula isnt showing up at all, when entered...

And the sectionbreak thing, that should work the only problem is that between the runs i clear the document, and i the linking between the headers is cut like you told me, so every bookmark in my first header gets deleted...
So i need a way of reestablishing the links before cleaning and it should work..
 
Hi kalle82,

To find the formula, press Alt-F9 to expose the field code. Then make sure it's *exactly* as per what I posted, including the spaces.

If you go down the Section break route, unlinking the headers does not of itself delete anything.

Cheers

[MS MVP - Word]
 
Hi Paul,

How does that answer the OP's need?

Cheers

[MS MVP - Word]
 
macropod - It's possibly redundant now but it was the OP's very first question.

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top