Monday, December 17, 2012

Automating Word Document- 1

How to print all words in a word document?
Dim oWord, Doc, currentDocument
Doc = "C:\Desktop\Test.doc" 'contains 200 words
Set oWord = CreateObject("Word.Application")
oWord.DisplayAlerts = 0
oWord.Documents.Open Doc
Set currentDocument = oWord.Documents(1)
Dim nWords, n
nWords = currentDocument.Words.count
Print  "Number of Words:  " & nWords
For n = 1 to nWords
     Print currentDocument.Words(n)
Next
currentDocument.Close
Set currentDocument = Nothing
oWord.Quit
Set oWord = Nothing
********************************************
 How to count number of pages in a word document?
Below lines of code shows how to find total number of pages in a word document:
SystemUtil.CloseProcessByName("WINWORD.exe")
Dim oWord, Doc
Doc = "C:\Desktop\Test.doc" 'contains 20 pages
Set oWord = CreateObject("Word.Application")
oWord.Documents.Open  doc
Dim nPages
nPages=oWord.ActiveDocument.BuiltInDocumentProperties("Number of pages")
msgbox nPages ' will display total number of pages
ActiveDocument.Close
Set ActiveDocument = Nothing
oWord.Quit
Set oWord = Nothing

No comments:

Post a Comment