vbarenamedrawcoreldraw

"VBA CorelDraw "Rename Object A-Z not in order


I have created a script to rename objects in CorelDraw according to alphabetical order. This script should work as expected, but I don't know why the sequence is from Z to 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

But it's another matter if the 'Object as a whole' succeeds with this script.

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

Is there anyone who can assist me in making the script work only on the selected objects and maintaining their order from A to Z?


Solution

  • 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