My goal is to use a cell reference to search for the string of text in the cell by opening up a certain word document and passing in the cell or string into a find macro located in word VBA. Essentially, I am trying to pass a variable from an excel macro into a word macro. I cannot make this work. My goal is to pass the variable Party
to a Word macro called macro5. I can get the below code to work by not passing in an argument and just hardcoding in the text in the word macro, but it does not work when I try to pass in an argument from excel. Any help would be greatly appreciated.
My excel Macro code:
Sub Open_Correct_WordDOC()
' Open_Correct_WordDOC Macro
Dim WordApp As Object
Dim WordDoc As Object
Dim Party As String
Party = "commercial"
MsgBox Party
Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open(Filename:="J:enterdocumenthere.docx", _
ReadOnly:=True)
WordApp.Visible = True
WordApp.Run "Normal.NewMacros.Macro5", Party
Set WordDoc = Nothing
Set WordApp = Nothing
End Sub
Word Macro Code:
Sub Macro5(xlvar As String)
Selection.Find.ClearFormatting
With Selection.Find
.Text = xlvar
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.Execute
End Sub
Normal
template), in spite of theWord VBA
help onApplication.Run
suggesting exactly the code posted. – chris neilsen