Hello, I can get the html code of Internet Explorer when it is opened by the program. but how to do it when Internet Explorer is already open?
thank you
;***COMate*** COM automation through iDispatch.
;*===========
;*
;*Web gadget demo. (Advanced users!) Based upon code by hm.
;*
;*This demo not only shows how to obtain a COMateObject from a web-gadget, but also sets a 'global' event handler.
;*
;*This demo also shows how to over-ride the COMateObject SetEventHandler() method and direct COMate to seek out a specific
;*'out-going interface'. Left to it's own devices, COMatePLUS will utilise the first suitable 'connection point' and out-going interface
;*that it encounters. In the case of a web gadget, we direct COMatePLUS to directly seek out a connection point for the newer DWebBrowserEvents2
;*interface.
;*I did state that this demo is for advanced users!
;/////////////////////////////////////////////////////////////////////////////////
IncludePath "..\"
XIncludeFile "COMatePLUS.pbi"
Global browser.COMateObject
Global document.COMateObject
Global frames.COMateObject
Global eventName$
Declare GetHtml()
;/////////////////////////////////////////////////////////////////////////////////
;The following is our event callback for our web-gadget object.
;The 'Object' parameter will contain the COMate object upon which was used the \SetEventHandler() method; in our case it will point
;to our browser object.
Procedure EventProc(Object.COMateObject, eventName$, parameterCount)
Debug eventName$
EndProcedure
;/////////////////////////////////////////////////////////////////////////////////
Procedure GetHtml()
document = browser\GetObjectProperty("Document") : Debug "document :"+document
If document
frames = document\GetObjectProperty("frames") : Debug "frames :"+frames
If frames
Debug document\GetStringProperty("Head\InnerHtml")
Debug document\GetStringProperty("Body\InnerHtml")
EndIf
EndIf
EndProcedure
If OpenWindow(0, #PB_Ignore, #PB_Ignore, 130, 60, "WebGadget Events Test", #PB_Window_SystemMenu | #PB_Window_TitleBar )
StickyWindow(0,1)
ButtonGadget(1, 10, 10, 110, 40, "GetHTML")
; hwnd = FindWindow_("IEFrame",0) : Debug hwnd
; browser = COMate_WrapCOMObject(GetWindowLongPtr_(hwnd, #GWL_USERDATA))
browser = COMate_CreateObject("InternetExplorer.Application")
browser\SetProperty("Visible = #true")
browser\invoke("Navigate('http://www.purebasic.com')")
Debug browser
;Set the 'global' event handler for the browser object.
;In order to over-ride COMatePLUS' selection of 'connection point' we pass a pointer to the IID of the DWebBrowserEvents2 out-going interface
;within the second optional parameter.
If COMate_GetIIDFromName("DWebBrowserEvents2", @iid.IID) = #S_OK
browser\SetEventHandler(#COMate_CatchAllEvents, @EventProc(), 0, iid)
EndIf
EndIf
Repeat
Until browser\GetIntegerProperty("ReadyState") = 4
If browser
document = browser\GetObjectProperty("Document") : Debug "document :"+document
If document
frames = document\GetObjectProperty("frames") : Debug "frames :"+frames
If frames
Debug document\GetStringProperty("Head\InnerHtml")
Debug document\GetStringProperty("Body\InnerHtml")
EndIf
EndIf
EndIf
Repeat
EventID = WaitWindowEvent()
Select EventID
Case #PB_Event_Gadget
Select EventGadget()
Case 1
GetHtml()
EndSelect
EndSelect
Until EventID = #PB_Event_CloseWindow
frames\Release()
document\Release()
browser\Release()
sorry for my english, I'm french
Last edited by Christophe_fr on Wed Feb 20, 2019 1:06 pm, edited 1 time in total.
FindWindow_ is an API that search for an opened window, so if you remove it, your program will never search for window
if I commented the two lines is to show that my program works when it opens itself Internet Explorer.
But when I search the window with FindWindow_ it does not work
Thank you mk-soft, but what I want is to have the html code when Internet Explorer is already launched. What I want is to retrieve information on a web page for my job (name, delivery date ...)
Dim w, winTitle, oWindow
winTitle = "Your chosen title"
for each w in createobject("shell.application").windows
if instr(1, typename(w.document),"htmldocument", vbTextCompare) > 0 then
if instr(1, w.document.title, sWinTitle, vbTextCompare) = 1 then
set oWindow = w
end if
end if
next
if not IsObject(oWindow) then
set oWindow = CreateObject("InternetExplorer.Application")
oWindow.Navigate "about:Blank"
do until oWindow.Readystate = 4 : wsh.sleep 50 : Loop
oWindow.document.title = winTitle
end if
For Each w in oShell.Windows
If InStr(1, typename(w.document),"htmldocument", 1) > 0 Then
If InStr(1, w.document.title, sWinTitle, 1) = 1 Then
Set oWindow = w
Runtime.String("Title") = w.document.title
End If
End If
Next
Set oShell = Nothing
If Not IsObject(oWindow) then
Runtime.String("result") = "InternetExplorer: Page not found"
Else
Runtime.String("result") = oWindow.document.documentElement.outerHTML
Set oWindow = Nothing
End If