PDA

View Full Version : Solved: Adding Word count programatically to footer?



Simon Lloyd
10-24-2006, 11:55 AM
Hi all, is there a way of automatically including word count in the footer on each page, i have some experience using VBA but in excel not word.

Regards,
Simon

fumei
10-24-2006, 12:10 PM
A word count of what?

Simon Lloyd
10-24-2006, 12:57 PM
Sorry for not being clear, i want the word count of the document im working on, meaning all the main body of of the document not headers and footers. My assignments have to have a word count, i dont mind if each page has the word count for that page, rather than the total either way i dont mind.

Thanks for the speedy response!

Regards,
Simon

fumei
10-26-2006, 01:35 PM
is this for a school assignment?

Simon Lloyd
10-26-2006, 02:46 PM
Haha I havent been to school for 24 years!!!, no its for my girlfriends assignment she's typing she has to have the page word count in the footer of each page, i was just trying to save her the trouble of doing it manually!

Regards,
Simon

fumei
10-27-2006, 02:37 AM
Sorry, but you have a problem.

You can get a page count of any page the Selection is on with:Dim oRange As Word.Range
Dim intWordCount As Integer
Set oRange = ActiveDocument.Bookmarks("\page").Range
intWordCount = oRange.Words.Count
Set oRange = NothingSo, OK you have the word count of whatever page the Selection is on.

The problem lies in the way Word is designed regarding pages and Sections. Page numbers are objects of the footers. Footers are objects of the Section.

Say you have 10 pages, one Section. Even using Different first page, and Different odd/even, the maximum number of DIFFERENT footers in that document is three.

Page 1 word count = x
Page 2 (even) word count = y
Page 3 (odd) word count = z
Page 4(even).....word count = y AS IT WOULD USE THE SAME FOOTER as page 2.

That is, unless you made every page = one section. Then, yes you could do it.

What I am saying is...a Section can only have three different footers. It makes no difference how many pages there are, if a Section has 100 pages...there are three possible footers. If a Section has 20 pages...there are three possible footers. If a Section has 1 page....there are three possible footers.

So if you want a different footer for every page (so you can put that page word count in it), you will have to have every page in its own Section.

There is nothing wrong with doing this, but that is what you would have to do.

Simon Lloyd
10-30-2006, 04:14 PM
Ok Gerry thanks for the reply i understood that. Is there perhaps a way to make the word count for that page appear on the last line of the document directly above the footer so that it is fixed? i.e not move down to next new sheet if the characters on the page exceed the limit for that page.

I suppose what im trying to say is can the last line of each page be dedicated to just the information regarding the word count?

regards,
Simon

fumei
10-31-2006, 02:35 AM
Not unless to make a fixed separation of pages. In other words...what used to be called a hard page break.

You must understand that Word does NOT know what a page is. A "page" is what Word calculates can be displayed on a page using the current printer driver.

You can take the same document, to a different machine, and even if that machine uses the SAME printer, if that machine has a different printer driver version...Word will make the pages different.

Let me repeat that. Word does NOT know what a page is. The pagination of a Word document is totally, completely, 100% determined by the current printer driver.

When you open a document, ever see that little printer icon on the staus bar fluttering away? That is Word figuring out what the pages currently are. It has to do that every single time. There are NO fixed pages in Word...unless of course you make every page break an explicit ("hard") page break.

Then...yes, you could put the word count at the end of page. Of course it would include the text of "Word Count is" - unless you removed those characters from the count.

Frankly, I fail to see the point of the exercise. As to your question:
Is there perhaps a way to make the word count for that page appear on the last line of the document directly above the footer so that it is fixed? i.e not move down to next new sheet if the characters on the page exceed the limit for that page.
Simple answer...NO. Read you own last words. If characters on the page exceed the limit for that page.....they move on to the next page.

This is what word processors DO.

Are there ways to do fancy tricking to achieve pretty much what you ask...possibly...well...yes. But really, again, I fail to see the point for that amount of work. What is a real business case for having the word count per page?

Simon Lloyd
10-31-2006, 06:26 PM
Thanks again Gerry, its not business as you know it was just to help my girlfriend with her assignment which has to be 8,000 words no less than 750 words per page! that is the only reason i was looking to automate it for her. Like you said its a lot of work for a small gain!

