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
 
I usually find when I have difficulties with AppActivate (and switching windows/applications in any scenario) it's usually a result of VBA trying to do something too quickly.
The following will go to Chrome. Wait 5 seconds and then come back to Excel.

VBA Code:
Sub GoToChromeAndComeBack()
    AppActivate "Chrome"
    Pause 5
    AppActivate ActiveWorkbook.Name
End Sub

Sub Pause(Seconds As Single)
    Dim CurrentTime As Single
    CurrentTime = Timer
    Do
        DoEvents
    Loop Until CurrentTime + Seconds < Timer
End Sub
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I usually find when I have difficulties with AppActivate (and switching windows/applications in any scenario) it's usually a result of VBA trying to do something too quickly.
The following will go to Chrome. Wait 5 seconds and then come back to Excel.

VBA Code:
Sub GoToChromeAndComeBack()
    AppActivate "Chrome"
    Pause 5
    AppActivate ActiveWorkbook.Name
End Sub

Sub Pause(Seconds As Single)
    Dim CurrentTime As Single
    CurrentTime = Timer
    Do
        DoEvents
    Loop Until CurrentTime + Seconds < Timer
End Sub
How can I use this if i do not know the browser they will use? It will either be Chrome or Edge.

I also have a timer in this code that will at least wait until the file is downloaded before it moves to the next set of code. Where would I put this GoToChromeAndComeBack code within my code?
 
Upvote 0
From what I can tell, it's not the part about going to the browser that you're having a problem with - or did misunderstand?

The GoToChromeAndComeBack is just an example.

How can I use this if i do not know the browser they will use?
I have no earthly no idea who "they" are, let alone what browser "they" will use. I don't see that you've explained that scenario above, save that you have said that you "had users".

So ignoring the Chrome part of the solution, you just need to add:

VBA Code:
   ThisWorkbook.FollowHyperlink NewURL
    Pause 5
    AppActivate ActiveWorkbook.Name

I also have a timer in this code that will at least wait until the file is downloaded before it moves to the next set of code.
I don't see a timer reference in your code, save for I guess the WaitForFileDownload sub? But that's after the AppActive code and so isn't going to help here. My suggestion is making the computer pause between attempting to switch back to Excel.

Alternatively, JohnnyL's API suggestion works too (and doesn't seem to need to pause) - have you tried it? I would just say that there is a slightly change needed in order for it to work on 64 bit systems.
 
Upvote 0
From what I can tell, it's not the part about going to the browser that you're having a problem with - or did misunderstand?

The GoToChromeAndComeBack is just an example.


I have no earthly no idea who "they" are, let alone what browser "they" will use. I don't see that you've explained that scenario above, save that you have said that you "had users".

So ignoring the Chrome part of the solution, you just need to add:

VBA Code:
   ThisWorkbook.FollowHyperlink NewURL
    Pause 5
    AppActivate ActiveWorkbook.Name


I don't see a timer reference in your code, save for I guess the WaitForFileDownload sub? But that's after the AppActive code and so isn't going to help here. My suggestion is making the computer pause between attempting to switch back to Excel.

Alternatively, JohnnyL's API suggestion works too (and doesn't seem to need to pause) - have you tried it? I would just say that there is a slightly change needed in order for it to work on 64 bit systems.
Hi, yes my WaitForFileDownload waits for the file to show in the downloads folder before continuing as download speeds vary. Removing the Chrome option from you above still didn't get it even if I ran that code before the WaitForFileDownload code to run.

Looking at JohnnyL API code where do I add that code? I copied it and pasted into my Module1 and I get red code after the Else command. I have an Option Explicit at the top of my Module1 but nothing else except of Subs after it.
 
Upvote 0
Never mind the 'red code'. Just copy the entire code to a new workbook module and see if it works as it is written, ie. run the code in 'Sub Test'. The red code is just an indicator of 32bit or 64bit that will be executed, or not shall I say, as far as my understanding.

Edit: To further explain beyond the 'because I sad so' directions. I have 32bit and the top half of the API is in red for me, you indicated you use 64bit and the bottom of the API is in red for you.
 
Last edited:
Upvote 0
Never mind the 'red code'. Just copy the entire code to a new workbook module and see if it works as it is written, ie. run the code in 'Sub Test'. The red code is just an indicator of 32bit or 64bit that will be executed, or not shall I say, as far as my understanding.
Thanks @johnnyL . I moved the code to Module2 and ran Sub Test, it launched MrExcel.com in Internet Explorer and then got a Compile error: Type Mismatch on SetForegroundWindow in the Private Function BringWindowToFront
 
Upvote 0
Try the entire code, unaltered, in a new workbook module.

A brand new workbook,not another module in same workbook.
 
Upvote 0
completely new workbook, not involving your code
Sorry @johnnyL I am getting confused as I am a beginner/middle VBA person......Completly new workbook to me means a totally new Excel file?
Right now my code is in Module1 and yours is in Module2 and then I have some Workbook_Open code in ThisWorkbook so I am not sure what you are suggesting I do.

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
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