[英]from Xcel Vba select a range of text from one Word document and copy into another Word document
Very new to this: Please Help! 对此很新:请帮助!
I saw the following code from one of the answers (thanks to those who provided it). 我从其中一个答案中看到了以下代码(感谢提供该代码的人)。 The code works in Word VBA, I tested it.
该代码在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
I want to run the same code from EXCEL VBA, by calling it as a sub from a main excel vba sub and passing some arguments to it, as I need to use the data within EXCEL. 我想从EXCEL VBA运行相同的代码,方法是从主excel vba子程序调用它作为子程序,并将一些参数传递给它,因为我需要在EXCEL中使用数据。 My attempt below failed with a Compiler error:
我在下面的尝试失败,并出现编译器错误:
Argument not optional
参数不可选
in relation to the .Find.Execute(FindText:=
. 相对于
.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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.