PDA

View Full Version : Solved: VBA Internet Explorer References



pundabuyer
11-06-2008, 01:34 PM
Hi Experts!

We need to pull some information from an already open internet explorer window (if not possible it can be openned with code?) into an excel spreadsheet via VBA code withint the already openned sheet.

I have been looking all over for libraries and activeX refs etc and have found that i can use MSINET.OCX i have downloaded them and added the references (after 'installing' with regsvr32) but whenever i use them in vba/vb they dnt seem to work properly, they won't bring up prompts as i type like vb/vba usually does and this code fails:





Private Sub CommandButton1_Click()
'Dim ie As Object
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "google's web address here cnt add as under 5 posts limit!"
While ie.busy
DoEvents
Wend
ie.document.all("Q").Value = "HELLO"
End Sub


It opens up google and then errors out on:

Run-time error '-2147417848 (80010108)':
Automation error
The object invoked has disconnected from its clients.

Have i not installed them correctly or is this wrong? Is there a better way?
HELP!!!

OFFICE VERSION 2002 - CODE ERRORS ON LINE "While ie.busy" - I NEED THIS SECTION AS IT MUST PAUSE WHILE IT FINISHES LOADING!

Thanks in advance....

mdmackillop
11-06-2008, 01:43 PM
Is the code in this (http://vbaexpress.com/kb/getarticle.php?kb_id=818) any help?

Demosthine
11-06-2008, 01:46 PM
Good Afternoon.

For starters, to use Intellisense (the little window that pops up with the Properties, Methods, etc.), you have to properly declare your Variable.

Once you reference your Library using Tools | References... and enable "Microsoft Internet Controls" (this is C:\Windows\System32\ShDocVw.Dll), you can define a Variable for InternetExplorer.

If anything errors once you've changed this, let us know.

Scott

pundabuyer
11-06-2008, 01:52 PM
With all due respect.. that has nothing to do with it! :P

But thanks anyway

pundabuyer
11-06-2008, 02:03 PM
Thanks Scott,

I seem to have the same problem still

i have added the dll but intellisense still isn't working and it doesn't like ie.busy still!

thanks

Demosthine
11-06-2008, 02:13 PM
Hey there.

I've attached a Workbook that has the appropriate reference and works perfectly for me. Once the page is completely loaded, it places "HELLO" in the Search Box.



Option Explicit
Private Sub CommandButton1_Click()
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate ("www.google.com (http://www.google.com/)")
While ie.busy
DoEvents
Wend
ie.document.all("Q").Value = "HELLO"
End Sub

Hope this helps.
Scott

jfournier
11-06-2008, 02:19 PM
You may want to try a reference to shdocvw.dll, it works pretty well for me...


Sub TryInternetExplorer()
'Reference to system32\shdocvw.dll 'Microsoft Internet Controls
Dim IEApp As InternetExplorer
Set IEApp = New InternetExplorer
IEApp.Visible = True
IEApp.Navigate "www.techcrunch.com"
While IEApp.ReadyState <> READYSTATE_COMPLETE And IEApp.ReadyState <> READYSTATE_LOADED
DoEvents
Wend
End Sub

pundabuyer
11-06-2008, 02:25 PM
The code you have done is not assigned to a button, i have moved it to worksheet1 and added a button and it runs and opens google but fails on:

ie.document.all("Q").Value = "HELLO"

Automation Error
Unspecified Error

pundabuyer
11-06-2008, 02:31 PM
I have run the other one from JFOURNIER too (thanks) I get the disconnected from clients error again from the 'while' line again. The .busy is not the problem now i dnt think, It is the last line

pundabuyer
11-06-2008, 02:39 PM
error code is 2147467259
any ideas?

pundabuyer
11-06-2008, 02:39 PM
i am using:


Private Sub CommandButton1_Click()
Dim IEApp As InternetExplorer
Set IEApp = New InternetExplorer
IEApp.Visible = True
IEApp.Navigate "www.google.com (http://www.google.com)"
While IEApp.busy
DoEvents
Wend
IEApp.document.all("q").Value = "HELLO"
End Sub

MaximS
11-06-2008, 07:32 PM
try this:



Private Sub cmdOpen_Click()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
With ie
.navigate "http://www.google.co.uk (http://www.google.co.uk/)"
Do While .busy: DoEvents: Loop
Do While .ReadyState <> 4: DoEvents: Loop
With .Document.Forms(0)
.q.Value = "HELLO"
.btnG.Click
End With
End With
End Sub

Demosthine
11-06-2008, 09:58 PM
Good Evening.

Well, after playing around with it at work, I was unable to reproduce the error. As soon as I tried it at home, the process errored at ie.Document.all("Q").Value ... just as you said.

It appears to be a difference in versions between Internet Explorer 6.0 and 7.0. At least that's the difference on my Laptop.

Now, to fix the problem, I changed from a While ... Wend Loop to a Do ... Loop Until. I also changed from IE.Busy to IE.ReadyState. The corrected code is listed below and works fine on Windows Vista with Internet Explorer 7.0. I also had Mark (GTO) check it in Windows XP with Internet Explorer 6.0.



Option Explicit
Private Sub CommandButton1_Click()
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate ("www.google.com (http://www.google.com)")
Do
DoEvents
Loop Until ie.ReadyState = READYSTATE_COMPLETE
ie.Document.All("q").Value = "HELLO"
End Sub


Take care.
Scott

NY2DR
11-07-2008, 06:31 AM
This is a nice little program!

Can this code be used with Mozilla Firefox?

pundabuyer
11-07-2008, 01:36 PM
Thanks for helping me out with this Scott, I have tried the code :



Option Explicit
Private Sub CommandButton1_Click()
Dim ie As InternetExplorer
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate ("www.google.com (http://www.google.com)")
Do
DoEvents
Loop Until ie.ReadyState = READYSTATE_COMPLETE
ie.Document.All("q").Value = "HELLO"
End Sub



But it opens 2 IE WINDOWS! One opens to the homepage as normal and the other is a smaller thinner ie window and just crashes out and the code just waits i imagine as state is not 'ready' Any Ideas?

I like to keep you all busy :)

