Visio Guy

Visio Discussions => Programming & Code => Topic started by: Thomas Winkel on July 04, 2015, 10:27:30 AM

Title: Connection Point: Change Row Type
Post by: Thomas Winkel on July 04, 2015, 10:27:30 AM
Hi,

I have the following code to change the row type of all connection points in a stencil:

Sub editMasters()
    Dim shp As Visio.Shape
    Dim mst As Visio.Master
    Dim cpy As Visio.Master
    Dim doc As Visio.Document
    Dim i As Integer
    Dim txt As String
   
    For Each doc In Application.Documents
        If Right(doc.Name, 8) = "EDIT.vss" Then
            Debug.Print doc
           
            For Each mst In doc.Masters
                Set cpy = Nothing
                Set cpy = mst.Open
                Set shp = cpy.Shapes(1)
               
                On Error Resume Next
               
                If shp.SectionExists(visSectionConnectionPts, False) Then
                    For i = 0 To shp.Section(visSectionConnectionPts).Count - 1
                        shp.RowType(visSectionConnectionPts, i) = visTagCnnctPtABCD
                        If Err.Number Then
                            Err.Clear
                            shp.RowType(visSectionConnectionPts, i) = visTagCnnctNamedABCD
                            txt = "Na"
                        Else
                            txt = "Pt"
                        End If
                    Next i
                End If
               
                If Err.Number Then
                    Err.Clear
                    Debug.Print "Err (" & txt; "): " & mst.Name
                Else
                    Debug.Print "OK (" & txt; "): " & mst.Name
                End If
               
                On Error GoTo 0
               
                Set shp = Nothing
                cpy.Close
                Set cpy = Nothing
                Set mst = Nothing
            Next
        End If
    Next
End Sub

When I step through the code in debug mode it works, with some random, not reproduceable "Err" messages.
But when I start it normally it crashes immediately with a Run-time error: An exception occurred.
When I try to change the row type by hand in the shapesheet then I receive a "Unexpected end of file" error message.

Any ideas?

Best regards,
Thomas



Title: Re: Connection Point: Change Row Type
Post by: JuneTheSecond on July 04, 2015, 12:50:56 PM
Is it not old drawing that no supports D cell?
Title: Re: Connection Point: Change Row Type
Post by: Thomas Winkel on July 04, 2015, 05:42:55 PM
It's Visio 2010. Also the stencil was created with this version.

But why does it work in debug mode?
This is really strange, I never had such a behavior before.

Also I don't really understand the difference between "visTagCnnctPtABCD" and "visTagCnnctNamedABCD".

And why do I have to change the "Type" of a row to enable a cell?
Why isn't this cell just enabled by default?

Best regards,
Thomas
Title: Re: Connection Point: Change Row Type
Post by: wapperdude on July 04, 2015, 09:12:33 PM
Indeed, why are there two row types?   ???   Eureka!!!   ;)  To make some of us look smart!   :o.  >:( 

I think the named row and "D" cell are under-utilized.

Wapperdude
Title: Re: Connection Point: Change Row Type
Post by: JuneTheSecond on July 04, 2015, 11:51:17 PM
My simple skeleton macro works fine.
Any other parts may be worth checking.

Sub test()
    Dim shp As Visio.Shape
    Set shp = ActiveWindow.Selection(1)
    shp.CellsSRC(visSectionConnectionPts, 0, visCnnctX).RowNameU = "Test"
    shp.RowType(visSectionConnectionPts, 0) = visTagCnnctNamedABCD
    shp.CellsSRC(visSectionConnectionPts, 0, visCnnctD).FormulaU = "PNT(LocPinX,LocPinY)"
End Sub
Title: Re: Connection Point: Change Row Type
Post by: wapperdude on July 05, 2015, 01:45:38 AM
Thomas,

I think your problem is here:  For i = 0 To shp.Section(visSectionConnectionPts).Count  .  That doesn't count the rows in the connects section as far as I can tell.  See https://msdn.microsoft.com/en-us/library/office/ff765894.aspx (https://msdn.microsoft.com/en-us/library/office/ff765894.aspx)

The format should be something like:  intRows = vsoShape.RowCount(Visio.visSectionProp)

So, taking a little from you, a little from JuneTheSecond, and a little from me, your code ought to look something like:
Sub test()
    Dim shp As Visio.Shape
    Dim visPg As Page
   
    For Each visPg In ActiveDocument.Pages
        Set shp = visPg.Shapes(1)
        If shp.SectionExists(visSectionConnectionPts, False) Then
            For i = 0 To shp.RowCount(visSectionConnectionPts) - 1
                shp.CellsSRC(visSectionConnectionPts, i, visCnnctX).RowNameU = "Test" & i
                shp.RowType(visSectionConnectionPts, i) = visTagCnnctNamedABCD
                shp.CellsSRC(visSectionConnectionPts, i, visCnnctD).FormulaU = "PNT(LocPinX,LocPinY)"
            Next
        End If
    Next
End Sub


HTH
Wapperdude
Title: Re: Connection Point: Change Row Type
Post by: JuneTheSecond on July 05, 2015, 11:53:29 PM
Thomas, your code works fine in my Visio 2013.
I made and added a new stencil named EDIT.vss to my drawing that has your macro, and run your macro.
I think your code is complete.
Title: Re: Connection Point: Change Row Type
Post by: Thomas Winkel on July 12, 2015, 12:15:14 PM
Thanks for your effort, Wapperdude and Junichi.

I've uploaded a video that shows the effect without code:
https://youtu.be/DLUQUaMvhU4 (https://youtu.be/DLUQUaMvhU4)

This does not appear for all shapes.
And when I do the same directly in the stencil (Edit Master Shape) it works for some shapes that failed in the document, but not for all.
I'm not able to locate the problem, because all shapes (including the connection points) were generated by the same code.
And for some of them it is working and for others not.

Now I re-generated all shapes with the D-Cell activated.

Best regards,
Thomas