Userform BringToFront

NateGreen

New Member
Joined
Mar 7, 2014
Messages
3
Issue: Bring to front userform when Excel is invisible

Background info:
I am launching a vbs script from AutoCAD which, in turn, opens a particular excel workbook and initiate a macro. This workbook is opened invisibly (i.e. "xlApp.Visible = False", "Application.Visible = False"), so that the end user is can only view the userforms created by the macro. The problem that I'm running into is that the userforms are being displayed behind AutoCAD (or any other active window). I want to force these forms to appear on top of other windows.
 

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

BrianB

Well-known Member
Joined
Feb 17, 2003
Messages
8,127
Yhis can be done using 2 API calls - one to get the window handle (which changes each time it is opened) and the other to bring the window to the top.
Code:
'==============================================================
'- CODE TO BRING USERFORM WINDOWTO THE TOP
'==============================================================
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function BringWindowToTop Lib "user32" (ByVal Hwnd As Long) As Long
Dim Hwnd As Long
'-------------------------------------------------------------

Private Sub UserForm_Initialize()
    Hwnd = FindWindow("ThunderDFrame", Me.Caption)
    BringWindowToTop (Hwnd)
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,123,366
Messages
5,601,207
Members
414,434
Latest member
Riyen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top