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
 
@gmooney Correct. I am asking you to open a totally new excel file and copy the code, unaltered, into a module that you insert and then run the Sub Test. To see if the code works before you start adding in your coding.
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
@gmooney Correct. I am asking you to open a totally new excel file and copy the code, unaltered, into a module that you insert and then run the Sub Test. To see if the code works before you start adding in your coding.
Do I need to move all the worksheets from the existing file over or are we simply testing this with a blank excel file?
 
Upvote 0
If you have opened a new excel file, with only the code I suggested, what compile error are you getting?

Screenshot?
 
Upvote 0

Attachments

  • Screenshot 2021-08-06 201425.png
    Screenshot 2021-08-06 201425.png
    109.7 KB · Views: 9
Upvote 0
Just tested the code JohnnyL gave you.

A couple of things:

1- SetForegroundWindow returns a Long not a LongPtr (This is probably the reason you are getting the compile error). So correct the API declaration.

2- Include a DoLoop in the test macro so the excel window is not brought to the front until the page is fully loaded.
VBA Code:
    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "http://www.mrexcel.com"
        Do While .Busy Or .ReadyState <> 4: DoEvents: Loop
    End With
  
    BringWindowToFront Application.hwnd
 
Upvote 0
Just tested the code JohnnyL gave you.

A couple of things:

1- SetForegroundWindow returns a Long not a LongPtr (This is probably the reason you are getting the compile error). So correct the API declaration.

2- Include a DoLoop in the test macro so the excel window is not brought to the front until the page is fully loaded.
VBA Code:
    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "http://www.mrexcel.com"
        Do While .Busy Or .ReadyState <> 4: DoEvents: Loop
    End With
 
    BringWindowToFront Application.hwnd
@Jaafar Tribak so I just replaced the test macro with your code and I am still getting the compile error. So is the API declaration something different than your new code?
 
Upvote 0
Try this :
VBA Code:
Option Explicit

#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 Long
    
    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"
        Do While .Busy Or .ReadyState <> 4: DoEvents: Loop
    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
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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