我为 CorelDraw 编写了一个 VBA 宏,它循环遍历选定的对象并以字符串形式返回值。我希望它能够更好地处理重复数据,例如,如果两个对象的大小相同,它应该返回
"2 of 10 x 10"
代替
"1 of 10 x 10"
"1 of 10 x 10"
来自 Ruby(特别是哈希),我认为宏应该循环遍历选定的范围,将(object.sizeWidth、object.sizeHeight)数据作为字符串添加到数组/字典中,然后检查重复项并进行计数。我不知道什么是最好的,也不知道如何设置/检查它们的值。
这是我目前的代码
Sub objectsToString()
Dim str As String
Dim v As Shape, vr As ShapeRange
Dim xSize#, ySize#
Dim dupCount As Integer
str = ""
Set vr = ActiveSelectionRange
For Each v In vr
dupCount = 'value assigned via iteration
xSize = v.SizeWidth
ySize = v.SizeHeight
str = str & dupCount & " of " & xSize & " x " & ySize & vbNewLine
Next v
End Sub
答案1
我自己没有 CorelDraw,但这里有一个可比较的例子:
' Set up some sample items
Dim items(2) As String
items(0) = "10 x 10"
items(1) = "20 x 20"
items(2) = "10 x 10"
' Create a dictionary to store the items and count
' Key: [n] x [n]
' Value: Count of item
Dim dict As New Scripting.Dictionary
For Each Item In items
If dict.Exists(Item) Then
' Increase existing count
dict(Item) = dict(Item) + 1
Else
' Add new item to dictionary and set count to 1
dict.Add Item, 1
End If
Next
' Print dictionary
For Each Key In dict.Keys
Debug.Print dict(Key) & " of " & Key
Next
如果你还没有这样做,你需要添加对Microsoft 脚本运行时在工具 → 引用对话框中找到库。如果该库不在列表中,请使用浏览按钮选择C:\Windows\System32\scrrun.dll
。