What's Wrong Here eh?

Started by wapperdude, August 19, 2023, 08:02:23 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

wapperdude

Not sure why Forum has been so quiet.  Popped over to StackOverflow, and while it seems to have bit more activity, not great there either.  Well, I stumbled upon this piece of code submitted by the OP.  The OP stated that he wanted to modify some properties of arrow connector, ran the macro recorder, and then edited the results.  When he ran the macro, nothing changed.  Well, what surprised me was that no had viable answer.  To me, it was obvious that his editing introduced some fatal errors.  Add to that, the specific changes desired were never identified, so only the code provides a clue to that.

Below is the code copied from SO.  I removed all the undoscope stuff that the macro recorder so lavishly provides.  Do you see the error?

Sub Macro1(shapeID As Long)

    Application.ActiveWindow.Page.Shapes.ItemFromID(shapeID).CellsSRC(visSectionObject, visRowXFormOut, visXFormWidth).FormulaForceU = "GUARD(EndX-BeginX)"
    Application.ActiveWindow.Page.Shapes.ItemFromID(shapeID).CellsSRC(visSectionObject, visRowXFormOut, visXFormHeight).FormulaForceU = "GUARD(EndY-BeginY)"
    Application.ActiveWindow.Page.Shapes.ItemFromID(shapeID).CellsSRC(visSectionFirstComponent, 2, 0).FormulaForceU = "0.6024533433537" & " in"
    Application.ActiveWindow.Page.Shapes.ItemFromID(shapeID).CellsSRC(visSectionFirstComponent, 3, 0).FormulaForceU = "0.6024533433537" & " in"

End Sub


Visio 2019 Pro

wapperdude

As a hint, I believe there are 2 fatal errors and 3 lines of code that accomplish nothing.
Visio 2019 Pro

Yacine

I cheated and copied the code in the VBA IDE. It run without error.
I wouldn't write the code in this way though (No checking for 1D, cellsSRC)
I will need 2nd hint.
Yacine

wapperdude

#3
Really?  It would Not run at all from VBA window in Visio.  Did not like that  "sub" definition line.  First, syntax seems wrong, 2nd, as stand-alone macro, there ought to be nothing inside the parentheses.

How does the code grab a shape since  the shapeID is variable and would not get assigned/passed into the sub as a stand-alone macro? 
Visio 2019 Pro

Yacine

I did of course write another macro that called yours with the id of the selected Shape. I made also sure to select a 1d shape.
Yacine

wapperdude

Ok.  That makes sense.  Not sure the OP ran the macro that way...very little info...but plausible.

This whole thing begs the issue regarding rerouting dynamic connectors as those are under the pervasive guidance from within Visio.
That being said...
1) while nothing wrong with 1st two executable lines, they seem redundant, as the connector shapesheet seems to default guarding width and height.
2a) in the case of a straight connector, the 4th executable code line would refer to non-existent row 3 ...error
2b) likewise, in the preceding executable line, row 2 refers to end point in such scenario and does nothing.


Visio 2019 Pro

Paul Herber

ActiveWindow might be a problem in some scenarios.
Application.ActivePage would be better.
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

Visisthebest

I didn't know the answer but these Visio puzzles are nice Wapperdude!

Paul sounds like a good tip, so I understand Application.ActivePage might be the more solid choice to get the selection the user made.
Visio 2021 Professional

Yacine

Activepage has no direct selection property. I guess you would need to access the window of the activepage?
Yacine

Yacine

But still, what is the OP really asking for?
Yacine

wapperdude

Regarding the OP...
Other than the original posting, there was no additional information provided.  Thus, merely he would like to change connector properties and the macro doesn't do anything.  Thought I'd grab/post it to break the boredom around here.

The response has been far more detailed and interesting than on StackOverflow!
Visio 2019 Pro

wapperdude

Decided to pursue Yacine's response, as the subroutine definition syntax bothered me.  Here's what I found.  Passing a variable can be done ByReference which is default or ByValue which must be declared.  Why by value?

by value
A way of passing the value of an argument to a procedure instead of passing the address. This allows the procedure to access a copy of the variable. As a result, the variable's actual value can't be changed by the procedure to which it is passed. 

So ran a test of the various syntax options.  In this case, all ran.  In the code, as previously provided, the ByValue key word is omitted, so I would conclude that it is done by default reference.  Thus the As Long is unnecessary.  Below is my test code for these options.

Sub selectIt()
    Dim vShp As Visio.Shape
   
    Set vShp = ActiveWindow.Selection(1)
'Two valid methods to call a macro and pass variable
    Call Macro1(vShp.ID)
'    Macro1 vShp.ID

End Sub
'Sub Macro1(shapeID As Long)              'default, by reference           '
'Sub Macro1(ByVal shapeID As Long)     'declared, by value
Sub Macro1(shapeID)                           'default by reference
'Either of the above 3 subroutine definitions is valid
'The 3rd one uses ByRef, the default
'Below, the  keyword Application may be deleted

    Application.ActiveWindow.Page.Shapes.ItemFromID(shapeID).CellsSRC(visSectionObject, visRowXFormOut, visXFormWidth).FormulaForceU = "GUARD(EndX-BeginX)"
    Application.ActiveWindow.Page.Shapes.ItemFromID(shapeID).CellsSRC(visSectionObject, visRowXFormOut, visXFormHeight).FormulaForceU = "GUARD(EndY-BeginY)"
    Application.ActiveWindow.Page.Shapes.ItemFromID(shapeID).CellsSRC(visSectionFirstComponent, 2, 0).FormulaForceU = "0.6024533433537" & " in"
    Application.ActiveWindow.Page.Shapes.ItemFromID(shapeID).CellsSRC(visSectionFirstComponent, 3, 0).FormulaForceU = "0.6024533433537" & " in"

End Sub
Visio 2019 Pro