indi visual
New Member
- Joined
- Oct 15, 2010
- Messages
- 22
Fire Macro Only IF Specific URL Address Change Is Requested From WebBrowser Object?
I am using a WebBrowser Sub that runs a macro when the URL Address changes.
The problem is it fires the macro each time the browser is refreshed because it changes (or refreshes back to itself and runs the macro).
I would like the macro only to run ONLY when a SPECIFIED URL is requested.
The code beneath works great.
All are welcome to try this code for yourself (click on the developer tab>insert tools>more tools>microsoft web browser>draw browser>paste code beneath in vba)
The only problem is (as stated above), it launches each time the page is opened or refreshed. I am in need of a way to code this the right way so that it only runs the macro if a specified URL address is requested only. This way the macro will not fire up at start up or when the page is refreshed, but yet only when the specified page is loaded with the web browser.
Can anyone help me with the correct working syntax for this code?
I am using a WebBrowser Sub that runs a macro when the URL Address changes.
The problem is it fires the macro each time the browser is refreshed because it changes (or refreshes back to itself and runs the macro).
I would like the macro only to run ONLY when a SPECIFIED URL is requested.
The code beneath works great.
All are welcome to try this code for yourself (click on the developer tab>insert tools>more tools>microsoft web browser>draw browser>paste code beneath in vba)
Code:
Private Sub Worksheet_Activate()
WebBrowser1.Navigate “http://www.google.com” ‘this is home page I want the web browser to start with but NOT run the macro
End Sub
Private Sub WebBrowser1_TitleChange(ByVal sText As String)
MsgBox “Macro Runs Once Web Page Change Occurs” ‘this code works, but the problem is I need the macro to only launch with a specific URL only
End Sub
The only problem is (as stated above), it launches each time the page is opened or refreshed. I am in need of a way to code this the right way so that it only runs the macro if a specified URL address is requested only. This way the macro will not fire up at start up or when the page is refreshed, but yet only when the specified page is loaded with the web browser.
Code:
Private Sub WebBrowser1_TitleChange(ByVal sText As String)
If address.Equals("http://www.google.com") Then
MsgBox "Macro Runs Because Of Specified URL" ‘I would like the MACRO to RUN because it EQUALS the SPECIFIED URL
If address.Equals("http://www.YAHOO.com") Then
Do Nothing and Exit Sub ‘I would like it to DO NOTHING and EXIT SUB if the page changes to YAHOO or anything else for the matter
End Sub
Can anyone help me with the correct working syntax for this code?