Thanks for your time.

Regards,
Simon

mdmackillop
11-01-2006, 12:58 AM
Hi Simon
Here's some code to do a count. You could tweak this to write a summary for appending to the assignment.
Regards
MD

Option Explicit

Sub CountPages()
Dim objPage As Page, tmp As Long, Cnt As Long, i As Long
Dim msg As String
Application.ScreenUpdating = False
tmp = ActiveDocument.ActiveWindow.Panes(1).Pages.Count
Cnt = 0
For i = 2 To tmp
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=CStr(i)
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend

msg = msg & Selection.Words.Count - Cnt & vbCr
Cnt = Selection.Words.Count
Next
msg = msg & ActiveDocument.Content.Words.Count - Cnt & vbCr
Selection.HomeKey Unit:=wdStory
MsgBox msg
Application.ScreenUpdating = True

End Sub

fumei
11-01-2006, 11:58 AM
Hi Malcom,

1. you declare objPage As Page, but never use it. Further, I take it you are using 2003, as there is no Page in earlier versions.

2. this builds a message string on the word count per page. Indeed that is the only way to do it, by moving the Selection through the pages. However, as you state, this could be used as an summary appendage. The problem with a word count per page ON that page remains.

The only real solution, AFAIK, would be to have every page a Section. But hey, if a summary is good enough, then you certainly got it right.

mdmackillop
11-01-2006, 12:55 PM
Hi Gerry,
You're right about objPage. I started one way and ended going another route, but I couldn't see any future in the footer method either. Of course one could also use the tried and tested method of "writing" to put the word count at the bottom of each page.

Simon Lloyd
11-01-2006, 05:02 PM
Malcom thanks for the reply, a summary page won't do unfortunately it has to be a word count per page as each page has a limitation on the amount of words you can have the examination board are strict on this.

Regards,
Simon

mdmackillop
11-01-2006, 06:01 PM
Try
Option Explicit

Sub CountPages()
Dim objPage As Page, tmp As Long, Cnt As Long, i As Long
Dim msg As String, Tot As Long
Application.ScreenUpdating = False
tmp = ActiveDocument.ActiveWindow.Panes(1).Pages.Count
Cnt = 0
Tot = ActiveDocument.Content.Words.Count
For i = 2 To tmp
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=CStr(i)
Selection.HomeKey Unit:=wdStory, Extend:=wdExtend

msg = msg & "Page " & i - 1 & " has " & Selection.Words.Count - _
Cnt & " words" & vbCr
Cnt = Selection.Words.Count
Next
msg = msg & "Page " & i - 1 & " has " & ActiveDocument.Content.Words.Count _
- Cnt & " words" & vbCr

Selection.HomeKey Unit:=wdStory

Application.ScreenUpdating = True

Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak
Selection.TypeText msg & vbCr
Selection.TypeText "Total words " & Tot
End Sub

Simon Lloyd
11-01-2006, 08:19 PM
Malcom, I like what its doing but unfortunately its adding the word counts and totals at the end of the entire text, so if it spans 3 pages it will give me the counts on page 4, the problem is she needs each seperate page's word count on its own page i.e Page 1 has xxx words, this would appear on page 1, on page 2, Page 2 has xxx words etc.

In your code this line Selection.InsertBreak Type:=wdPageBreak would put the word counts and totals on a seperate page i changed it for Selection.EndKey Unit:=wdStory
'Selection.InsertBreak Type:=wdPageBreak
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText msg & vbCr
Selection.TypeText "Total words " & Totto move 2 lines down from the last line of text on the page but of course still puts the count on page 3 if the document was that long.

i have understood that its near impossible as Fumie said unless you did something with page breaks, i'm not familiar with word VBA and variables so wouldn't know where to start......still i like what you have produced, she could always cut and paste each page's word count! as she will need something to do after she whisks through 8,000 words! LOL

Regards,
Simon

mdmackillop
11-02-2006, 01:29 AM
Is this exercise being submitted electronically or on paper. If it's electronically, then the wordcount will vary according to the recipients' printer setup, so a problem there.
If it's being submitted in printed form you could either:
1. Write the total on each page (who says it must be printed?)
2. Run the final version through the printer again, overprinting with the page count data at the bottom.

