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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Cross-posting (posting the same question in more than one forum) is not against our rules. However, for the future, you might note point 24 of our Guidelines for Posting

You might understand that helpers can get somewhat disgruntled if they put time into developing a solution only to find later that a solution had already been achieved elsewhere.

You may also want to consider whether cross-posting actually increases your chance of getting a quick answer. For me anyway, because of the point in the last paragraph, I usually skip questions that I know are cross-posted because I don’t want to bother going to the other forum(s) to check if it has already been solved. It can also become rather messy trying to keep up if the threads are active in multiple forums at the same time.

Cross posted (at least) at:
Excel Forum
and
dBforums
 
Last edited:
Upvote 0
I apologize sincerely.

But like anyone who visits this site I just need some help. I personally clean up any and every post I make and mark them resolved accordingly.

Unfortunately, my question is almost always ignored (and or) followed up with a response about posting rules which leaves me back to square one.

Is there anyway you could help me out with this one?
 
Upvote 0
I apologize sincerely.
Apology accepted but note my comments were not meant to be accusative but helpful in guiding you to get the best out of the forum. We do try to make this a friendly forum and note that we didn't ban you or lock your thread just because you didn't follow one of our guidelines. :)


I personally clean up any and every post I make and mark them resolved accordingly.

Unfortunately, my question is almost always ignored (and or) followed up with a response about posting rules which leaves me back to square one.
Hmm, I have just had a look at you other threads in this forum and neither of them have been ignored. In fact, both have suggestions that you have not yet responded to. ;)


Is there anyway you could help me out with this one?
This particular one is not in my field of expertise so I cannot help, but I do have an idea about one of the others so I will post something there when I can get to it.
 
Upvote 0
I knew you would probably check my other threads. Quite simply, they weren't that long ago, and I do plan to get back to them (and I will).

Some of it is a fear of them being perceived as resolved and closed if I leave certain closing comments.

I am still reviewing alternative ideas, and other questions and other threads might also tie into one another.
 
Upvote 0
OK, I've posted a suggestion to this one that hadn't been responded to for about 4 days.
 
Upvote 0
Oh wow, I referenced this a few times before we even started going back and forth (didn't know it was you).

If it means anything now I thank you, and for the record I played with your suggestion for almost 6 hours straight when you first posted it. I've been back and forth to this post a million times since (I just never left a comment because other questions pertaining to other things came up).

I planned to get back to you on this one.

I bookmarked it and tweaked it as many times as I possibly could to possibly get it to work with what I was doing.

My primary problem,.. is I don't know what I'm doing : )

But at the very least, I do take the time to analyze what developers so as yourself take the time to provide for me.

I study it, and I try to find the logic in it to understand it more, as I did yours.
 
Upvote 0
Maybe this:
Code:
Private Sub WebBrowser1_TitleChange(ByVal sText As String)
    If InStr(WebBrowser1.LocationURL, "http://www.google.com") Then
        MsgBox "Title = " & sText & vbNewLine & "URL = " & WebBrowser1.LocationURL
    End If
End Sub
You might like to post a link in your other forum posts to this thread and, in addition, if my code has answered your question say so in those posts.
 
Upvote 0
... for the record I played with your suggestion for almost 6 hours straight when you first posted it. I've been back and forth to this post a million times since (I just never left a comment because other questions pertaining to other things came up).
Im not sure what your previous post is about, in particular the part I've just quoted here.

If you are referring to the post I made in your other thread, I only made that post 11 minutes before your post above, so I don't know what the 6 hours is about? :eek: :confused:
 
Upvote 0
I thought you were the different member at the top (the mouse avatar), and I thought you were saying that you posted the first reply on that.

I got a little confused my apologies.

I am experimenting your code now though however.
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,872
Members
449,097
Latest member
dbomb1414

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