Fire Excel Macro Only IF Specific URL Change, Exit Sub IF Not This URL

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)
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?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
So let me get this straight. Instead of actually helping me, you just flag me all over the internet?

If you notice I am checking and responding to everyone, and soon as it's resolved I'm going to post the solution in all forums.

Is there anyone I can pay to do this?

Because free assistance is more costly than paying someone it seems.

I'm not trying to be rude here, but it seems like everyone is dogging me out in this forum and dodging my question.

For the record, I did not know the developers help out in other forums. I thought one question in one forum meant a select group "might" see it and "might" have an answer.

It's a challenging question, and I thought trying different forums would improve my odds.

I understand the importance of forum rules; however, let's be honest here, when people are in a desperate panic they don't read the fire extinguisher instructions first.

I'm just being honest, and if my tone is too abrasive in this reply I apologize, but I practically light up when I see I've gotten an answer .. only to find out it's a "you broke the asking a question" rule post.:eek:
 
Upvote 0
all cross post links (8 total): I am checking any every one of them with follow up questions (responses), and I will mark them all as solved once I implement the solution tip top. I thank anyone in advance for any resolution, and for all feed back thus far. I saved these links because my heart was in the right place to mark all of these as solved, but I honestly didn't know that developers check other forums as well. Sincerely: Indi Visual

http://www.access-programmers.co.uk/forums/showthread.php?p=1012675#post1012675
http://forums.anandtech.com/showthread.php?p=30682733#post30682733
http://www.dbforums.com/microsoft-e...l-change-exit-sub-if-not-url.html#post6483450
http://www.excelforum.com/excel-gen...nge-exit-sub-if-not-this-url.html#post2409449
http://social.answers.microsoft.com...g/thread/6486470a-478b-4bf9-9f67-06fe60958da7
http://social.technet.microsoft.com/Forums/en/excel/thread/0e6975d6-68e6-444f-8e35-871e9a936f3c
http://www.mrexcel.com/forum/showthread.php?p=2494267#post2494267
http://www.xtremevbtalk.com/showthread.php?p=1372719
 
Upvote 0
Thank you for providing those links.

So let me get this straight. Instead of actually helping me, you just flag me all over the internet?
.......
I practically light up when I see I've gotten an answer .. only to find out it's a "you broke the asking a question" rule post
Just for the record, I replied with those links in post #11 as a courtesy for the benefit of the other members here rather than trying to make an example of you by "flagging you up all over the internet". When it comes to cross-posting, most people I know think along these lines. That article is well worth a read. With that, let's say no more about it and see if we can help you with your question.

If I understand your question correctly, you only want to do something within the TitleChange event handler when the user has navigated to yahoo.com. You said you are experimenting with the code John_w provided. How is that coming along.... are you having any troubles with it? If yes, please give us a bit more information around that and we'll try to come up with some suggestions?
 
Last edited:
Upvote 0
This actually works; however, if "02.html" is the last page loaded before the excel file is closed and saved,.. it still runs the macro at start up. This is actually what I'm trying to avoid. I simply want the macro to run only if a specific url is requested (which would be "02.html"); however, it seems as if the only way it's working for me thus far is if "01.html" is the home page loaded before the file is closed (to prevent the macro from triggering once the page is opened new again).

I believe I am close though.

Code:
Private Sub Worksheet_Activate()
   WebBrowser1.Navigate "http://www.iandmyself.me/affina.production.organizer.files/button_archive/search_button_01/search_01.html"
End Sub

Private Sub WebBrowser1_TitleChange(ByVal sText As String)
    If InStr(WebBrowser1.LocationURL, "http://www.iandmyself.me/affina.production.organizer.files/button_archive/search_button_02/search_02.html") Then
        MsgBox "Title = " & sText & vbNewLine & "URL = " & WebBrowser1.LocationURL
End If
End Sub

Click on only the middle button of the html page to test the trigger (the other two on the sides I just left empty). I am trying to produce web browser buttons for my macros. But they'll be no good if they all trigger when the page opens (it could be a disaster actually). So that's why I'm trying a situation where only a specific macro fires if a specific url is requested.
 
Last edited:
Upvote 0
Why don't you just remove the part in the worksheet activate event that is doing what you don't want to happen?

ie the Navigate bit.

How many web browsers are you using and for what purpose?

You said in an earlier post that you don't know what you are doing, I think that makes it a wee bit unlikely we do too.:)
 
Upvote 0
@ Norie

Good suggestion, I removed the activate part (it was unecessary).

But the macros are still executed at start up.

I am making web browser buttons, and I would like to launch a macro if only a specified url is selected.



my WebBrowser Button logic
specified url= specified specified button = specified macro = specified action
 
Upvote 0
What do you mean by web browser 'buttons'?

As far as I know a web browser can't 'contain' other controls.

Do you mean command buttons that you are using to allow navigation to various URLs?

Or are these buttons on the actual page that is initially navigate to?
 
Upvote 0
The buttons are just buttons on an html page.

I trigger the macro with the buttons if the link changes to another page.

ie,

If the button links to google.com, and the page changes from yahoo to google.com, then the sub will fire a macro (creating the same result of a button).

Check this out, click on the middle button for example:

http://www.iandmyself.me/affina.pro...utton_archive/search_button_02/search_02.html
 
Upvote 0
I'm not 100% sure that clicking a button on the actual page showing in the webpage will actually trigger the TitleChange event.

Try removing the If in the title change event to see exactly what's happening.
 
Upvote 0

Forum statistics

Threads
1,215,527
Messages
6,125,336
Members
449,218
Latest member
Excel Master

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