非常新:请帮助!从一个Word文档中选择一系列文本并复制到另一个Word文档中
我从其中一个答案中看到了下面的代码(感谢那些提供它的人)。该代码在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中使用这些数据来运行从Excel VBA相同的代码。我的下面的尝试失败了编译器错误:
Argument not optional
关于.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
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-find- method-excel – braX
参数不是可选的意思就是这样。你省略了一些参数。 – peakpeak