从Xcel Vba中选择一个Word文档中的一系列文本并复制到另一个Word文档中

时间:2017-08-08 14:43:49

标签: excel vba

对此非常新:请帮忙!

我从其中一个答案中看到了以下代码(感谢那些提供它的人)。代码在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

1 个答案:

答案 0 :(得分:0)

参数不是可选的意思就是那个。你省略了一些参数。 使用Intellisense:输入rng1.Find( ......你会得到论点 enter image description here