简体   繁体   English

从Xcel Vba中从一个Word文档中选择一系列文本,然后复制到另一个Word文档中

[英]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

Argument not optional means just that. 参数不是可选的就意味着。 You have omitted some arguments. 您已经省略了一些参数。 Use Intellisense: Type rng1.Find( ... and you will se the arguments 使用Intellisense:键入rng1.Find(...,您将获得参数 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM