How to run a macro on single click (select)?

M

Michael Pollard

I want to run a macro (VBA subroutine) when an object is selected. I can work
with this if it works with any object or only with a specific object.

I can get it to work with a doubleclick (EventDblClick), but not singleclick
or select, at least not without getting into detailed mouse event processing.

I have a sub that creates a box behind the selected object, making it more
clearly selected. I want to have a different routine on double-click (pull
associated data from a database and display it as custom properties).

Any ideas?
 
A

Andy

The Visio 2003 SDK has an example of catching mouse events. I have used
this in my automated Visio application. You have to catch the mouse
click event, then spatial search to find the shapes at the mouse
position.
 
A

Andy

The Visio 2003 SDK has an example of catching mouse events. I have used
this in my automated Visio application. You have to catch the mouse
click event, then spatial search to find the shapes at the mouse
position.
 
M

Michael Pollard

That's more what I was looking for. Now just to figure out how it works...
Thank you very much.
 
M

Michel LAPLANE

Just more explanation as you work in VBA. Here is the VBA Code :

Dim WithEvents MyWindow As Visio.Window

Private Sub Document_RunModeEntered(ByVal doc As IVDocument)
Set MyWindow = ActiveWindow
End Sub

Private Sub MyWindow_SelectionChanged(ByVal Window As IVWindow)
MsgBox "The selection has changed"
End Sub

Save the document, close and reopen. When the document is opened the
RunModeEntered event fire thus declaring the MyWindow on which you can
further manage events.
 
M

Michael Pollard

Two answers in one! That also shows me how to initialize variables I need
elsewhere! (Which reduces error-checking/correcting code otherwise needed!)
It also answers a lot of other how-to questions... Like I said in another
post, I'm new to this. I've done some C and more COBOL (in school), but the
VB there was extremely elementary, and mostly just using the VB graphical
interface, not the text as VBA needs.

Thank you very much!
------------------------------
:

Just more explanation as you work in VBA. Here is the VBA Code :

Dim WithEvents MyWindow As Visio.Window

Private Sub Document_RunModeEntered(ByVal doc As IVDocument)
Set MyWindow = ActiveWindow
End Sub

Private Sub MyWindow_SelectionChanged(ByVal Window As IVWindow)
MsgBox "The selection has changed"
End Sub

Save the document, close and reopen. When the document is opened the
RunModeEntered event fire thus declaring the MyWindow on which you can
further manage events.
------------------------------
"Michael Pollard" <[email protected]> a écrit dans le
message de news: (e-mail address removed)...

That's more what I was looking for. Now just to figure out how it works...
Thank you very much.
------------------------------
:
track the SelectionChanged event
------------------------------
"Michael Pollard" <[email protected]> a écrit dans le
message de news: (e-mail address removed)...

I want to run a macro (VBA subroutine) when an object is selected. I can
work with this if it works with any object or only with a specific object.

I can get it to work with a doubleclick (EventDblClick), but not singleclick
or select, at least not without getting into detailed mouse event processing.

I have a sub that creates a box behind the selected object, making it more
clearly selected. I want to have a different routine on double-click (pull
associated data from a database and display it as custom properties).

Any ideas?
 
M

Michael Pollard

Sorry - a followup question:

When I tried the code as given: Dim WithEvents MyWindow As Visio.Window
I get: Compile error: Only valid in object module

I have it right at the top, right under my generic Dims for objects I reuse
in many functions. I tried both right after the "Option Explicit" and after
the rest and before the first Sub.

Sorry so many "newbie" questions...

------------------------------
:

Two answers in one! That also shows me how to initialize variables I need
elsewhere! (Which reduces error-checking/correcting code otherwise needed!)
It also answers a lot of other how-to questions... Like I said in another
post, I'm new to this. I've done some C and more COBOL (in school), but the
VB there was extremely elementary, and mostly just using the VB graphical
interface, not the text as VBA needs.

Thank you very much!
------------------------------
:

Just more explanation as you work in VBA. Here is the VBA Code :

Dim WithEvents MyWindow As Visio.Window

Private Sub Document_RunModeEntered(ByVal doc As IVDocument)
Set MyWindow = ActiveWindow
End Sub

Private Sub MyWindow_SelectionChanged(ByVal Window As IVWindow)
MsgBox "The selection has changed"
End Sub

Save the document, close and reopen. When the document is opened the
RunModeEntered event fire thus declaring the MyWindow on which you can
further manage events.
------------------------------
"Michael Pollard" <[email protected]> a écrit dans le
message de news: (e-mail address removed)...

That's more what I was looking for. Now just to figure out how it works...
Thank you very much.
------------------------------
:
track the SelectionChanged event
------------------------------
"Michael Pollard" <[email protected]> a écrit dans le
message de news: (e-mail address removed)...

I want to run a macro (VBA subroutine) when an object is selected. I can
work with this if it works with any object or only with a specific object.

I can get it to work with a doubleclick (EventDblClick), but not singleclick
or select, at least not without getting into detailed mouse event processing.

