我的目标是使用单元格引用通过打开某个单词文档并将单元格或字符串传递到位于单词VBA中的查找宏来搜索单元格中的文本字符串。本质上,我试图将一个变量从excel宏传递到一个word宏。我无法做到这一点。我的目标是将变量Party
传递给名为 macro5 的Word宏。我可以通过不传入参数并只是在单词宏中的文本中硬编码来获得下面的代码,但是当我尝试从excel传入参数时它不起作用。任何帮助将不胜感激。
我的Excel宏代码:
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宏代码:
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
答案 0 :(得分:0)
在excel模块中:
Option Explicit
Public WordApp As Object
Public WordDoc As Object
Public Party As String
Sub Open_Correct_WordDOC()
' Open_Correct_WordDOC Macro
Party = "commercial"
MsgBox Party
Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open(Filename:="H:\docsearch.docx", ReadOnly:=True)
WordApp.Visible = True
WordApp.Activate
WordApp.Run "Macro5", Party
Set WordDoc = Nothing
Set WordApp = Nothing
End Sub
我认为您的问题是您在excel宏中声明了对象和字符串 。我让他们公开,对我来说代码运行正常。
Addon:Word宏位于Normal模板下的模块中。这对我来说很好。