Link to home
Start Free TrialLog in
Avatar of Roger
RogerFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Having made an instance of IE as a child of ThisWorkbook via API SetParent oIE.hwnd, Application.hwnd, I CANNOT now release the child from its parent.

Using Excel 2016: In the following code:  Sub getIE_makeChild_ofXl() opens internet explorer as a child of ThisWorkbook.
As expected, when the workbook is dragged, the IE wondow moves. When the excel.App.window is clicked, the IE window remains on top of thisWorkbook

However, I could not release IE from its parent either via Sub releaseParent1() nor via Sub releaseParent2().

Can you help me to release the child window?
Thanks, Kelvin

Option Explicit

#If Win64 Then
    'source (Chip Pearson) https://www.pcreview.co.uk/threads/in-vba-make-ie-browser-object-always-in-foreground.2503326/
    Declare Function SetParent Lib "user32" (ByVal hWndChild As LongLong, ByVal hWndNewParent As Long) As LongLong

    'source: http://www.vbforums.com/showthread.php?526112-RESOLVED-Undo-SetParent
    Declare Function GetDesktopWindow Lib "user32.dll" () As LongLong
#Else
    Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    Declare Function GetDesktopWindow Lib "user32.dll" () As Long
#End If

Private oIE As Object

'this places the IE window ontop of the Excel sheet window, U can drag it about to see the xl window,
'' but the VISIBLE part of the IE window is that WITHIN the margins of the xl window.
   
Sub getIE_makeChild_ofXl()
   
    Set oIE = CreateObject("InternetExplorer.application")
    oIE.Visible = True
    oIE.navigate ("http://bbc.co.uk")
    oIE.Visible = True
   
    'make child
    SetParent oIE.hwnd, Application.hwnd

End Sub

Sub releaseParent1()

'source: http://www.vbforums.com/showthread.php?526112-RESOLVED-Undo-SetParent
    SetParent oIE.hwnd, GetDesktopWindow()
End Sub

Sub releaseParent2()
    'source: http://www.xtremevbtalk.com/general/262412-desktop-hwnd.html
Dim hWndDesktop As Long
   
    hWndDesktop = GetDesktopWindow
    SetParent oIE.hwnd, hWndDesktop
End Sub
Avatar of Norie
Norie

Kelvin

What happens when you run releaseParent1 or releaseParent2?
Avatar of Roger

ASKER

Hi Norie,
No sign of a bug with either releaseParent1 or releaseParent2.
However, before attempting release, IE is on top of the Green header band behind the Xl fluent ribbonUser generated image
After :  releaseParent1() or releaseParent2() the full header region of xl is ON TOP of IE; but IE is on top of the worksheet, and still moves with xl when xl is dragged.

I dont have the knowledge to try to put in reporters for API processes, but I can try under instruction.
Would you like to have the xl file?

Many thanks
Kelvin
Kelvin

It probably would help to see the file.
Avatar of Roger

ASKER

Attached is the file, thanks.
Kelvin
1.-setParent-of-excel.xlsm
Avatar of Roger

ASKER

Sub releaseParent3()
         'also failed to release
         'source: http://www.jasinskionline.com/windowsapi/ref/s/setparent.html
    SetParent oIE.hWnd, 0
End Sub

At present, my best work around is to set the child IE .visible = false, and recall it to view with .visible = true.
To unload the child IE, I need to close xl and re-open.
ASKER CERTIFIED SOLUTION
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Roger

ASKER

Thank you