VBA code to maniplate web browser fails

Kafrin

Database Designer
Local time
Today, 07:37
Joined
Feb 17, 2009
Messages
149
SOLVED: VBA code to maniplate web browser fails

I have some code which worked fine on one computer (Vista, Excel 2007 (in compatibility mode), IE8) but which falls apart on my main computer (Vista, Excel 2003, IE7). The code is:
Code:
Sub MySub(Usr As String)
    Dim ie As Object
 
    Set ie = CreateObject("InternetExplorer.Application")
 
    ie.Visible = True
    ie.navigate ("MyURL" & Usr)
 
    ...
End Sub
I do have the Microsoft HTML Object Library set as a reference.

The problem is, the line "ie.navigate" won't use the browser window it's just created to launch the URL. If another browser window is open it uses a new tab in that, otherwise it opens a new window. Therefore my following code isn't looking at the browser window containing the webpage, so nothing works.

I don't understand why "ie.navigate" won't launch the URL in the window set to "ie". Help!!
 
Last edited:
is "URL" a string variable?

also, try:
PHP:
ie.navigate "URL" & yourvariable

also make sure of these things:

1) there are no () involved in the NAVIGATE command. they're never needed, and I don't think they work either.
2) before you go through with this. break the code, and write this:
PHP:
debug.print "YOUR URL" & yourvariable.  if it looks off, then the concat is not being meshed together correctly.  that'll tell you real quick.
3) also make sure you variable is a STRING data type.
4) HTML object libraries have nothing to do with creating IE objects in VBA  ( I don't believe)
5) make sure you have the IE object library included in the refs.
 
Thanks for the suggestions. I don't have time now to try the debug, I'll do it tomorrow and let you know how it goes. As for the other points...

I left the brackets in the navigate line by mistake after trying something else that didn't work, they weren't in my original code and have been removed again now. Sorry about that.

MyURL is in place of the actual URL and is a hard-written string in the code.

I can't find an IE object library in the references list, nor can I seem to force a reference to the .exe. Do you know what I should be looking for?

Thanks so much for the help!
 
Thanks for the suggestions. I don't have time now to try the debug, I'll do it tomorrow and let you know how it goes. As for the other points...

I left the brackets in the navigate line by mistake after trying something else that didn't work, they weren't in my original code and have been removed again now. Sorry about that.

MyURL is in place of the actual URL and is a hard-written string in the code.

I can't find an IE object library in the references list, nor can I seem to force a reference to the .exe. Do you know what I should be looking for?

Thanks so much for the help!

i apologize. i don't htink there's a ref to IE, but only to HTML since it's a language with a library. IE is part of windows, so it makes snese that there wouldn't be a library for it. no need reallly,....i would think
 
Well, the concatenation of the URL is defintely going through right. Not only does it debug correctly, the webpage does open correctly too, just not in the right window. Any other thoughts?
 
I would suggest you check the IE settings on the PC that is not opening in the new browser. There are a number of options in Tools/Internet Options/Tabs that might be worth checking. It could be that IE is determining where the new window should open based on these settings.
 
BTW - I used your code on IE 7.0.5730.11 and the URL opened in the new browser every time. There were a number of posts on various sites with similar issues for IE 7 RC1, so you may find you just need to update you IE version.
 
Thanks Cameron. I did look at some of the IE Tools/Internet Options settings, maybe just haven't hit on the right one yet.

Incidentally, my IE version is 7.0.6002.18005.

Nice to know I was thinking along the right lines anyway, I'll keep fiddling!
 
OK, I finally got it working!

It's to do with the security settings of IE. I tried to manually navigate the IE window to the site so I could test the rest of my code and I got a message about the site being in a different zone and so it had to be opened in a different window. Apparantly the IE window is intiialised in teh Local Intranet zone instead of the Internet zone (despite it not having a page loaded yet).

My version of IE includes the option for each zone to Enable Protected Mode. Disabling this for the Internet zone allowed my code to work but caused a security banner to appear at the top of the browser window for every website I visited, so I enabled it again.

I finally solved the problem by adding the website to the list of Trusted Sites (a zone which by default has Enable Protected Mode disabled, presumably as you've chosen to trust the sites...).

The site I'm opening brings up different stats depending on the Usr string in my code, however as the root of the page is the same I can add this to Trusted Sites and it works for the different versions.

Thanks for the help guys, I appreciate it.
 
Glad we could point you in the right direction, and thanks for posting your solution - I am sure others will encounter the same issue.
 

Users who are viewing this thread

Back
Top Bottom