Thanks Though seriously!

georgiboy
11-07-2008, 01:59 PM
I am not very good with the ie thing but i have this example lurking in the depths of my computer, it will log into gmail with a desired username/password, i have adapted this to work with sites such as facebook and ebay in the past.

hope it helps

pundabuyer
11-07-2008, 02:40 PM
I still get an automation error, Interface is unknown. ??? :(

georgiboy
11-07-2008, 03:02 PM
This may be obvious but have you enabled internet controls in the vbe referenes window.

GTO
11-07-2008, 04:36 PM
Thanks for helping me out with this Scott, I have tried the code ...But it opens 2 IE WINDOWS! One opens to the homepage as normal and the other is a smaller thinner ie window and just crashes out and the code just waits i imagine as state is not 'ready' Any Ideas?

Greetings pundabuyer,

Unless you have the "tremors":old: , I see no way for Scott's code to open two windows. (Just joking about the "shakes" of course...)

I have run both Maxims' code from #12, as well Scott's from #13. Both run flawlessly, so there must be something else going on here.

Sorry if I missed it, but I don't see anywhere where you specified: what version of Internet Explorer are you using?

I did catch that you are running Excel 2002.

Just out of curiousity, did you try Maxims' as well? (I have Excel 2000 at home and 2003 at work, where I am currently at - so can't check for any app version issues - but thought you might want to try the late binding)

Finally, how about you post a cleaned-up (ie: no private/company/proprietary information included) version of your workbook as currently written. Maybe we could see the error better if looking at the 'big picture'.

Mark

Edit: PS - Sorry, forgot to mention. When pasting code, click the green/white VBA button at the top of the msg window and paste the code between the tags.

Carl A
11-07-2008, 05:49 PM
This may be an issue with IE7. I was using the MSINET.OCX in Office and all of a sudden I started receiving an error in my VB6 programs that used the web browser control could not find the ieframe.dll\1 file. I googled it and found several sites that recommend searching the registry and modifying the string value to ieframe.dll. In my case this worked. This may not be the case in your situation. I highly recommend you backup your registry first then do a search for the aforementioned file and change it. Check out this URL for more information.
http://forums.microsoft.com/msdn/showpost.aspx?postid=843101&siteid=1&sb=0&d=1&at=7&ft=11&tf=0&pageid=1
For what it is worth I did not have this problem until I started using the MSINET.OCX in Office 2007.

HTH

pundabuyer
11-08-2008, 07:27 AM
I think am going to ge this one up as a bad job!! I know its not the code or the workbook it is something to do with the computer and/or the ocx file!

I think ill carry on the research at work where it works fine!!

Thanks Anyway!

pundabuyer
11-09-2008, 01:28 PM
Hey Guys!

Upgraded to the new ie8 Beta and works fine!

Thanks for all your help!