Using WebBrowser Control within Class Modules

I'm creating an ActiveX Dll project that has 'frmWebBrowser' which has a WebBrowser1 control in it.  I also have a 'clsWidget'.  For each instance of 'clsWidget', I would like to have a 'thisWebBrowser as WebBrowser' property with events.  Here's what I have so far:

From clsWidget:

Private WithEvents thisWebBrowser As frmWebBrowser

Public Function Connect() As Long
Dim newIndex as Integer

   newIndex = frmWebBrowser.WebBrowser.UBound
   Load frmWebBrowser.WebBrowser(newIndex)
   Set thisWebBrowser = frmWebBrowser.WebBrowser(nexIndex)
   ...
End Function

I get "Object or class does not support the set of events" when trying to set thisWebBrowser equal to the one I just created.  Does anyone have any idea how I could make this work?  Thanks.
vt45Asked:
Who is Participating?
 
AzraSoundCommented:
>>One thing to note here is that there will be no user interaction.

then perhaps you need to look into automating IE instead.  e.g., instead of selecting Microsoft Internet Controls via project -> COMPONENTS, select it via project -> REFERENCES.  then in code, in your class:


Private WithEvents IEObj As SHDocVw.InternetExplorer


'
'-- all code for events for IEObj
'


Private Sub Class_Initialize()
   Set IEObj = New SHDocVw.InternetExplorer
End Sub

Private Sub Class_Terminate()
   IEObj.Quit
   Set IEObj = Nothing
End Sub



>>What I really need is HTML document objects to navigate through and capture values of some elements and then store those in the database

there is a way to only use the HTML Object Library to load documents in and then navigate through the Document Object Model of an HTML Document.  i have never required to do so myself, but that, too, may be an option.
0
 
Dave_GreeneCommented:
Firstly, I wouldn't dim the form like this(not sure if it would work anyway)
<<Private WithEvents thisWebBrowser As frmWebBrowser>>

<CLASS CODE>

Try:
Dim thisWebBrowser as frmWebBrowser

<INITIALIZE>
'Create a new instance of the form
Set thisWebBrowser = New frmWebBrowser
'Tie the class object to the form
Set thisWebBrowser.objWebClass = Me
thisWebBrowser.show

</CLASS CODE>

<FORM CODE>
Dim objWebClass as clsWidget (or Whatever the name is)

For the events of the browser..

Private sub WebBrowser1_Navigate(...)
  objWebClass.Navigate(...)
End Sub
</FORM CODE>

'This is all from memory, but you should get the point...

Dave


0
 
Dave_GreeneCommented:
P.S. I think everyone here would agree that this in not an easy problem, and the point award should reflect this...

-d
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
AzraSoundCommented:
unless i dont understand your naming conventions:

>>Set thisWebBrowser = frmWebBrowser.WebBrowser(nexIndex)

here you are trying to set an instance of your form, frmWebBrowser, equal to a Webbrowser control object.

just out of curiosity, why are you trying to wrap this form up into a class?
0
 
Richie_SimonettiIT OperationsCommented:
Interesting...
0
 
vt45Author Commented:
Azra-
Sorry, I goofed up the "Private WithEvents thisWebBrowser As frmWebBrowser" it should read "Private WithEvents thisWebBrowser As WebBrowser". What I want to happen is for the clsWidget class to contain the WebBrowser events.  Eventually I will have clsWidget2, clsWidget3...clsWidget44 and so on.  Each class will have Widget specific code for each event.
0
 
vt45Author Commented:
Dave-

I see how your code would work.  I may have to do it that way, but I would have to create a frmWebBrowserx for each clsWidegtx.  Hopefully I will find a way to use just one form and create multiple WebBrowser object off that one form.  Thanks.
0
 
vt45Author Commented:
Dave-

I see how your code would work.  I may have to do it that way, but I would have to create a frmWebBrowserx for each clsWidegtx.  Hopefully I will find a way to use just one form and create multiple WebBrowser object off that one form.  Thanks.
0
 
AzraSoundCommented:
note that form objects are really just classes themselves, with a UI.  that is why i questioned the design of implementing a wrapper class for this "form class".


>>Eventually I will have clsWidget2, clsWidget3...clsWidget44 and so on.  Each class will have Widget specific code for each event.

so how will this be more beneficial than having frmWebBrowser1, frmWebBrowser2...and so on, with specific code for each event in that form?  i am not trying to be hard on you, just raising some design issues.

it looks like maybe you are trying to use the form as a template, but at the same time, you want multiple webbrowser object without multiple forms.  the webbrowser component can not exist without its form container.  thus, to have multiple instances of the webbrowser, youll need multiple instances of the form (unless you use only a control array for the webbrowser).  it looks like you were already taking that route, but i'm not sure.  perhaps if you could offer some details on what your ultimate goal is here, we can come up with something workable.
0
 
vt45Author Commented:
Sorry, let me explain further (or at least try to).  One thing to note here is that there will be no user interaction.  This .dll will run on it's own at specified times.  What I really need is HTML document objects to navigate through and capture values of some elements and then store those in the database.  I use alot of the WebBrowser object events, so if I could incorperate those into my class, life would be easier and my code would be more readable for other folks.  Also it would be easier to add new clsWidgets to the project if they all referenced one form. Is this possible!?  If not, does anyone this VB7 will handle something like this?  Thanks again.
0
 
vt45Author Commented:
Dave-

I see how your code would work.  I may have to do it that way, but I would have to create a frmWebBrowserx for each clsWidegtx.  Hopefully I will find a way to use just one form and create multiple WebBrowser object off that one form.  Thanks.
0
 
vt45Author Commented:
Azra-
Sorry, I goofed up the "Private WithEvents thisWebBrowser As frmWebBrowser" it should read "Private WithEvents thisWebBrowser As WebBrowser". What I want to happen is for the clsWidget class to contain the WebBrowser events.  Eventually I will have clsWidget2, clsWidget3...clsWidget44 and so on.  Each class will have Widget specific code for each event.
0
 
vt45Author Commented:
Sorry for the multiple entries!  Guess it's not a good idea to keep refresing this page!  
0
 
vt45Author Commented:
Thanks Azra, i'm in the process of implementing the IE object.  I'm running across a problem though:

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=visualbasic&qid=20144509
0
 
vt45Author Commented:
Looks like the IE object suits my need.  My problem now is that I have a hard time figuring out which document object i'm on.

Is there anyway I can send a document object to a WebBrowser control just for view purposes?  I've tried:

Set WebBrowser1.Document = IEobj.Document

but that doesn't work.  Any ideas?

Thanks.
0
 
Dave_GreeneCommented:
Put the objects into a collection then you can go by the key value...
0
 
AzraSoundCommented:
what error do you get with that line of code?
0
 
vt45Author Commented:
Azra-

Set WebBrowser1.Document = IEobj.Document

Error: "Object doesn't support this property or method"

0
 
AzraSoundCommented:
hmm...strange.  last time i checked, both of those objects supported it...oh well.

an alternative may be:

WebBrowser1.Navigate "about:blank"
Do While WebBrowser1.ReadyState <> READYSTATE_COMPLETE
    DoEvents
Loop
Call WebBrowser1.Document.write(IEObj.Document.documentElement.outerhtml)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.