“VBA CorelDraw”不按顺序重命名对象 A-Z

问题描述 投票:0回答:1

我创建了一个脚本来根据字母顺序重命名 CorelDraw 中的对象。该脚本应该按预期工作,但我不知道为什么顺序是从 Z 到 A。 Screenshots on the left show the code functioning as intended.

Sub RenameSelectedObjects()
    Dim i As Integer
    Const START_CHAR = 65 ' ASCII untuk 'A'
    Const END_CHAR = 90   ' ASCII untuk 'Z'
    Dim objCount As Integer
    Dim selectedShape As Shape
    
    i = 0
    objCount = ActiveSelectionRange.Count
    
    For Each selectedShape In ActiveSelectionRange
        If i > END_CHAR - START_CHAR Then Exit Sub
        selectedShape.Name = Chr(START_CHAR + i)
        i = i + 1
    Next selectedShape
End Sub

但是,如果“对象作为一个整体”通过此脚本成功,那就是另一回事了。

Sub RenameObjects()
    Dim i As Integer
    Const START_CHAR = 65 ' ASCII untuk 'A'
    Const END_CHAR = 90   ' ASCII untuk 'Z'
    
    i = 0
    For Each s In ActivePage.Shapes.All
        If i > END_CHAR - START_CHAR Then Exit Sub
        s.Name = Chr(START_CHAR + i)
        i = i + 1
    Next s
   
End Sub

有谁可以帮助我使脚本仅适用于选定的对象并保持它们从 A 到 Z 的顺序吗?

vba rename draw coreldraw
1个回答
0
投票
  • 由于我没有CorelDRAW软件,所以我不确定这个问题是否与用户选择多个形状的顺序有关。
  • 参考你的截图,代码可以修改如下。
Sub RenameSelectedObjects()
    Dim i As Integer
    Const START_CHAR = 65 ' ASCII untuk 'A'
    Const END_CHAR = 90   ' ASCII untuk 'Z'
    Dim objCount As Integer
    Dim selectedShape As Shape
    i = 0
    objCount = ActiveSelectionRange.Count
    If objCount > END_CHAR - START_CHAR Then objCount = END_CHAR - START_CHAR ' **
    For Each selectedShape In ActiveSelectionRange
        selectedShape.Name = Chr(START_CHAR + objCount - i) ' **
        i = i + 1
        If i > END_CHAR - START_CHAR Then Exit Sub
    Next selectedShape
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.