• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA loop for auto launch/ wait / exit of web browser

DjDaddy

New Member
Hi everyone, I'm a newbie with almost zero background on programming :)

I'm in need of a program that can automatically in a loop manner launch my web browser (preferably firefox), and exit it after a given time period (preferably 10 seconds).
Someone told me that VBA can do this for me. I would greatly appreciate it, if one of you experts can first of all confirm this possibility. And if that is the case, then it would be great if some one could guide me through or more helpful if some one could just upload a file that could do this task for me :)
I'm using excel 2010 on Win7 and excel 2011 on Mac.

Thanks in advance for your help!
 
Hi, thanks for the reply, but it's too technical for me. As I said, I have no background in programming. I'll have to find some one in person I guess. Even if you guys provide me with the actual solution, I would't be able to use it. I was hoping that some one would guide me through or just upload a file that I could simply run it, but apparently it's not that simple.
Another person in another forum suggested the following code, but I have no clue on how to use the code :(
But again, thanks for your time and effort.
.........
Sub myLauncher()

Dim myProgram As Variant
Dim myPath As String 'the location of whatever program you choose

myPath = "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
myProgram = Shell(myPath)
Application.Wait (Now + TimeSerial(0, 0, 10)) 'wait 10 seconds
Shell ("taskkill /PID " & myProgram)

End Sub
.........
 
I had trouble closing FireFox after opening it through Wscript.Shell, but finally got it figured out.

In attached workbook, enter url in Cell B2 and then hit Alt + F8 and run "Test" macro.

Code:
Sub OpenInAnotherBrowser(url As String)
    Dim firefoxPath As String
    Dim wshell As Object
    Dim oExec As Object
    Set wshell = CreateObject("Wscript.Shell")
   
    firefoxPath = "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
   
    If Dir(firefoxPath) = "" Then pathFireFox = "C:\Program Files\Mozilla Firefox\firefox.exe"
   
    Set oExec = wshell.Exec(firefoxPath & " " & url)
   
    Application.Wait (Now + TimeValue("0:00:10"))
   
    oExec.Terminate
    Set wshell = Nothing
    Set oExec = Nothing
   

   
End Sub
Sub Test()
OpenInAnotherBrowser Sheet1.Cells(2, 2).Value

End Sub
 

Attachments

  • FireFox.xlsb
    15.1 KB · Views: 53
Thank you so much for your help. I've tried it, but received an error. The I clicked on Debug and saw the line that was yellow. Please see the attached screenshots.
 

Attachments

  • Screen Shot 2016-04-07 at 4.29.30 PM.png
    Screen Shot 2016-04-07 at 4.29.30 PM.png
    27.1 KB · Views: 13
  • Screen Shot 2016-04-07 at 4.29.30 PM.png
    Screen Shot 2016-04-07 at 4.29.30 PM.png
    27.1 KB · Views: 12
  • Screen Shot 2016-04-07 at 4.29.54 PM.png
    Screen Shot 2016-04-07 at 4.29.54 PM.png
    95.5 KB · Views: 20
Go to VBA Project Editor->Tools.

Select "References".

Add reference to "Windows Script Host Object Model".

upload_2016-4-7_16-41-13.png
 
You need to stop the VBA process that's running, before you can add references. ;)
Lol, that should tell you how uneducated I'm on this subject :)
Ok, I just checked it, and the tick mark is already there. Is there anything that I'm missing here?
Please see the attached file.
 

Attachments

  • Screen Shot 2016-04-07 at 5.08.19 PM.png
    Screen Shot 2016-04-07 at 5.08.19 PM.png
    239.9 KB · Views: 9
On second thought, you shouldn't need the reference anyways ;) It was used during testing.

Hmm, try copying and pasting the code into fresh workbook and see if it throws same error.
 
On second thought, you shouldn't need the reference anyways ;) It was used during testing.

Hmm, try copying and pasting the code into fresh workbook and see if it throws same error.
Sorry, I have no idea about how to open a new workbook! :) Are you talking about excel workbook?
 
