dksj
09-25-2007, 10:34 AM
All,
I have an Excel script that goes through a file share and opens up published Word documents in order to find and replace text. Unfortunately it doesnt seem to do the job with below:
Set wrd = CreateObject("Word.Application")
wrd.DisplayAlerts = wdAlertsNone
wrd.Visible = True
While strCurFile <> ""
strFullFileName = strTgtPath & strCurFile
Range("C12").Value = strFullFileName
Range("C14").Value = Range("C14").Value + 1
On Error Resume Next
Set MyDoc = wrd.Documents.Open(strFullFileName)
MyDoc.Select
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Crisis Response Leader"
.Replacement.Text = "Immediate Response Leader"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
If MyDoc.Find.Found = True Then
Range("C26").Value = Range("C26").Value + 1
shtFileListTab.Cells(k, 1).Value = MyDoc.FullName
End If
k = k + 1
MyDoc.Close Savechanges:=wdSaveChanges
strCurFile = Dir()
Wend
wrd.Quit
Set wrd = Nothing
Set MyDoc = Nothing
The highlighted portion of the code does work if run within Word. Is there some way to call a Word procedure from the Excel script?
Dan
I have an Excel script that goes through a file share and opens up published Word documents in order to find and replace text. Unfortunately it doesnt seem to do the job with below:
Set wrd = CreateObject("Word.Application")
wrd.DisplayAlerts = wdAlertsNone
wrd.Visible = True
While strCurFile <> ""
strFullFileName = strTgtPath & strCurFile
Range("C12").Value = strFullFileName
Range("C14").Value = Range("C14").Value + 1
On Error Resume Next
Set MyDoc = wrd.Documents.Open(strFullFileName)
MyDoc.Select
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Crisis Response Leader"
.Replacement.Text = "Immediate Response Leader"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
If MyDoc.Find.Found = True Then
Range("C26").Value = Range("C26").Value + 1
shtFileListTab.Cells(k, 1).Value = MyDoc.FullName
End If
k = k + 1
MyDoc.Close Savechanges:=wdSaveChanges
strCurFile = Dir()
Wend
wrd.Quit
Set wrd = Nothing
Set MyDoc = Nothing
The highlighted portion of the code does work if run within Word. Is there some way to call a Word procedure from the Excel script?
Dan