[RESOLVED] microsoft word VBA coding that counts word/s in the document
I need a code that counts word/s in the microsoft word document. Can it be done using MS VB 6.0? I am neophyte to this forum, thanks in advance.
Re: microsoft word VBA coding that counts word/s in the document
VB Code:
Sub countwords()
'
' countwords Macro
' Macro created 16/11/2005 by
'
Debug.Print ActiveDocument.Words.Count
End Sub
Re: microsoft word VBA coding that counts word/s in the document
sorry if i did not explain briefly my question in the first post... here it is, i need a code that count specific word or words in the microsoft word document, i mean the occurrence of the word or words in the document. i need this to be coded in the visual basic 6.0, how can it be done? thanks in advance.
Re: microsoft word VBA coding that counts word/s in the document
Use the .Find method in a loop counting the instances.
Re: microsoft word VBA coding that counts word/s in the document
sir RobDog888, could you please show me a code on this. it would be helpful for me if i can see the code. sorry for the ignorant. thanks for helping me. hoping for your kind and consideration.
Re: microsoft word VBA coding that counts word/s in the document
Sure no problem. Here is an example from the helpfile.
VB Code:
With ActiveDocument.Content.Find
.ClearFormatting
.Style = wdStyleHeading3
Do While .Execute(FindText:="", Forward:=True, _
Format:=True) = True
With .Parent
.StartOf Unit:=wdParagraph, Extend:=wdMove
.InsertAfter "Tip: "
.Move Unit:=wdParagraph, Count:=1
End With
Loop
End With
Re: microsoft word VBA coding that counts word/s in the document
it seems that you give me a code using the VBA coding not the VB 6.0 coding.... is that possible if i code that using VB 6.0, does it run... There are methods in VBA that can not be supported in VB 6.0, correct me if i am wrong....
Re: microsoft word VBA coding that counts word/s in the document
VBA is a subset of VB, so nearly all VBA code can be used in VB. Also, all the methods that RobDog used are methods of the Word object model, rather than anything VBA specific. When you create an instance of a Word object in VB, you can use it in the same way as you would in VBA (so you can almost copy & paste macro's into VB programs).
In the code above all you need to change is "ActiveDocument", which needs to be the Document object variable that you have created & initialised. From your comments I assume you don't know how to do that, so here's a little example:
VB Code:
'Add a reference (under "Project"->"References") to "Microsoft Word [i]X.X[/i] object library"
'declare objects
Dim oWordApp as Word.Application
Dim oWordDoc as Word.Document
'set up objects
Set oWordApp = New Word.Application
Set oWordDoc = oWordApp.Documents.Open("c:\my folder\test.doc")
'work with objects (just a copy of the post above)
With [b]oWordDoc[/b].Content.Find
.ClearFormatting
.Style = wdStyleHeading3
Do While .Execute(FindText:="", Forward:=True, _
Format:=True) = True
With .Parent
.StartOf Unit:=wdParagraph, Extend:=wdMove
.InsertAfter "Tip: "
.Move Unit:=wdParagraph, Count:=1
End With
Loop
End With
'save & close objects
oWordDoc.Save
oWordDoc.Close
Set oWordDoc = Nothing
oWordApp.quit
Set oWordApp = Nothing
(note: there may be minor errors, as I just typed this into the browser!)
Re: microsoft word VBA coding that counts word/s in the document
to find the details of document
iPg = 0: iPg = wdoc.ComputeStatistics(wdStatisticPages)
iWrd = 0: iWrd = wdoc.ComputeStatistics(wdStatisticWords)
iChrNoSp = 0: iChrNoSp = wdoc.ComputeStatistics(wdStatisticCharacters)
iChrWSp = 0: iChrWSp = wdoc.ComputeStatistics(wdStatisticCharactersWithSpaces)
iPara = 0: iPara = wdoc.ComputeStatistics(wdStatisticParagraphs)
iLines = 0: iLines = wdoc.ComputeStatistics(wdStatisticLines)
Header and FOtter Count
Set wFRangeHdr = wPageStart.Sections(1).Headers(wdHeaderFooterFirstPage).Range
Set wFRangeFtr = wPageStart.Sections(1).Footers(wdHeaderFooterFirstPage).Range
Set wPRangeHdr = wPageStart.Sections(1).Headers(wdHeaderFooterPrimary).Range
Set wPRangeFtr = wPageStart.Sections(1).Footers(wdHeaderFooterPrimary).Range
If (iSection < wSec.Index) And wSec.PageSetup.DifferentFirstPageHeaderFooter Then
HdrWrd = wFRangeHdr.ComputeStatistics(wdStatisticWords)
HdrChrNoSp = wFRangeHdr.ComputeStatistics(wdStatisticCharacters)
HdrChrWsp = wFRangeHdr.ComputeStatistics(wdStatisticCharactersWithSpaces)
HdrPara = wFRangeHdr.ComputeStatistics(wdStatisticParagraphs)
HdrLines = wFRangeHdr.ComputeStatistics(wdStatisticLines)
FtrWrd = wFRangeFtr.ComputeStatistics(wdStatisticWords)
FtrChrNoSp = wFRangeFtr.ComputeStatistics(wdStatisticCharacters)
FtrChrWsp = wFRangeFtr.ComputeStatistics(wdStatisticCharactersWithSpaces)
FtrPara = wFRangeFtr.ComputeStatistics(wdStatisticParagraphs)
FtrLines = wFRangeFtr.ComputeStatistics(wdStatisticLines)
End if
Re: microsoft word VBA coding that counts word/s in the document
neo_phyte is looking for a specific word and how many times it appears in the document. The document stastics wil not really be of mush help in this scenerio. :)