Log in

View Full Version : Calling Word macro from Excel macro



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

Oorang
09-25-2007, 03:00 PM
Just set a reference to Microsoft Word :)

Brandtrock
09-25-2007, 10:50 PM
Just set a reference to Microsoft Word :)

This article (http://vbaexpress.com/forum/showthread.php?t=9586) illustrates how to add a reference manually or programmatically. The reference in the article is NOT to Word, but the procedure remains the same (except for the GUID number). To get a list of these numbers that you might have occasion to use frequently, this KB (http://vbaexpress.com/kb/getarticle.php?kb_id=713) will list them for any references checked manually before the macro is run.

Hope this helps,