Avatar of Roger
Roger
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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

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)
        UserForm1.top = Application.top + (Me.Shapes("CmdButton1").top * crn) + (UserForm1.Height / 2)
        UserForm1.Show
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: http://ramblings.mcpher.com/Home/excelquirks/snippets/mouseposition, but is that the method of choice?
I also read "Understanding Screen Scaling Issues" https://msdn.microsoft.com/en-us/library/windows/desktop/ee671605(v=vs.85).aspx. but am uncertain of its relevance

3. Application.left & Application.top (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
Kelvin
Microsoft VisioMicrosoft OfficeVBA

Avatar of undefined
Last Comment
Roger

8/22/2022 - Mon
Scott Helmers

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.)
Roger

ASKER
Scott:
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.

Kelvin
Roger

ASKER
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.

Kelvin
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Scott Helmers

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Roger

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