News:

Happy New Year!

Main Menu

How do I use the SelectionChanged command?

Started by Hellogiraffe, October 30, 2015, 08:21:57 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Hellogiraffe

Hey everyone,

I am very new to Visio and trying to throw together an interactive fault tree that has been going well so far, but I hit a snag. I really want to move away from the double-click events and move to a single-click initiation instead. Everywhere I check, people recommend using the SelectionChanged command but I can't quite figure out exactly how to get it to work. It looks like it is supposed to constantly run in the background every time you click on something, but nothing happens for me.

What I need is to click once on a box (we can call it ID=1) and have it create a rectangle nearby with some text in it. With those basics, I'm sure I can figure out how to apply it to everything else. From what I've read, it should look something like the following:


Private Sub winObj_SelectionChanged(ByVal Window As IVWindow)
Private WithEvents winObj As Visio.Window   'People list this as the first line but I get an error when I try that?
Dim vsoShape1 As Visio.Shape
Dim TheShapes As Shapes
Dim shp As Shape
Dim vsoCharacters1 As Visio.Characters
Set TheShapes = ActivePage.Shapes
     For Each shp In TheShapes
          If shp.ID = "1" And ActiveWindow.Selection.Count = 1 Then
               Set vsoShape1 = ActivePage.DrawRectangle(1,2,3,4)
               Set vsoCharacters1 = vsoShape1.Characters
                    vso.Characters1.Text = "This is my super cool box. Thanks VisGuy!"
          End if
     Next shp
End Sub


If I just change it to "Sub Test()" and get rid of the WithEvents line, it runs perfectly fine so it has to be me not knowing how to get that SelectionChanged command to run. I'm trying to get my company to allow me to download the SDK for the Event Monitor to see if the SelectionChanged command is even firing, but until then, can anyone help me out with how that command works and what I'm doing wrong? Thank you so much!

JuneTheSecond

Please try to adjust your macro just a little.

Option Explicit

Private WithEvents winObj As Visio.Window   'People list this as the first line but I get an error when I try that?

Sub test()
    Set winObj = ActiveWindow
End Sub

Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
    Set winObj = ActiveWindow
End Sub

Private Sub winObj_SelectionChanged(ByVal Window As IVWindow)
Dim vsoShape1 As Visio.Shape
Dim vsoSel As Visio.Selection
Dim shp As Visio.Shape
Dim vsoCharacters1 As Visio.Characters

Set vsoSel = Window.Selection 'see above (ByVal Window As IVWindow)
     For Each shp In vsoSel
          If shp.ID = "1" And ActiveWindow.Selection.Count = 1 Then
               Set vsoShape1 = ActivePage.DrawRectangle(1, 2, 3, 4)
               Set vsoCharacters1 = vsoShape1.Characters
                    vsoCharacters1.Text = "This is my super cool box. Thanks VisGuy!"
          End If
     Next shp
End Sub


Best Regards,

Junichi Yoda
http://june.minibird.jp/

Hellogiraffe

That code kept giving me an error and I spent over 30 mins trying to figure out what was wrong, only to find out I spelled Rectangle incorrectly haha. Thanks so much for the help!

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 175 (show)
Files included: 34 - 1306KB. (show)
Memory used: 1072KB.
Tokens: post-login.
Cache hits: 15: 0.00292s for 26,587 bytes (show)
Cache misses: 5: (show)
Queries used: 17.

[Show Queries]