Mac上Microsoft Word的VBA中的多维数组 [英] Multi-dimensional array in VBA for Microsoft Word on Mac
问题描述
我正在研究一个宏,以遍历一系列字符串(a1, a2, a3
)并将其替换为一系列相应的值(b1, b2, b3
).我创建了一个数组来存储要匹配的字符串:
I'm working on a macro to loop through a series of strings (a1, a2, a3
) and replace them with a series of corresponding values (b1, b2, b3
). I've created an array to store the strings to match:
Dim search_strings(1 To 2) As String
search_strings(1) = "match1"
search_strings(2) = "match2"
我可以使用For Each
循环遍历此数组.但是我不知道如何存储和引用相应的替换文本.我知道我需要某种键/值对.我已经尝试过使用字典,像这样:
I can loop through this array with a For Each
loop. But I can't figure out how to store and reference the corresponding replacement text. I know that I need some sort of key/value pair. I've tried using a dictionary, like this:
Dim dict As New Scripting.Dictionary
dict.Add "match", "replace"
但是要使其正常工作,我需要引用Microsoft Scripting Runtime,该脚本在Mac OS X中不可用.(当前,出现此错误:Compile error: User-defined type not defined
.)
But for that to work, I need to reference Microsoft Scripting Runtime, which isn't available on Mac OS X. (Currently, I get this error: 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
推荐答案
这可能是完全不正确的,我看起来像个傻瓜,但是您尝试过使用二维数组,以便可以将值及其替换项存储在两个数组中维数组.然后,您可以遍历以获得替换值.那只是我的一个想法,可能还很遥远.
This may be completely off and I may look like a fool but have you tried using a two dimensional array so you can store the values with their replacements in the two dimensional array. Then you can loop through to get the replacement values. That is just an idea I had, it could be way off.
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
我真的不确定这是否是您要寻找的东西,如果您在浪费时间,我深表歉意.
Im really not sure if this is what you are looking for so I apologize if I am wasting your time.
这篇关于Mac上Microsoft Word的VBA中的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!