Question and help, Connected shapes and setting colors (vba)

Started by lrdgrd, August 06, 2014, 03:27:54 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

wapperdude

@ Yacine

1.  Correct.  Don't need to iterate thru all pages.  Last code submission stopped doing that.
2.  Actually, was trying to do what Jumpy said.  You actually clarified what needed to change in that last code update.
3.  In your code, the last loop takes care of everything, lines and connectors.  Doesn't seem like the 1st loop, which you labeled for connectors is necessary.
4.  Modified my code to include your changes, plus allow change of z-order.  Note, this code will pop all highlighted shapes to the top.

Updated code is below.  Also attached simple Visio test file.  The purple line is a simple 1D line shape, all others are dynamic connectors.  The code properly identifies the shape and connections. 

Sub FindConnections()
'User selects a shape on a page.  The selected shape gets highlighted.
'Code iterates thru the connector collection and finds which are connected to the shape
'Identified connector is raised to the front z-order and its color is changed.
'Shapes connected to opposite end are highlighted, same color as connectors.
'Uses universal parameter, HiClr, to set highlighting color via RGB values.

    Dim i As Integer
    Dim HiClr As String
    Dim ConsObj As Visio.Connect
    Dim ConShp As Visio.Shape
    Dim selshp As Visio.Shape
   
   
    HiClr = "RGB(0,0,255)"                                                  'Set the desired highlight color
   
    If Visio.ActiveWindow.Selection.Count = 0 Then
        MsgBox "Select a shape first"                                       'Check for selection, if fails, bailout
    Else
        Set selshp = Visio.ActiveWindow.Selection(1)
        selshp.CellsSRC(visSectionObject, visRowLine, visLineColor).FormulaU = HiClr
       
        For i = 1 To selshp.FromConnects.Count
            Set ConShp = selshp.FromConnects(i).FromSheet
            ConShp.CellsSRC(visSectionObject, visRowLine, visLineColor).FormulaU = HiClr
            For Each ConObj In ConShp.Connects
                Set ConShp = ConObj.ToSheet
                ConShp.CellsSRC(visSectionObject, visRowLine, visLineColor).FormulaU = HiClr
                ConShp.BringToFront
            Next ConObj
        Next i
    End If
End Sub


Wapperdude
Visio 2019 Pro

wapperdude

Ah.  This version only pops the connections:

Sub FindConnections()
'User selects a shape on a page.  The selected shape gets highlighted.
'Code uses the connects collection for the shape, and then searches each connector for shape connected other end
'Identified connector is raised to the front z-order and its color is changed.
'Shapes connected to opposite end are highlighted, same color as connectors.
'Uses universal parameter, HiClr, to set highlighting color via RGB values.

    Dim i As Integer
    Dim HiClr As String
    Dim ConObj As Visio.Connect
    Dim FromConShp As Visio.Shape
    Dim ToConShp As Visio.Shape
    Dim selShp As Visio.Shape
   
    HiClr = "RGB(0,0,255)"                                                  'Set the desired highlight color
   
    If Visio.ActiveWindow.Selection.Count = 0 Then
        MsgBox "Select a shape first"                                       'Check for selection, if fails, bailout
    Else
        Set selShp = Visio.ActiveWindow.Selection(1)
        For i = 1 To selShp.FromConnects.Count                              'Get connectors, lines glued to selected shape
            Set FromConShp = selShp.FromConnects(i).FromSheet
            FromConShp.CellsSRC(visSectionObject, visRowLine, visLineColor).FormulaU = HiClr
            FromConShp.BringToFront
            For Each ConObj In FromConShp.Connects                          'Get shape connected to opposite end
                Set ToConShp = ConObj.ToSheet
                ToConShp.CellsSRC(visSectionObject, visRowLine, visLineColor).FormulaU = HiClr
            Next ConObj
        Next i
    End If
End SubEnd Sub


Again, thanks Yacine for showing the syntax use of the FromConnects and ToConnects. 
Visio 2019 Pro

wapperdude

Finally, (yeah, some of us just can't let go), if desire different highlight color for the selected shape:

Sub FindConnections()
'User selects a shape on a page.  The selected shape gets highlighted.
'Code uses the connects collection for the shape, and then searches each connector for shape connected other end
'Identified connector is raised to the front z-order and its color is changed.
'Shapes connected to opposite end are highlighted, same color as connectors.
'Uses universal parameter, HiClr, to set highlighting color via RGB values.
'Modified highlight color so selected shape stands out from others

    Dim i As Integer
    Dim HiClr As String
    Dim ConObj As Visio.Connect
    Dim FromConShp As Visio.Shape
    Dim ToConShp As Visio.Shape
    Dim selShp As Visio.Shape
   
   
    HiClr = "RGB(0,0,255)"                                                  'Set the desired highlight color
   
    If Visio.ActiveWindow.Selection.Count = 0 Then
        MsgBox "Select a shape first"                                       'Check for selection, if fails, bailout
    Else
        Set selShp = Visio.ActiveWindow.Selection(1)
        selShp.CellsSRC(visSectionObject, visRowLine, visLineColor).FormulaU = "RGB(0,180,0)"   'Highlight selected shape
       
        For i = 1 To selShp.FromConnects.Count                              'Get connectors, lines glued to selected shape
            Set FromConShp = selShp.FromConnects(i).FromSheet
            FromConShp.CellsSRC(visSectionObject, visRowLine, visLineColor).FormulaU = HiClr
            FromConShp.BringToFront
            For Each ConObj In FromConShp.Connects                          'Get shape connected to opposite end
                Set ToConShp = ConObj.ToSheet
                If ToConShp <> selShp Then                                  'Skip selected shape; already colored.
                    ToConShp.CellsSRC(visSectionObject, visRowLine, visLineColor).FormulaU = HiClr
                End If
            Next ConObj
        Next i
    End If
End Sub
Visio 2019 Pro

Yacine

Good job. Now, just add a non-modal dialog with 2 buttons - "highlight" and "off" and Irdgrd will be fully satisfied.
Yacine

lrdgrd

This is far more than what i could have asked from you guys! :)  Thanks again!

After seeing how to do some looping with shapes, selections and stuff... i started doing my own stuff for another thing i needed to do! and for now it works!  Opening an excel file and comparing the shape text to what is in the excel and applying a shape fill depending on the color code assigned in the excel file!

This is keeping me entertained these long work night! :)
lrdgrd (LRDGRD)
One of Seven!

wapperdude

@Yacine:  Not sure what the actual term "non-modal" means, but, I get the intent.  Since this code was a defacto collaborative effort, be my guest to add as many non-modal buttons as needed. 

Wapperdude
Visio 2019 Pro

Yacine

A non-modal dialog is one that does not block the underlaying application. It stays open and ready to do whatever it is intended for, but you can still work in the application as if the dialog was not there.
Yacine

lrdgrd

I wouldnt really worry about it anymore!  I got it working, did some tweaks and it perfect for what i need it!  Have some buttons in place and im chaning the colors and line width with it.  The reset used the same function but sets the color back to black and line width to 1. 

lrdgrd (LRDGRD)
One of Seven!