
May 20th, 2008, 02:56 AM
|
 |
|
|
Join Date: Jun 2006
Location: Salem, OH
|
|
First off, you need to define the wdReplaceAll constant. It does not become available when you instantiate the Word object.
vb Code:
Original
- vb Code |
|
|
|
You can also simplify the next statement.
vb Code:
Original
- vb Code |
|
|
|
Set myRange = objDoc.ActiveDocument.Range(0,objDoc.ActiveDocumen t.Characters.Count)
You're using a Range to select the entire contents of the document. This can be achieved by calling the Range method without any parameters.
vb Code:
Original
- vb Code |
|
|
|
Set myRange = objDoc.ActiveDocument.Range()
The problem here seems to be a little syntax issue in the Execute method. Here's a simple Find/Replace example. You can use either a selection or a range at your own discretion.
vb Code:
Original
- vb Code |
|
|
|
Set objWord = CreateObject("Word.Application") objWord.Visible = False Set objDoc = objWord.Documents.Open("mydoc.doc") Sub ReplaceText(tWhat, tWith) Const wdFindContinue = 1 Const wdReplaceAll = 2 Set objSelection = objWord.Selection 'or Set objRange = objDoc.ActiveDocument.Range() With objSelection.Find 'or With objRange.Find .Text = tWhat .Forward = True .MatchWholeWord = True .Wrap = wdFindContinue .Format = False .Replacement.Text = tWith .Execute ,,,,,,,,,,wdReplaceAll End With End Sub
Be careful using the ClearFormatting methods. They can lead to some unexpected results and your saved document may have some expected formatting removed.
|