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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create new Word doc showing word and page count of active doc 1

Status
Not open for further replies.

Strobeman

Programmer
May 16, 2003
40
AU
I wrote the following code to count the words and pages in the current active document and then write them out to another document.


Dim wordCount As Long
Dim myRange As Range
Dim pageCount As Long
Dim objWdApp As Object
Dim objWdDoc As Object
Dim objwdRange As Object

Set myRange = ActiveDocument.Range
wordCount = myRange.ComputeStatistics(wdStatisticWords)
ActiveDocument.Repaginate
pageCount = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)

sPath = ActiveDocument.Path

Set objWdApp = CreateObject("Word.Application")
Set objWdDoc = objWdApp.Documents.Add
Set objwdRange = objWdDoc.Content

objwdRange.Text = "Word Count: " & wordCount & vbCr & "Page Count: " & pageCount

objWdDoc.SaveAs FileName:=sPath & "\Word_Count.doc"

objWdApp.Quit

Set objwdRange = Nothing
Set objWdDoc = Nothing
Set objWdApp = Nothing
 
OK. Is this a question or are you just bragging?
 
There is nothing to brag about.

1. Do not need the myRange object, as you never use it again.
Code:
wordCount = ActiveDocument.Range.ComputeStatistics(wdStatisticWords)
will give the value to wordcount just as well.

2. You use
Code:
    sPath = ActiveDocument.Path

BEFORE you use:
Code:
    Set objWdApp = CreateObject("Word.Application")
    Set objWdDoc = objWdApp.Documents.Add

sPath = ActiveDocument.Path will only work if you are already in Word - i.e. Word is open.

In which case, there is absolutely no need to create another instance of Word (CreateObject("Word.Application")). In fact, there are very good reasons NOT to create another instance.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
If you are actually in Word, you can do this much easier (without all those extraneous variables) by using Documebnt objects.
Code:
Sub StatsToNewDoc()
Dim ThisDoc As Document
Dim StatsDoc As Document

Set ThisDoc = ActiveDocument
Set StatsDoc = Documents.Add

' do you really need to repaginate?
ThisDoc.Repaginate

StatsDoc.Range.Text = _
   "Word Count: " & ThisDoc.ComputeStatistics(wdStatisticWords) _
      & vbCrLf & "Page Count: " & _
      ThisDoc.BuiltInDocumentProperties(wdPropertyPages)

StatsDoc.SaveAs FileName:=ThisDoc.Path & _
      "\Word_Count.doc"
   
End Sub

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Thanks guys. Actually I don't brag about my code but I do try and share things I discover which is why I marked this post with the little light bulb. Thanks to those of you that actually gave me some helpful advice.
 
You are welcome. And thank you for being willing to share. That is what makes this site work.

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top