Yes. Open a new Excel workbook copy code and see if it makes difference. If that fails I'm out of ideas. Working fine on my end.
 
Yes. Open a new Excel workbook copy code and see if it makes difference. If that fails I'm out of ideas. Working fine on my end.
OMG, it was the path :) I don't have "Program Files (x86)", I have "Program Files". Just removed "(x86)" and it worked just fine. You're life saver dude.
One last thing please, I need this to run over and over again. But it only runs one time and stops. Any idea about that?

Once again, thanks a ton!
 
Last edited:
So you just want to open FireFox and close, again and again, without need to specify URL as it's already set up to open specific sites? Then use this code.

Code:
Sub OpenInAnotherBrowser()
    Dim firefoxPath As String
    Dim wshell As Object
    Dim oExec As Object
    Dim i As Integer, x As String
  

    firefoxPath = "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"

    If Dir(firefoxPath) = "" Then firefoxPath = "C:\Program Files\Mozilla Firefox\firefox.exe"
    
    i = 1
  
EnterNumber:
    x = InputBox(Prompt:="Enter Number of times to oepn and close FireFox", Title:="No. of Loop")
  
    If Not IsNumeric(x) Then
        MsgBox "You must enter a numeric value"
        GoTo EnterNumber:
    End If
  
    For i = 1 To x
  
        Set wshell = CreateObject("Wscript.Shell")
        Set oExec = wshell.Exec(firefoxPath)

        Application.Wait (Now + TimeValue("0:00:10"))

        oExec.Terminate
        Set wshell = Nothing
        Set oExec = Nothing
        i = i + 1
    Next

End Sub

You don't need to store URL for this one. Just run OpenInAnotherBrowser by hitting ALT & F8.

Inputbox will show up, and you can enter number of times it should loop there.
 
So you just want to open FireFox and close, again and again, without need to specify URL as it's already set up to open specific sites? Then use this code.

Code:
Sub OpenInAnotherBrowser()
    Dim firefoxPath As String
    Dim wshell As Object
    Dim oExec As Object
    Dim i As Integer, x As String
 

    firefoxPath = "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"

    If Dir(firefoxPath) = "" Then firefoxPath = "C:\Program Files\Mozilla Firefox\firefox.exe"
   
    i = 1
 
EnterNumber:
    x = InputBox(Prompt:="Enter Number of times to oepn and close FireFox", Title:="No. of Loop")
 
    If Not IsNumeric(x) Then
        MsgBox "You must enter a numeric value"
        GoTo EnterNumber:
    End If
 
    For i = 1 To x
 
        Set wshell = CreateObject("Wscript.Shell")
        Set oExec = wshell.Exec(firefoxPath)

        Application.Wait (Now + TimeValue("0:00:10"))

        oExec.Terminate
        Set wshell = Nothing
        Set oExec = Nothing
        i = i + 1
    Next

End Sub

You don't need to store URL for this one. Just run OpenInAnotherBrowser by hitting ALT & F8.

Inputbox will show up, and you can enter number of times it should loop there.
You're a genius :)
Thank you so much dude! You just made my day!
Please message me your email address. I'd like to send you a gift for your time and effort!
 
You are welcome and no need for a gift. I stick around this site for learning. I'm no genius and am learning from trying to solve various questions/problems posted here.
 
You are welcome and no need for a gift. I stick around this site for learning. I'm no genius and am learning from trying to solve various questions/problems posted here.
Well, I truly appreciate what you've done for me. It would make me feel better if you were to accept a gift from me. But, either way, I wish you all the success in your life and future goals :)
You have a great evening.
 
