AppActivate After opening Web Brower

gmooney

Active Member
Joined
Oct 21, 2004
Messages
254
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the following code that essentially looks for a downloaded file before launching the URL and automatically downloading a file.

I want the Excel file to return to focus but this code is not and it is staying on the web browser, thus I have to click on Excel from the taskbar. Any ideas how to get Excel back to be displayed?

VBA Code:
Sub GetURL()

    Dim NewURL As String
    Dim FollowURL As String

    NewURL = ThisWorkbook.Sheets("Category Review").Range("AP107").Value
    
    CheckforDuplicateDownloadFile
    
    ThisWorkbook.FollowHyperlink NewURL
    
    'Sheets("Instructions").Select
    
    AppActivate "Category Review Builder"
    
    WaitForFileDownload
    
    
    
    BuildMyCategoryReview
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Give this a try:

AppActivate Application.Caption
 
Upvote 0
how about:

AppActivate ThisWorkbook.Application
 
Upvote 0
Assuming your workbook is called 'Category Review Builder.xlsm' ... how about:

AppActivate "Category Review Builder.xlsm - Microsoft excel"
 
Upvote 0
how about:

AppActivate "Category Review Builder.xlsm - Microsoft excel"
I don't think that will work because AppActivate "Category Review Builder" works in other areas of code just fine. It is only when web browser gets launched and displays over the top of the Excel file.
 
Upvote 0
If you tried that last one with no luck, are you running excel 32bit or 64bit?
 
Upvote 0
If you tried that last one with no luck, are you running excel 32bit or 64bit?
64bit....early on in the coding process of this excel file I was having issues with the AppActivate function and someone helped me to get it corrected. I built the Excel file on 64bit Excel but I do have users on 32bit and this fix they had me do allowed AppActivate in other procedures of the code to work on both 32 ands 64.
 
Upvote 0
Ok, last thing I can suggest is some code from @Jaafar Tribak that uses a function to 'BringWindowToFront':

VBA Code:
#If VBA7 Then

    Private Declare PtrSafe Function GetWindowThreadProcessId Lib "user32" _
        (ByVal hwnd As LongPtr, _
        lpdwProcessId As LongPtr) As Long
    
    Private Declare PtrSafe Function AttachThreadInput Lib "user32" _
        (ByVal idAttach As Long, _
        ByVal idAttachTo As Long, _
        ByVal fAttach As Long) As Long  'all Long because thread IDs are still 32 bits wide in 64-bit Windows
    
    Private Declare PtrSafe Function GetForegroundWindow Lib "user32" _
        () As LongPtr
    
    Private Declare PtrSafe Function SetForegroundWindow Lib "user32" _
        (ByVal hwnd As LongPtr) As LongPtr
    
    Private Declare PtrSafe Function IsIconic Lib "user32" _
        (ByVal hwnd As LongPtr) As Long
    
    Private Declare PtrSafe Function ShowWindow Lib "user32" _
        (ByVal hwnd As LongPtr, _
        ByVal nCmdShow As Long) As Long
       
#Else

    Private Declare Function GetWindowThreadProcessId Lib "user32" _
        (ByVal hwnd As Long, _
        lpdwProcessId As Long) As Long
    
    Private Declare Function AttachThreadInput Lib "user32" _
        (ByVal idAttach As Long, _
        ByVal idAttachTo As Long, _
        ByVal fAttach As Long) As Long
    
    Private Declare Function GetForegroundWindow Lib "user32" _
        () As Long
    
    Private Declare Function SetForegroundWindow Lib "user32" _
        (ByVal hwnd As Long) As Long
    
    Private Declare Function IsIconic Lib "user32" _
        (ByVal hwnd As Long) As Long
    
    Private Declare Function ShowWindow Lib "user32" _
        (ByVal hwnd As Long, _
        ByVal nCmdShow As Long) As Long
       
#End If
 
Private Const SW_SHOW = 5
Private Const SW_RESTORE = 9


Sub Test()

    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "http://www.mrexcel.com"
    End With
   
    BringWindowToFront Application.hwnd
   
End Sub

Private Function BringWindowToFront _
(ByVal hwnd As Long) As Boolean
 
    Dim ThreadID1 As Long
    Dim ThreadID2 As Long
    Dim nRet As Long
   
    On Error Resume Next
   
    ' Nothing to do if already in foreground.
    If hwnd = GetForegroundWindow() Then
        BringWindowToFront = True
    Else
   
        'First need to get the thread responsible for this window,
        'and the thread for the foreground window.
        ThreadID1 = _
        GetWindowThreadProcessId(GetForegroundWindow, ByVal 0&)
        ThreadID2 = _
        GetWindowThreadProcessId(hwnd, ByVal 0&)
       
        'By sharing input state, threads share their concept of
        'the active window.
        Call AttachThreadInput(ThreadID1, ThreadID2, True)
        nRet = SetForegroundWindow(hwnd)
       
        'Restore and repaint.
        If IsIconic(hwnd) Then
            Call ShowWindow(hwnd, SW_RESTORE)
        Else
            Call ShowWindow(hwnd, SW_SHOW)
        End If
       
        'BringWindowToFront returns TRUE if success.
        BringWindowToFront = CBool(nRet)
       
    End If
End Function

Test that to see if that works for you.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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