This macro works by selecting 2 objects. The object that has been previously named is called 'A', and the other object's name is RANDOM. I hope that with this macro, I can change the name of the selected object. If an object named A is found, then the other object is renamed to '1'. Similarly, if an object named B is found, then the other object is renamed to '2', and so on
Sub ChangeObjectName()
Dim sr As ShapeRange
Dim s As Shape
Set sr = ActiveSelectionRange
If sr.Count <> 2 Then Exit Sub
For Each s In sr
If s.Name <> "A" Then
s.Name = "1"
End If
Next s
End Sub
(https://i.sstatic.net/37n6h.png)
I tried to develop the script to be like this,
Sub ChangeObjectName()
Dim sr As ShapeRange
Dim s As Shape
Set sr = ActiveSelectionRange
If sr.Count <> 2 Then Exit Sub
For Each s In sr
If s.Name <> "A" Then
s.Name = "1"
If s.Name <> "B" Then
s.Name = "2"
If s.Name <> "C" Then
s.Name = "3"
If s.Name <> "D" Then
s.Name = "4"
End If
Next s
End Sub
However, encountering an error message, Compile Error, Next without For
I've also attempted looping through the context, but encountered an issue where the object names didn't change as expected. I would greatly appreciate it if someone could assist me in resolving this script. Thank you.
coreldraw
sw. It is revised as the VBA logic.Sub ChangeObjectName()
Dim sr As ShapeRange
Dim s As Shape, s2 As Shape, s1 As Shape
Const START_NAME = "A"
Const END_NAME = "D"
Set sr = ActiveSelectionRange
If sr.Count <> 2 Then
MsgBox "Please select two shapes"
Exit Sub
End If
For Each s In sr
If Len(s.Name) = 1 And s.Name >= START_NAME And s.Name <= END_NAME Then
Set s1 = s
Else
Set s2 = s
End If
Next s
If s1 Is Nothing Or s2 Is Nothing Then
MsgBox "Can't get the shape " & IIf(s1 Is Nothing, "s1", "s2")
Exit Sub
Else
s2.Name = CStr(Asc(s1.Name) - 64)
MsgBox "New shape name is " & s2.Name
End If
End Sub