VBA中用于Mac上的Microsoft Word的多维数组

时间:2015-04-20 18:24:21

标签: vba replace ms-word word-vba-mac

我正在处理一个宏来循环遍历一系列字符串(a1, a2, a3)并用一系列相应的值(b1, b2, b3)替换它们。我创建了一个数组来存储要匹配的字符串:

Dim search_strings(1 To 2) As String
search_strings(1) = "match1"
search_strings(2) = "match2"

我可以使用For Each循环遍历此数组。但我无法弄清楚如何存储和引用相应的替换文本。我知道我需要某种键/值对。我尝试过使用字典,如下所示:

Dim dict As New Scripting.Dictionary
dict.Add "match", "replace"

但为了实现这一点,我需要引用Microsoft Scripting Runtime,这在Mac OS X上是不可用的。(目前,我收到此错误:Compile error: User-defined type not defined。)

还有其他办法吗?

这里是完整的代码:

Sub MyMacro()
    ' Initialize variables
    Dim search_strings(1 To 2) As String
    Dim this_search_string As Variant
    Dim myRange As Range
    Dim Reply As Integer
    ' Define strings to match
    search_strings(1) = "match1"
    search_strings(2) = "match2"
    ' Run a search for each string in the array of strings to match
    For Each this_search_string In search_strings
        ' Define the search range to be the whole document
        Set myRange = ActiveDocument.Content
        ' Set the Find parameters
        myRange.Find.ClearFormatting
        myRange.Find.MatchWildcards = True
        ' Loop through each match in the document
        Dim cached As Long
        cached = myRange.End
        Do While myRange.Find.Execute(this_search_string)
            myRange.Select
            ' Prompt the user to replace the match
            Reply = MsgBox("Replace '" & myRange.Find.Text & "'?", vbYesNoCancel)
            If Reply = 6 Then ' "Yes" clicked
                myRange.Text = "replacement"
            ElseIf Reply = 2 Then ' "Cancel" clicked
                Exit Do
            End If
            myRange.Start = myRange.Start + Len(myRange.Find.Text)
            myRange.End = cached
        Loop
    Next this_search_string
End Sub

1 个答案:

答案 0 :(得分:1)

这可能完全关闭,我可能看起来像个傻瓜但是你尝试过使用二维数组,这样你就可以将它们的替换值存储在二维数组中。然后,您可以循环访问以获取替换值。这只是我的一个想法,它可能会离开。

Sub MyMacro()
    ' Initialize variables
    Dim search_strings(1 To 2, 1 to 2) As String
    Dim this_search_string As Variant
    Dim myRange As Range
    Dim Reply As Integer
    ' Define strings to match
    search_strings(1, 1) = "match1"
    search_strings(2, 1) = "match2"
    search_strings(1, 2) = "result"
    search_strings(2, 2) = "result"
    ' Run a search for each string in the array of strings to match
    For Each this_search_string In search_strings
        ' Define the search range to be the whole document
        Set myRange = ActiveDocument.Content
        ' Set the Find parameters
        myRange.Find.ClearFormatting
        myRange.Find.MatchWildcards = True
        ' Loop through each match in the document
        Dim cached As Long
        cached = myRange.End
        Do While myRange.Find.Execute(this_search_string)
            myRange.Select
            ' Prompt the user to replace the match
            Reply = MsgBox("Replace '" & myRange.Find.Text & "'?", vbYesNoCancel)
            If Reply = 6 Then ' "Yes" clicked
                myRange.Text = search_strings(1, 2)
            ElseIf Reply = 2 Then ' "Cancel" clicked
                Exit Do
            End If
            myRange.Start = myRange.Start + Len(myRange.Find.Text)
            myRange.End = cached
        Loop
    Next this_search_string
End Sub

我真的不确定这是不是你要找的,所以如果我浪费你的时间,我道歉。