2017-08-08 161 views
0

非常新:請幫助!從一個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 
+1

https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-find- method-excel – braX

+0

參數不是可選的意思就是這樣。你省略了一些參數。 – peakpeak

回答

0

參數不是可選的意思就是這樣。你已經省略了一些論據。 使用IntelliSense:類型rng1.Find( ,而且你會本身的參數 enter image description here

+0

感謝你的迴應。實際上.Find命令在Word vba sub中工作。我所要做的就是在Excel中做同樣的子工作。 –

相關問題