在 Word 中搜索电子表格中的特定值

在 Word 中搜索电子表格中的特定值

我想使用 Word VBA 查找电子表格中括号之间的所有单词。

我让“查找”例程在 Excel VBA 中完美运行,但尝试将代码传输到 Word 时,出现错误。以下是代码(错误位于加粗的行中.Find,就在下方n = 1 to 100。错误是Object doesn't support method

Function ScanExcelforVariables(ExcelPath, WorkbookToWorkOn)
Dim WORDIS As String
Dim N As Integer
Dim openPos As Integer
Dim closePos As Integer
Dim midBit As String
Dim ALLVARS As String
Dim sText As String
Dim ExcelWasNotRunning As Boolean
Dim oXL As Object
Dim oXLwb As Object
If BeginTxtIDB = "" Then SetBoundaryMarks
'If Excel is running, get a handle on it; otherwise start a new instance
On Error Resume Next
Set oXL = GetObject(, "Excel.Application")
If Err Then
    Set oXL = CreateObject("Excel.Application")
    ExcelWasNotRunning = True
    'Set oXL = New Excel.Application
End If
On Error goto 0
If Dir(AddLastSlash(ExcelPath) & WorkbookToWorkOn) <> "" Then
Set oXLwb = oXL.Workbooks.Open(filename:=AddLastSlash(ExcelPath) & WorkbookToWorkOn, ReadOnly:=True)
End If
oXL.Visible = True
Set oWS = oXLwb.worksheets(1)
oWS.Range("A1").Select
With oWS
For N = 1 To 100
    **.Find(what:="[", After:=.ActiveCell, LookAt:=2, _
        SearchOrder:=1, SearchDirection:=1, MatchCase:=False, _
        SearchFormat:=False).Activate**
    'to break out of looping:    
    If .ActiveCell.Address = FIRSTCELL Then Exit Function

    FIRSTCELL = .ActiveCell.Address
    sText= .ActiveCell.text
    On Error Resume Next
    openPos = 1
    Do Until openPos = 0
        openPos = InStr(closePos, sText, "[")
        If openPos = 0 Then Exit Do
        On Error Resume Next
        closePos = InStr(openPos, sText, "]")
        On Error Resume Next
        midBit = Mid(sText, openPos + 1, closePos - openPos - 1)
        If InStr(ALLVARS, "|" & midBit & "|") Then
            'DO NOTHING, VAR EXISTS
        Else
            ALLVARS = ALLVARS & "|" & midBit
        End If
    Loop
    closePos = 1
Next N
End With
MsgBox ALLVARS
End Function

相关内容