Quote:
| Originally Posted by xfactor20 Thanks much, this is clarifying. However, I am having some continued problems with this object model. I am trying to: 1) Open a word document; 2) read line by line; 3) insert text as I come across matches to my constant strings that I have defined and 4) Write all of the new text out to a word file. Any further assistance you can provide is highly appreciated. |
You first need to understand that a Word file is not a text file. Text files have definite line endings. Word files do not necessarily have definite line endings. They also contain markup that prevent them from being read as plain text. Thus, the Word Automation object does not treat them like text files. It treats them like Word documents. Let me take your points one at a time.
Quote:
| Originally Posted by xfactor20 1) Open a word document; |
Opening a Word document is as simple as creating an instance of the Word automation object and using it's Open method. There are various other properties that control the programs behavior.
vb Code:
Original
- vb Code |
|
|
|
Const wdDoNotSaveChanges = 0
strDocument = "C:\mydoc.docx"
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
objWord.DisplayAlerts = False
objWord.Documents.Open strDocument,, True
Set objDoc = objWord.ActiveDocument
Here I've also showed you how to access the Document object as well.
Quote:
| Originally Posted by xfactor20 2) read line by line |
Again, you shouldn't attempt to read Word files line by line. You can easily access the entire contents of the document using the Document object.
vb Code:
Original
- vb Code |
|
|
|
Set objRange = objDoc.Content
strContents = objWord.CleanString(objRange.Text)
For larger documents, you should move through smaller ranges by reading one paragraph at a time.
vb Code:
Original
- vb Code |
|
|
|
Set colParagraphs = objDoc.Paragraphs
For Each objParagraph In colParagraphs
strParaText = objParagraph.Range.Text
Next
Quote:
| Originally Posted by xfactor20 3) insert text as I come across matches to my constant strings that I have defined |
Again, there's no need to work line by line. There's really no need to even read in any text at all. You can use Word's own Find and Replace tool instead.
vb Code:
Original
- vb Code |
|
|
|
Const wdDoNotSaveChanges = 0
Const wdFindContinue = 1
Const wdReplaceAll = 2
strFindText = "Text to find"
strReplaceText = "Text to replace with"
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
Set objDoc = objWord.Documents.Open("mydoc.doc")
Set objSelection = objWord.Selection
With objSelection.Find
.MatchWholeWord = vbTrue
.MatchCase = vbFalse
.Wrap = wdFindContinue
.Forward = vbTrue
.Text = strFindText
.Replacement.Text = strReplaceText
blnFound = .Execute(,,,,,,,,,, wdReplaceAll)
If blnFound Then
intMatches = .Found
Else
intMatches = 0
End If
End With
objDoc.Close
objWord.Quit wdDoNotSaveChanges
WScript.Echo intMatches & " instances were replaced."
Word allows you to control all of the search and replace options as well. Here are the constants you may find useful.
vb Code:
Original
- vb Code |
|
|
|
'WdReplace
Const wdReplaceNone = 0 'Only Find matches
Const wdReplaceOne = 1 'Only replace first instance
Const wdReplaceAll = 2 'Replace all matches
'WdFindWrap
Const wdFindStop = 0 'Stop when the end of the document is reached
Const wdFindContinue = 1 'Continue from the beginning if the search began in the middle of the document
Const wdFindAsk = 2
Quote:
| Originally Posted by xfactor20 4) Write all of the new text out to a word file. |
There's no need to "write the new text out to a word file". You can actually use the Word automation object to save a new document. The Document object provides access to Word's familiar Save As command.
vb Code:
Original
- vb Code |
|
|
|
objDoc.SaveAs("C:\newfilename.doc")