How do I link code to a text box?(Execute a sub when the text shape is selected)

Started by PF4VisioGuy, February 12, 2020, 06:46:52 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

PF4VisioGuy

Hi guys

I have the following code, begginer code, not sure if there is better ways to do this
What I want is to create a shape text box that has text in it (name of a shape)
When I click on the shape I want the below code to be executed (basically read the shape.Text and then call the SelectShape() below which will find the page and the shape, it will switch the active page to that shape and select the shape

How do I link the below code to a text box shape? (or any other shape) THere should be an onClick() somewhere but I can't find it


Sub SelectShape(ShapeName As String)
    Dim page As Visio.page
    Dim vsoSelection As Visio.Selection
    Dim shp As Visio.Shape
    For Each page In ActiveDocument.Pages
        'Debug.Print (page.Name)
        For Each shp In page.Shapes
            If shp.Name = ShapeName Then
                'Debug.Print (shp.Name)
           'ActiveWindow.Page = ActiveDocument.Pages(page.Name)
                ActiveWindow.page = page.Name
                ActiveWindow.DeselectAll 'if you want to start a new selection
                ActiveWindow.Select shp, visSelect
                'Set vsoSelection = ActiveWindow.Selection
                'vsoSelection.Select shp, visSelect
                Exit For
            End If
        Next shp
    Next page
End Sub
Sub FindShape()
    ThisDocument.SelectShape ("TestShape")
End Sub

Croc

You can use Event Monitor to see all the events that occur at the moment you are interested. Event Monitor also allows you to use a filter to reduce the range of observed events.
Closest to the OnClick event are the MouseDown, MouseUp events. But also pay attention to the SelectionChanged event. I think this event will be the most convenient in your case.
SelectionChanged is a member of Application or Window objects.

PF4VisioGuy

12522 MouseDown 709;1;1;1.162500e+00;1.000000e+01;Drawing1
12525 VisioIsIdle
12529 SelectionChanged window[1] Drawing1 Page-3 subType is 128  [/hwnd=263602]

OK I am getting the above but how do I deal with that?
I  guess I need to add to my code to determine which is the selected shape, then read shape.Text and then call my Sub(shape.Text)
But where do connect the above with MouseDown?

PF4VisioGuy

I think that unknowingly I have been too restrictive here
What I need is a thing to click on which must hold the shape.Name text value which would be used by FindShape(shapename)
I guess a switch box will be fine
Ideally a drop down  list with search options (my lists will be very long)

Croc

I have attached a small example. You can see how it works.
The task is complicated by the fact that a special object is required that will provide the SelectionChanged event. To create this object, additional StartMonitoring and StopMonitoring procedures are used.
After starting StartMonitoring, the SelectionChanged handler will be active. Now this handler displays the shape text in the Immediate Window. You can replace the Debug.Print command with a call to your program.
Dim WithEvents win As Visio.Window

Private Sub StartMonitoring()
    Set win = ActiveDocument.Application.Window.Windows(1)
End Sub
Private Sub StopMonitoring()
    Set win = Nothing
End Sub

Private Sub win_SelectionChanged(ByVal Window As IVWindow)
    If Window.Selection.Count <> 1 Then Exit Sub            'incorrect selection
    If Window.Selection(1).NameID = "Sheet.1" Then Exit Sub 'this is button
    Debug.Print Window.Selection(1).Characters.Text
End Sub


Croc

But I realized that you really need a different solution :)
Now I'll think about how best to do it.

Croc

Watch the new version.
The shape contains a very long list of values ​​separated by a semicolon in cell Data1.
When the shape text is changed, the macro "filter" is triggered, which transfers only those values ​​that contain the shape text, to the Prop.Row_1.Format cell. In this case, the selection list is reduced.
After selecting a value in the Shape Data window, double-clicking on the shape displays the selected value (macro "sel"). You can run your macro instead.
Sub filter()
    s = ActiveWindow.Selection(1).Data1
    v = Split(s, ";")
    s1 = ""
    Set shp = ActiveWindow.Selection(1)
    s2 = shp.Characters.Text
    For i = LBound(v) To UBound(v)
        If InStr(1, v(i), s2) > 0 Then
            s1 = s1 & v(i) & ";"
        End If
    Next
    shp.Cells("Prop.Row_1.Format").Formula = Chr(34) & s1 & Chr(34)
End Sub

Sub sel()
    Set shp = ActiveWindow.Selection(1)
    MsgBox shp.Cells("Prop.Row_1").ResultStr(0)
End Sub


PF4VisioGuy

I sorted this out
Here is what I did
I created a ListBox2 control


Sub LoadContent()  'this loads the names of all the shapes in the document
    Dim page As Visio.page
    Dim shp As Visio.Shape
   
   
   
    ListBox2.Clear
    For Each page In ActiveDocument.Pages
        For Each shp In page.Shapes
            If InStr(1, shp.Name, "HHeader") = 1 Then
                ListBox2.AddItem (shp.Text)
            End If
        Next shp
    Next page
 

Sub ListBox2_Change()   'this gets the name of the selected shape
    Dim sel_item As String
    sel_item = ListBox2.Value
    ThisDocument.SelectShape (sel_item)
End Sub

Sub SelectShape(ShapeName As String)    'this selects the shape you want to see
    Dim page As Visio.page
    Dim vsoSelection As Visio.Selection
    Dim shp As Visio.Shape
    For Each page In ActiveDocument.Pages
     
        For Each shp In page.Shapes
            If shp.Name = ShapeName Then
               
                ActiveWindow.page = page.Name
                ActiveWindow.DeselectAll
                ActiveWindow.Select shp, visSelect
               
                Exit For
            End If
        Next shp
    Next page
End Sub