Keeping focus on IE so it stays on top

Sharid

Well-known Member
Joined
Apr 22, 2007
Messages
1,064
Office Version
  1. 2016
Platform
  1. Windows
I am using this code to keep my userform on top, however my IE goes behind this, I tried to set focus on the IE but it is not working
VBA Code:
'Summary: Keeps a UserForm on top of all other Windows.

'API Call Declarations
Public Declare PtrSafe Function GetActiveWindow _
  Lib "user32.dll" () As Long

Private Declare PtrSafe Function SetWindowPos _
  Lib "user32.dll" _
    (ByVal hWnd As Long, _
     ByVal hWndInsertAfter As Long, _
     ByVal X As Long, _
     ByVal Y As Long, _
     ByVal cx As Long, _
     ByVal cy As Long, _
     ByVal wFlags As Long) As Long

'VBA Macro
Public Sub KeepFormOnTop()

  Dim hWnd As Long
  Dim RetVal As Long

Const HWND_TOPMOST = -1
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
    
    hWnd = GetActiveWindow()
    RetVal = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE)

End Sub

I tried this and get the following error message
VBA Code:
Set objIE = New InternetExplorer 'initiating a new instance of Internet Explorer and assigning it to objIE
        objIE.Visible = True
        objIE.document.Focus ()

1611090662327.png


I have also tried this and get another error message
VBA Code:
Set objIE = New InternetExplorer 'initiating a new instance of Internet Explorer and assigning it to objIE
        objIE.Visible = True
        objIE.document.Focus () = True
1611092844707.png
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Keeping focus on IE so it stays on top​

I am using this code to keep my userform on top,



Which one do you want on top, IE or the userform?
 
Upvote 0
You are making the userform the TOP MOST window so, by definition, you can't place the IE window on top of the userform.
However, if you set the IE as the owner, you can achieve the effect you want.
Try this and see how it goes :
VBA Code:
Option Explicit

#If VBA7 Then
    #If Win64 Then
        Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongPtrA" (ByVal hWnd As LongLong, ByVal nIndex As Long, ByVal dwNewLong As LongLong) As LongLong
    #Else
        Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    #End If
        Private Declare PtrSafe Function SetWindowPos Lib "user32.dll" (ByVal hWnd As LongPtr, ByVal hWndInsertAfter As LongPtr, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
        Private Declare PtrSafe Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hWnd As LongPtr) As Long
#Else
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function SetWindowPos Lib "user32.dll" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    Private Declare Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hWnd As Long) As Long
#End If


Public Sub KEEP_FORM_ONTOP_AND_IEXPLORER_ABOVE_IT()

    #If Win64 Then
        Dim hWnd As LongLong
    #Else
        Dim hWnd As Long
    #End If

    Const HWND_TOPMOST = -1
    Const SWP_NOSIZE = &H1
    Const SWP_NOMOVE = &H2
    Const GWLP_HWNDPARENT = (-8)

    Dim objIE As InternetExplorer
   
    Call IUnknown_GetWindow(UserForm1, VarPtr(hWnd))  '<== change userform name to suit.
   
    Set objIE = New InternetExplorer
    objIE.Visible = True
   
    Call SetWindowLong(objIE.hWnd, GWLP_HWNDPARENT, hWnd)
   
    Call SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE)

End Sub
 
Upvote 0
Jaafar

I have had a few issues with your code,

1) It would open multiple browsers - (When my code is run it opens up the browser, (Not sure this is what caused the issue)
2) All Opened browsers were blank.

So I changed this
VBA Code:
    Set objIE = New InternetExplorer
     objIE.Visible = True

To This
VBA Code:
        Set objIE = New InternetExplorer 'initiating a new instance of Internet Explorer and asigning it to objIE
        objIE.Visible = Sheet20.Range("H42").Value   'Get Browser Value from HERE - True OR False
        objIE.navigate Sheets("Sheet20").Range("G9").Value & Replace(Worksheets("Sheet20").Range("H9") & Range("I9").Value, " ", "+")  'navigate IE to this web page

Still had multiple browsers opening, but did navigate to right page.

3) I am using your code for resizing a form, not sure if YOUR above code is conflicting with that code. That being said both your code and the one I was using for keeping form onto, cause an issue if the code crashes or stops running. As I can not use the ESC key, nor can I action the debug error message, to end the task or debug the problem.

The userform is full screen and will not move as it is always on top. I can not use task manager to shut down excel, as useform is always ontop. Therefore I have to restart my pc. LOL. I do need to workout why the code code crashes, but for now I can't get to the debug message as the form is ontop and have to keep restarting the pc. Not sure if there is a workaround for this.
 
Upvote 0
Not sure what code you have that is causing the issues.
Can you upload here a workbook sample that reproduces the problem so I can take a look.
 
Upvote 0
Jaafar

I can not load the workbook as it has a lot of sensitive work related stuff in it. That being said I only need the "KeepFormOnTop" for only two or three codes.
I had My code and then yours in Form Activate. This kept the Userform on top ALL the time. When I only really need the form ontop for two codes as every time they are run the userform disappears off screen. I have to click on the excel icon in the task bar to bring the form back up. By using "KeepFormOnTop" this problem went away, but then when the browser code was run, the browser went to the back.

Is there was a way to enable and disable "KeepFormOnTop" then I would not have an issue with the browser going to the back as when that code is run the "KeepFormOnTop" would not be called.

I use this to call this Module. Is there a way to exit the "KeepFormOnTop" sub when my main code has finished running

VBA Code:
Command Button1_Click
     Application.Run "Module1.ReportGenerate"
End Sub


VBA Code:
Private Sub ReportGenerate ()
   
          Call KeepFormOnTop '''' Can I Call the form on top code HERE

'#########################
     Run My Main Code
'########################

''''' Is there a way to EXIT the Form On Top code Here
End Sub
 
Upvote 0
Try running the code I posted without any other of your existing code and you should see how it works.

I would add the code to a new fresh userform (UserForm1 for testing) and I would then call the code from a commandbutton on the form as follows :
VBA Code:
Private Sub CommandButton1_Click()
    Call KEEP_FORM_ONTOP_AND_IEXPLORER_ABOVE_IT
End Sub

You should see how the IExplorer window is launched and placed over the userform while still keeping the userform as the TOPMOST window.
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,251
Members
448,556
Latest member
peterhess2002

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
Back
Top