Simon Lloyd
11-02-2006, 10:24 AM
Its being submitted i printed format from our home computer, how would i
Run the final version through the printer again, overprinting with the page count data at the bottom.? would it not just print all of the assignment again with a final sheet with the counts on....to tell you the truth malcom i have no idea what i am doing, i haven't been able to delete a new page that was created, there doesnt seem to be an option for that, so i close the program and re-open a new one and paste the code!

LOL

Thanks for your time on this,

Regards,
Simon

mdmackillop
11-02-2006, 11:13 AM
Use my previous code to get the word count. Create a document like the attached and use it to do the overprinting of the original document

EricFletcher
11-04-2006, 09:44 AM
Maybe I'm missing something here, but are they REALLY that anal to want "no less than 750 words per page" and require a word count for every page? If the total and an average word count per page would suffice, you could just include the following in a page footer:

{ DOCPROPERTY Words \# "#,##0" } words in { NUMPAGES } pages; average of { ={ DOCPROPERTY Words }/{ NUMPAGES } words per page

As long as you enter these as field codes (i.e. the {} braces are NOT typed), this will report something like the following:

8,054 words in 11 pages; average of 732.18 words per page

You can use a numeric format switch to refine the presentation of the average value if you need to. Note that the fields will not update automatically within the footer: I find the easiest way to update them is to toggle to normal view and back again.

[added after a moments reflection] If you did add the above to the footer, you could make adjustments to font size, page margins, indents or other formatting attributes to bring the average word count closer to the required 750 words per page. Make a change, refresh the field count; repeat until average approaches 750. Tedious but not difficult...

fumei
11-04-2006, 11:33 AM
But you STILL have the issue that you can not have more han three different footers per Section. There is simply no way around the fact that footers are objects of the Section. If you want a different footer for every single page, then you need to section out the pages.

EricFletcher
11-04-2006, 12:14 PM
Quite so Gerry. My point was that having a report for the # of words on each page seemed an unlikely request (certainly an odd one). If a summary of the total and average word count per page would be sufficient for Simon's girlfriend, my fields will report it.

In fact, the DOCPROPERTY word count is actually not very precise: it doesn't return the same number as selecting the body and using Tools | Word Count. For articles with word count limits, I rely on the latter approach for a more accurate count. It also gives you the ability to select exactly what you need to count, so you can exclude submission notes and the count report itself.

Simon Lloyd
11-05-2006, 12:12 AM
Eric, thanks for the reply and suggestion, and YES! they really are that anal, not only is the word count restrictive if you hand in a written piece it must have no:- corrections, strike throughs or correction fluid it must not have:- indented paragraphs, underlining (expression must take place as bold words) or subtitling seperating text..........so when you say anal im thinking ducks a** because thats water tight!

Regards,
Simon.

P.S thanks to all that replied, i thought it would have a simple solution not knowing much about MS Word, but it seems so much trouble to automate rather than highlight the body on each page and write down the count then type it at the bottom of each page!

fumei
11-06-2006, 09:05 AM
I think I have said this enough (four? five? times). You can do this for every page, if you make every page a Section. Or at least every three pages - as a Section can have three different footers. So...again...yes, if CAN be done.

Although frankly...if they are that anal...screw 'em. I see absolutely no real case for it. If you are doing translation services - for example - where you are getting PAID by the word (as if often the case with translation)...then sure.

However, even then...a word count ON every page???? Get real.

It depends on how fussy you want to be, but mor eimportantly, how many changes will the document undergo. It the text is settled, then:

1. make a Section break every three pages - assuming you have not already got Section breaks.

2. use Page Setup to use Different first page, Different odd/even

3. starting at Section 1, run through the three pages of that Section, pick up the word count for each...and put it in the footer. Go to the next Section. Repeat.

fumei
11-06-2006, 10:23 AM
Here is the code to do that. It is dynamic, so it can be repeated as many times as you like. The footer text will be replaced. You can, of course, add other text to the footer Range text - such as Page X of Y stuff.

Further, you COULD add more logic to do something if the word count is not correct...although I am not sure what exactly the logic IS for that.

In any case, as long as you have a maximum of three pages per Section - then this will in fact put a page word count in the footer of that page. Check out the demo document attached. You can fire the code by clicking "Page Word Count" on the top toolbar.

Go in, change some text - remove some, add some...then fire the code again. Change it again, and fire the code again. It will put the word count for each page in the footer for that page.

I must emphasize that you MUST only have a MAX of three pages per Section. There are three footers per Section. If a Section has 5 pages, then page 4 will repeat the footer for page 2, page 5 will repeat the footer for page 3 etc. etc.Option Explicit

Sub EachPageWordCount()
Dim oSection As Word.Section
Dim oFooter As Word.HeaderFooter
Dim oRange As Word.Range
Dim i As Integer, FooterNumber As Integer
On Error Resume Next
For Each oSection In ActiveDocument.Sections()
' set the Range object to the Section
Set oRange = oSection.Range
For i = 1 To 3
' determine which footer object to use
' based on the loop variable
Select Case i
Case 1 ' make Footer object 2 - First Page
FooterNumber = 2
Case 2 ' make Footer object 3 - Even page
FooterNumber = 3
Case 3 ' make Footer object 1 - Primary (now Odd page)
FooterNumber = 1
End Select
' set footer object and
' explicitly set each to Same as previous = false
Set oFooter = oSection.Footers(FooterNumber)
oFooter.LinkToPrevious = False
' collapse Range to its start
' move the Range End to the next page break
' pick up the word count of the Range
' and put it in current footer text
' then collapse Range, and move to
' next page (1 character)
With oRange
.Collapse Direction:=wdCollapseStart
.MoveEndUntil cset:=Chr(12), Count:=wdForward
oFooter.Range.Text = "Current page word count: " & _
oRange.ComputeStatistics(wdStatisticWords)
.Collapse Direction:=wdCollapseEnd
.MoveStart Unit:=wdCharacter, Count:=1
End With
Next
Set oRange = Nothing
Next
End Sub
Oh...and the thing with adjusting the footer object number...this is simply one of the odd aspects of the Word object model.

The LOOP goes 1, 2, 3...

But the Word footer index (when you have Different first page, and Different odd/even set) goes 2, 3, 1 - as index 1 = Primary...but Primary becomes Odd/Even when you have Different odd/even. So...

First page = footer index (2)
Even page = footer index (3)
Odd page = footer index (1) - was Primary

Simon Lloyd
11-06-2006, 10:27 AM
Fumei thanks again for the response, but really i was just trying to help her out a little and thought it may have been simple. Please dont spend anymore time on it she is quite capable of highlighting the text doing the word count and typing at the foot of the page (a single line above the footer), she wants her distinction in it and wanted it all perfect...it still will be with her adding the word count herself.

Once again thanks!

Regards,
Simon

Simon Lloyd
11-06-2006, 10:28 AM
Sorry Fumie posted befor you posted the code......Big Thank you!

Regards,
Simon

fumei
11-06-2006, 10:39 AM
Fine. That's OK. Do whatever works for you. My point was that if you DO want to automate (by code) getting a word count for every page into the footer for the page...it IS possible. You simply have to use Sections, as footers are objects of Sections, and there is absolutely no way around that. Footers belong to Sections.

mdmackillop
11-06-2006, 11:48 AM
Hi Gerry,
If only there was an award for the best solution to the most anal documentation, I'm sure it would be awarded to you for your efforts here:trophy:. I wonder what we could call it.:thinking:

fumei
11-06-2006, 12:45 PM
It certainly seems to be anal requirements. We don't even know what it is FOR - that is, what is the context of this documentation.

Shrug...whatever. It only took me about 20 minutes to do the code, and I wanted to make sure it would do as I thought it would. Which it does.

Actually, it was a bit of an interesting exercise. I wanted to do it without using the Selection at all - just ranges. I got stuck for a little bit before I remembered about the footer index. I kept on getting the count on the wrong page. That is why there is the Select Case in the For..Next loop.

It expanded my curiosity as to how to extract each page (using only Range) from a section with more than three pages.

BTW: there is an error....or rather there is a possible error. The code is based on the fact that there IS three pages per Section. I did state that there MUST be, and in fact, that is true. The For...Next runs 1 To 3, so there MUST be 3 pages. If there is not, things could get messed up.

There is, of course a solution to that - and to working with more than three pages. You make the section a range, count the page breaks - Chr(12) - and extrapolate from there.

Let's see....The BrilliantDumbass award?