I have a sub that creates a box behind the selected object, making it more
clearly selected. I want to have a different routine on double-click (pull
associated data from a database and display it as custom properties).

Any ideas?
 
A

Andy

withevents can only be used in classes or the thisdocument (which is
also a class). You can't use these from ordinary modules.
 
M

Michael Pollard

So... What should I do? How do I put it in a class or thisdocument? You can
see the code Michel LAPLANE gave earlier - I am just trying to get it to work
as he described it.

(It may be related - the Document_RunModeEntered is't running either, but I
try to find the answers before I ask things, and I'm still looking.)
 
M

Michael Pollard

Answered my own question, after a little more trying.

When I created my macros, Visio put them in a new module called "NewMacros".

I got the ThisDocument window open and moved everything there, now it works.
------------------------------
:

Sorry - a followup question:

When I tried the code as given: Dim WithEvents MyWindow As Visio.Window
I get: Compile error: Only valid in object module

I have it right at the top, right under my generic Dims for objects I reuse
in many functions. I tried both right after the "Option Explicit" and after
the rest and before the first Sub.

Sorry so many "newbie" questions...

------------------------------
:

Two answers in one! That also shows me how to initialize variables I need
elsewhere! (Which reduces error-checking/correcting code otherwise needed!)
It also answers a lot of other how-to questions... Like I said in another
post, I'm new to this. I've done some C and more COBOL (in school), but the
VB there was extremely elementary, and mostly just using the VB graphical
interface, not the text as VBA needs.

Thank you very much!
------------------------------
:

Just more explanation as you work in VBA. Here is the VBA Code :

Dim WithEvents MyWindow As Visio.Window

Private Sub Document_RunModeEntered(ByVal doc As IVDocument)
Set MyWindow = ActiveWindow
End Sub

Private Sub MyWindow_SelectionChanged(ByVal Window As IVWindow)
MsgBox "The selection has changed"
End Sub

Save the document, close and reopen. When the document is opened the
RunModeEntered event fire thus declaring the MyWindow on which you can
further manage events.
------------------------------
"Michael Pollard" <[email protected]> a écrit dans le
message de news: (e-mail address removed)...

That's more what I was looking for. Now just to figure out how it works...
Thank you very much.
------------------------------
:
track the SelectionChanged event
------------------------------
"Michael Pollard" <[email protected]> a écrit dans le
message de news: (e-mail address removed)...

I want to run a macro (VBA subroutine) when an object is selected. I can
work with this if it works with any object or only with a specific object.

I can get it to work with a doubleclick (EventDblClick), but not singleclick
or select, at least not without getting into detailed mouse event processing.

I have a sub that creates a box behind the selected object, making it more
clearly selected. I want to have a different routine on double-click (pull
associated data from a database and display it as custom properties).

Any ideas?
 
M

Michel LAPLANE

Enjoy
Michael Pollard said:
Answered my own question, after a little more trying.

When I created my macros, Visio put them in a new module called
"NewMacros".

I got the ThisDocument window open and moved everything there, now it
works.
------------------------------
:

Sorry - a followup question:

When I tried the code as given: Dim WithEvents MyWindow As Visio.Window
I get: Compile error: Only valid in object module

I have it right at the top, right under my generic Dims for objects I
reuse
in many functions. I tried both right after the "Option Explicit" and
after
the rest and before the first Sub.

Sorry so many "newbie" questions...

------------------------------
:

Two answers in one! That also shows me how to initialize variables I need
elsewhere! (Which reduces error-checking/correcting code otherwise
needed!)
It also answers a lot of other how-to questions... Like I said in another
post, I'm new to this. I've done some C and more COBOL (in school), but
the
VB there was extremely elementary, and mostly just using the VB graphical
interface, not the text as VBA needs.

Thank you very much!
------------------------------
:

Just more explanation as you work in VBA. Here is the VBA Code :

Dim WithEvents MyWindow As Visio.Window

Private Sub Document_RunModeEntered(ByVal doc As IVDocument)
Set MyWindow = ActiveWindow
End Sub

Private Sub MyWindow_SelectionChanged(ByVal Window As IVWindow)
MsgBox "The selection has changed"
End Sub

Save the document, close and reopen. When the document is opened the
RunModeEntered event fire thus declaring the MyWindow on which you can
further manage events.
------------------------------
"Michael Pollard" <[email protected]> a écrit dans
le
message de news: (e-mail address removed)...

That's more what I was looking for. Now just to figure out how it works...
Thank you very much.
------------------------------
:
track the SelectionChanged event
------------------------------
"Michael Pollard" <[email protected]> a écrit dans
le
message de news: (e-mail address removed)...

I want to run a macro (VBA subroutine) when an object is selected. I can
work with this if it works with any object or only with a specific object.

I can get it to work with a doubleclick (EventDblClick), but not
singleclick
or select, at least not without getting into detailed mouse event
processing.

I have a sub that creates a box behind the selected object, making it more
clearly selected. I want to have a different routine on double-click (pull
associated data from a database and display it as custom properties).

Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top