How to place userForm1 neatly beside a Visio.shape at any level of screen zoom.

Kelvin4 used Ask the Experts™
I need to interact with smart tags of individual visio.shapes to call MACRO, which will in turn:
 - capture the identity of that individual shape (eg as a shapeID, objName, or via a shapesheet value).
 - open userForm1, adjacent to that individual shape, at the current level of ZOOM for laptop and extension screen.
 - UserForm1 receives input data affecting visio.shape properties and output data to a bound Excel wkbk.

A model sub in Excel showed ZOOM level affected whether UserForm1 launched adjacent to the visio.shape. The model used an ActiveX button 'CmdButton1' as a model shape, which I used to call this sub to position userform1 close to CmdButton1. Different zoom levels required different values of crn (correction) in this sub:
Sub CmdButton1_Click()
        Dim crn As Double
        crn = 0.6
        UserForm1.left = Application.left + (Me.Shapes("CmdButton1").left * crn) + (UserForm1.Width / 2) = + (Me.Shapes("CmdButton1").top * crn) + (UserForm1.Height / 2)
End Sub

For different zoom levels, the optimal value of crn was judged by trial and error so UserForm1 remained at a const posn from CmdButton1,  no matter the button was at the left or right hand side of the sheet, on the laptop screen or the extension screen. For zoom of 60%, and crn = 0.6; for zoom value 50% crn = 0.498; for zoom value 70% crn = 0.78.

2. I do not know how to automate the creation the correct crn value for the current value of ZOOM.

I am aware of API methods to convert mouse click coordinates to UserForm posn coordinates:, but is that the method of choice?
I also read "Understanding Screen Scaling Issues" but am uncertain of its relevance

3. Application.left & (see sub above) dont exist in Visio and I've not identified the equivalent Visio variable. In visio, how do I work around this to position UserForm1 next to CmdButton1?

[THE FALL-BACK is to abandon the UserForm in favour of a visio.shape, engineered to serve a similar purpose to userForm1. This WOULD simplify positioning  (via "PinX", "PinY" etc) , BUT it is a less attractive alternative to a userForm. It is harder to construct, has a more limited repertoir of ctls, and is a less convenient interface for the User.]
Hopefully, this is all subject to a well-worked solution, which is unknown to me. Otherwise, I'd be glad of advice as to where I should compromise in design to enable a practicable solution.

Competency-wise, I'm familar with handling control events via VBA classes, but by contrast would need step-by-step recipes to work with AddOns!

Many thanks
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Scott HelmersVisio Consultant, Trainer, Author, and Developer
Most Valuable Expert 2011

A couple of suggestions:
-- take a look in the Visio SDK for the Window.GetViewRect Method. It returns the coordinates of the specified window.
-- for crn value, a simple technique is to use two arrays (or a 2D array) to hold the zoom levels and corresponding crn values. When you determine the current zoom, select the closest zoom level in array 1, and then lookup the corresponding crn value in array 2. It's not elegant, and you're limited to the list of experimental values you've gathered, but you can probably generate other values by extrapolating from what you know. (You could also use a dictionary instead of a pair of arrays.)


Many thanks for speedy help..

Window.GetViewRect Method did not work for me, and I'll try to find indications why.

Window.GetWindowRect Method returned coordinates of the Visio application window which enabled me to place UserForm1 on a specific corner of the visio window.

I want to develop a bit more before signing off..

But now other experts can know ... the problem is being solved.



Hi Scott:

My problem with GetViewRec was resolved by better window identification:

For Each win In visioApp.Windows    
        Debug.Print "win.caption = "; win.Caption & "; win.type = "; win.Type
        win.GetViewRect pinLeft, pinTop, pinWidth, pinHeight
        Debug.Print "left = "; Round(pinLeft, 0), "t = "; Round(pinTop, 0), "w = "; Round(pinWidth, 0), "h = ";    
               Round(pinHeight, 0)
Next win

GetViewRect returned negative values for the 'left' value (Round(pinLeft, 0); dim as double).
To illustrate, here is a gif of four drawing views

Does the reference point for the Left value = the centre of the drawing? And ditto for top?

Units of measurement:
Am I right in thinking that if the drawing is scaled in points then the values returned from GetViewRect will be in points?

That's me done for this Q, thanks.

Visio Consultant, Trainer, Author, and Developer
Most Valuable Expert 2011
Thanks for posting work-in-progress results, Kelvin. I recall using GetViewRect a few years ago but don't recall enough details, or have time to experiment at the moment, to answer your specific questions. You might take a look a code sample in the Visio SDK that I just found: look for "Convert Visio Coordinates to Windows Coordinates". One thing it does say is:
'The conversion of
' the Y coordinate must also take into the account the different locations of
' the origin, since Visio's origin is at the lower-left corner and the Windows
' origin is at the upper-left corner.


More fast and effective support.
Just the encouragement I was hoping for
- many thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial