对此非常新:请帮忙!
我从其中一个答案中看到了以下代码(感谢那些提供它的人)。代码在Word VBA中工作,我测试了它。
Sub RevisedFindIt()
' Purpose: display the text between (but not including)
' the words "Title" and "Address" if they both appear.
Dim rng1 As Range
Dim rng2 As Range
Dim strTheText As String
Set rng1 = ActiveDocument.Range
If rng1.Find.Execute(FindText:="Title") Then
Set rng2 = ActiveDocument.Range(rng1.End, ActiveDocument.Range.End)
If rng2.Find.Execute(FindText:="Address") Then
strTheText = ActiveDocument.Range(rng1.End, rng2.Start).Text
MsgBox strTheText
End If
End If
End Sub
我想从EXCEL VBA运行相同的代码,通过从主excel vba sub调用它作为sub并传递一些参数,因为我需要使用EXCEL中的数据。下面的尝试因编译错误而失败:
参数不是可选的
与.Find.Execute(FindText:=
相关。
Sub FindIt(ftext, text1, text2, texto)
' Purpose: display the text between (but not including)
' the words "Title" and "Address" if they both appear.
'Dim wdDoc As Object, wdApp As Object
Dim wdApp As Object
Dim wdDoc As Object
'Set wdApp = CreateObject("Word.application")
Set wdApp = New Word.Application
Set wdDoc = wdApp.Documents.Open(ftext)
wdApp.Visible = True
Dim rng1 As Range
Dim rng2 As Range
Dim strTheText As String
With wdDoc
Set rng1 = ActiveDocument.Range
If rng1.Find.Execute(FindText:=text1) Then
Set rng2 = ActiveDocument.Range(rng1.End, ActiveDocument.Range.End)
If rng2.Find.Execute(FindText:=text2) Then
strTheText = ActiveDocument.Range(rng1.End, rng2.Start).Text
MsgBox strTheText
texto = strTheText
End If
End If
End With
wdDoc.Close savechanges:=False
wdApp.Quit
Set wdDoc = Nothing
Set wdApp = Nothing
End Sub