简体   繁体   English

使用VBA从Excel到Office Word数组获取数据

[英]Get data from excel to office word array using VBA

I have an excel file where are stored in columns some text and keywords. 我有一个Excel文件,其中一些文本和关键字存储在列中。

I want to use the data in excel to make some Advanced search in Word using vba. 我想使用Excel中的数据使用vba在Word中进行一些高级搜索。 But I'm getting an error trying to transpose the data from excel cells to an array in vba word. 但是我在尝试将数据从excel单元转置到vba word中的数组时遇到错误。

I have used the transpose excel function but it doesn't handle more than 255 characters so I can't get cell's value that exceeds 255 characters. 我已经使用了转置excel函数,但是它不能处理超过255个字符,因此我无法获得超过255个字符的单元格值。

I would be thankfull if someone could give me a hand. 如果有人可以帮我,我将非常感激。

Option Explicit
    Dim strArray
    Dim range As range
    Dim i As Long
    Dim numberOfUniqMatches As Integer
    Dim totalMatches As Integer

Sub HighlightMatchesAndSummarize()
    totalMatches = 0
    '************************************ GET DATA FROM EXCEL ***************************************
    Dim xlApp As Object
    Dim xlBook As Object
    Const strWorkBookName As String = "D:\keyword_source_3.xlsx"
    On Error Resume Next
    Set xlApp = GetObject(, "Excel.Application")
    If Err Then
        Set xlApp = CreateObject("Excel.Application")
    End If
    On Error GoTo 0
    Set xlBook = xlApp.Workbooks.Open(FileName:=strWorkBookName)
    'xlApp.Visible = True
    xlApp.Visible = False
    'transpose excel cells in our arrays
    strArray = xlApp.Transpose(xlApp.ActiveSheet.range("A1:A20" & AlRow).Value)
    Set xlBook = Nothing
    xlApp.Quit
    Set xlApp = Nothing
    '
    ' End of data extraction

    '/******************************** SEARCH LOOP START **********************************
    For i = 1 To UBound(strArray)
        numberOfUniqMatches = 0
        Set range = ActiveDocument.range

        With range.Find
        .Text = strArray(i)
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchFuzzy = False
        .MatchPhrase = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
            Do While .Execute(Forward:=True) = True
                    numberOfUniqMatches = numberOfUniqMatches + 1
                    totalMatches = totalMatches + 1
                    range.HighlightColorIndex = wdYellow
            Loop
        End With
    Next
    '
    ' End of search loop

    ' Display message if no matching word is found
    If totalMatches <= 0 Then
        MsgBox "Sorry! No matching keyword found."
    Else
        MsgBox "Search ended: " & totalMatches & " matching word(s)."
    End If

End Sub

Change this: 更改此:

strArray = xlApp.Transpose(xlApp.ActiveSheet.range("A1:A20" & AlRow).Value)

To: 至:

'remove the transpose (and fix the range...)
strArray = xlApp.ActiveSheet.range("A1:A" & AlRow).Value

Then in your loop: 然后在您的循环中:

For i = 1 To UBound(strArray, 1) '<<<<<<<
    numberOfUniqMatches = 0
    Set range = ActiveDocument.range

    With range.Find
    .Text = strArray(i, 1) '<<<<<<<
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchFuzzy = False
    .MatchPhrase = True
    .MatchSoundsLike = False
    .MatchAllWordForms = False
        Do While .Execute(Forward:=True) = True
                numberOfUniqMatches = numberOfUniqMatches + 1
                totalMatches = totalMatches + 1
                range.HighlightColorIndex = wdYellow
        Loop
    End With
Next

Saerch for Byte in your code and replace it by Long . 在您的代码中使用Saerch for Byte并将其替换为Long Ctrl+H is the shortcut for Replace . Ctrl+HReplace的快捷方式。

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

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