You are welcome and no need for a gift. I stick around this site for learning. I'm no genius and am learning from trying to solve various questions/problems posted here.
Hey, sorry to bug you again :)
So this runs the way I want, but the only issue is that it keeps stopping and giving an error after few runs as it fails to terminate the firefox. Is there any way that you can add a line to it right before or after the terminate, so if it fails to terminate, then it'll try to terminate again and again until it's done and then it moves to the next line? I've attached the screenshots of the error and debug.

I've tried to figure it out, but I couldn't lol
Here is a link to a post I found for a similar issue that has been solved, not sure if that'd be helpful or not!

http://www.vbforums.com/showthread.php?616895-RESOLVED-on-error-repeat-command

Once again thanks for your help.
 

Attachments

  • Screen Shot 2016-04-08 at 4.38.23 AM.png
    Screen Shot 2016-04-08 at 4.38.23 AM.png
    30 KB · Views: 8
  • Screen Shot 2016-04-08 at 4.38.39 AM.png
    Screen Shot 2016-04-08 at 4.38.39 AM.png
    28.9 KB · Views: 9
Last edited:
Hi,

Part of the issue is that FireFox can't be accessed to same degree as IE and I don't know and can't find a way to delay code until it has finished loading using usual method of ReadyState check.

One way to deal with it is to increase Application.Wait time from 10 sec to 15 sec and see if it is adequate. If not increase by another increment.

Error handler won't resolve the issue in this case, unless there is more direct way to control FireFox (such as SeleniumBasic).
 
Hi,

Part of the issue is that FireFox can't be accessed to same degree as IE and I don't know and can't find a way to delay code until it has finished loading using usual method of ReadyState check.

One way to deal with it is to increase Application.Wait time from 10 sec to 15 sec and see if it is adequate. If not increase by another increment.

Error handler won't resolve the issue in this case, unless there is more direct way to control FireFox (such as SeleniumBasic).
Hey, thanks for the reply. I changed it to 15 and then 20, but same thing happened!
I'll try to post a new thread to see if anyone can help me out, and of course will keep you posted if any solution was found. You definitely got me out of the dark spot where I had no clue about how to get this started.

Once again, thanks a lot for your help,

Cheers...
 
Hi,

Part of the issue is that FireFox can't be accessed to same degree as IE and I don't know and can't find a way to delay code until it has finished loading using usual method of ReadyState check.

One way to deal with it is to increase Application.Wait time from 10 sec to 15 sec and see if it is adequate. If not increase by another increment.

Error handler won't resolve the issue in this case, unless there is more direct way to control FireFox (such as SeleniumBasic).
Hey Chihiro,

Here is what some one else told me to do, but when I tried it, in keeps opening a new window without closing the previous ones. Any idea about what's the issue with it?

Here is the what the programmer said:

"There are probably fancier ways of doing this.... however this is one quick way..... Code running takes over the application, so the DoEvents statements give you a moment to make it stop. Put a button on a sheet and assign it the StopRun macro, clicking it will stop the code, although the way I have this set up it will only stop AFTER closing the Firefox window."

And here's the code:

Code:
Private StopMe As Boolean
Sub myLauncher()

Dim myProgram As Variant 'the process ID of the launched program
Dim myPath As String 'the location of whatever program you choose

StopMe = False
Do While StopMe = False
DoEvents
myPath = "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
myProgram = Shell(myPath)
Application.Wait (Now + TimeSerial(0, 0, 10)) 'wait 10 seconds
Shell ("taskkill /PID " & myProgram)
DoEvents
Loop

End Sub

Sub StopRun()

StopMe = True

End Sub
 
Last edited:
I think it's the same issue that my code had.

Basically, if loop is repeated before the process is terminated, that's where it causes issue as Code overwrites "myProgram" with new instance.

The difference is, my code will throw error at ".Terminate" and stops there, where as Shell will not and just moves on to open another.

Edit: Just thought of something. Add following after .Terminate or Taskkill line.
Code:
Application.Wait (Now + TimeValue("0:00:05"))

Basically it allows time for the process to be killed before moving on to next loop
 
Back
Top