Using VBA to programmatically bring a userform to always on top or floating

Joined
May 23, 2016
Messages
4
Reaction score
0
Points
1
I am new to Mac coding. I have an excel workbook application that was originally written in Windows. It essentially runs with Excel minimised then using the Onetime function it causes a userform to show/popup (modeless) periodically. In Windows I have used user32 APIs to set the userform window to always on top, but I'm struggling to find an equivalent method in Apple OSX (El Capitan). Perhaps I have foolishly presumed that OSX supports this functionality, but empirically there is evidence in some applications that it may be possible. I have found references to something in Cocoa relating to NSWindows (whatever they are) that allows a value of NSFloatingWindowLevel to be set such that it will float above all other windows. There is even a reference to another value that apparently would make a window float above even the screensaver, but that's not necessary.

I admit that I don't understand Apple APIs or their equivalent, but I simply assume there was a way (in VBA) of including OSX libraries (dylibs), then to call functions defined within those libraries and that one of these libraries might contain a function to float a window. In Windows it's straight forward: You use the user32 FindWindow function to get the handle of the userform widow by its window title, then use that handle to set the window level to TopMost.

Is there a similar method that can be used in VBA to achieve the same result from Excel VBA on a Mac? All I need is for the userform to appear over the top of the current application window when activated, always on top, topmost, or floating in some way - it doesn't matter as long as the popup is immediately visible to the user.

By way of background as to why I'd like to achieve this; my application is a time keeping tool and it is designed to prompt the user to periodically record what they've been doing in the previous period (e.g. last 15 minutes). Of course, while the userform may activate in Excel, if it is not visible and behind another application window, then it kind of defeats the purpose as the user is not reminded to record their time, especially if they have change tasks.

Anecdotally, I think this used to work in earlier versions of O365, Excel for Mac as I only seem to have experienced this challenge in the past few months and I transferred my workbook and its macros to Mac almost 1yr ago. Perhaps the userform.show modeless function has changed? I wouldn't be surprised as an earlier build of Excel (15.12 I think) stopped all the forms buttons from working and the most recent build 15.22 stopped the userform textbox control (at least) from being able to be edited properly and i had to reinstalled Excel 15.21. I am still waiting for a new build to be released that corrects the problems introduced by 15.22, but have a 15.21 pkg file on standby.

Hope someone can help.

Max
 

Rod


Joined
Jun 12, 2011
Messages
10,447
Reaction score
2,515
Points
113
Location
Melbourne, Australia and Ubud, Bali, Indonesia
Your Mac's Specs
2021 M1 MacBook Pro 14" macOS 14.5 Mid 2010MacBook 13" iPhone 13 Pro max, iPad 6, Apple Watch SE.
Have you looked at Afloat? It is an open source code which I still use successfully on my MBP with El Capitan.
 
OP
M
Joined
May 23, 2016
Messages
4
Reaction score
0
Points
1
Have you looked at Afloat? It is an open source code which I still use successfully on my MBP with El Capitan.
I was aware of Afloat, but thought that this was a manual way of pinning windows to the desktop, i.e. floating. Obviously if requires installing separately, which kind of defeats the purpose and what is easily achievable in a Windows environment. I'm still thinking there must be a library in Mac OSX somewhere that includes a suite of window manipulation functions that should be able to be called from VBA or any other programming language.

But thanks for the input.

Max
 

Rod


Joined
Jun 12, 2011
Messages
10,447
Reaction score
2,515
Points
113
Location
Melbourne, Australia and Ubud, Bali, Indonesia
Your Mac's Specs
2021 M1 MacBook Pro 14" macOS 14.5 Mid 2010MacBook 13" iPhone 13 Pro max, iPad 6, Apple Watch SE.
This may be a problem in El Capitan due to System Integrity Protection (SIP) introduced in 10.11.0. This can be disabled but I am not sure that what you want is achievable on OSX but someone with more programming knowledge may be able to comment on this.
 
OP
M
Joined
May 23, 2016
Messages
4
Reaction score
0
Points
1
This may be a problem in El Capitan due to System Integrity Protection (SIP) introduced in 10.11.0. This can be disabled but I am not sure that what you want is achievable on OSX but someone with more programming knowledge may be able to comment on this.

I'm not sure what you're gettting at. I don't want to use Afloat or other such utilities. My basic premise is that, like Windows, there is a set of API calls available in OSX that allow manipulatiuon of application windows. After all it's a basic function of the OS in that windows need to be created and manipulated (moved around etc). It would seem only logical that the OS would expose the APIs to all windows to be manipulated programatically. Therefore I'd need to know the dylib and contained function(s) and parameter values to define and call from VBA.

The reference to NSWindows and NSFloatingWindowLevel, seem to refer to some sort of Cocoa programatic interface to set a window atop the desktop by floating it above everything. However the only references to this I can find seem to use some weird coding format, perhaps assuming all the dylib inclusions have already been done.

Anyway, still hunting for an answer - It's got to be out there, but I'm surprised it's not as easy to find as the Windows equivalent was.
 

IWT


Joined
Jan 23, 2009
Messages
10,709
Reaction score
2,608
Points
113
Location
Born Scotland. Worked all over UK. Live in Wales
Your Mac's Specs
M2 Max Studio Extra, 32GB memory, 4TB, Sequoia 15.4.1 Apple 5K Retina Studio Monitor
OP
M
Joined
May 23, 2016
Messages
4
Reaction score
0
Points
1
Ok,

So two people have now suggested that the fact that my userform no longer pops up on top of my desktop is possibly due to a the introduction of the SIP in El Capitan. I kind of get it, since it's a recent inclusion in OSX, but I don't get why it might prevent an Excel (or any other VBA application for that matter) VBA userform might not appear top-most when shown as modeless. I would also prefer not to disable SIP :S

But back to the actual question~
I'm not so much looking for a reason why it may have stopped, rather a way to programatically make the userform window become topmost or float on the desktop. This is easily achieved in Windows and I've found some references to something called NSWindows Setlevel command/api and a parameter NSFloatingWindowLevel that seems to be what I'm looking for. However, i haven't been able to figure out just how (assuming it's possible) I could call the function from VBA. I only really know Windows system library call techniques and assumed that OSX would have a similar capability. I expected there to be a standard library (dylib???) somewhere in the OSX files that contains the function I need, then all I would need to do would be to reference that dylib and function and then use it as I need to set the userform window level to floating.

I'm surprised there's nothing apparent on the Internet (that I've been able to find, anyway) as it was very easy to find how to achieve this in MS Windows and I would have thought that Mac developers would have done something similar in the past.

Again - any help greatly appreciated.

Thanks
Max
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top