1

I'm trying to build a small nesting Function inside of my current working system. What I am having trouble with is that when I add the third shape to the collection and try to position it based on the previous shape added to the collection, it still positions it based on the very first shape. What I end up with is the original in the original position and the copies stacked on top of one another.

Function ArrangeImages(ByRef scol1 As Collection, ByRef sA, sB As Shape)
Dim i, ii As Long

i = scol1.Count
ii = i - 1

If i = 1 Then

Set sB = scol1.Item(i)

End If

If scol1.Count > 1 Then

Set sA = scol1.Item(ii)
Set sB = scol1.Item(i)

sB.SetPosition sA.PositionX, sA.PositionY + (sA.SizeHeight / 2) + 
(sB.SizeHeight / 2) + 0.15
End If

End Function
James Cunningham
  • 121
  • 1
  • 12

1 Answers1

1

You are setting the objects equal to each other and they reference each other in memory. In fact, you are doubling the object and using up twice as much memory.

Set sets an object equal to the reference of the object you want it to be. Here is an example.

Public Sub test()

    Dim s As Worksheet
    Dim s2 As Worksheet

    Set s = Application.Workbooks.Add().Worksheets.Add
    s.Name = "one"
    Set s2 = s     's and s2 Name = "one"
    s.Name = "two" 's and s2 Name = "two"

End Sub

Let sets an object equal to the value of the object you want it to be.

Try

Set sA = new Shape
Let sA = sB 

Instead of

Set sA = sB

If that doesn't work, you may have to create a property or variable for each of the shapes you are wanting to work with.

Similar Question

interesting-name-here
  • 1,851
  • 1
  • 20
  • 33
  • I made an edit to the code to have sA set differently I tested this but it still did not work correctly. I have not yet explored the Let method. Allow me to do that and get back with you. – James Cunningham May 24 '17 at 20:36
  • @JamesCunningham as long as you are `Set`ting it, it will still be a referenced object. Even if you `Set sC = sB` then `Set sA = sC` then `sA` will still be a reference of `sB`. I don't know what you've tried, just providing that as an assumption. – interesting-name-here May 25 '17 at 12:47
  • I wound up having to create a double to hold the position of the objects before they were repositioned. I also had to use the StaticID of the object for Corel to access the correct ones and place them where they should go. Ultimately my code looks quite different from the original, though @GibralterTop had a very useful post. Thanks. – James Cunningham May 25 '17 at 20:46
  • @JamesCunningham glad you were able to figure it out. I think maybe you should post edited code as answer instead of editing your question. – interesting-name-here May 26 '17 at 04:35