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.

Kelvin4
Kelvin4 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NorieAnalyst Assistant

Commented:
Kelvin

What happens when you run releaseParent1 or releaseParent2?

Author

Commented:
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 ribbonTop panel shows IE as child of xl parent, next panel: after releaseParent1(), bottom panel: after releaseParent2()
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
NorieAnalyst Assistant

Commented:
Kelvin

It probably would help to see the file.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Attached is the file, thanks.
Kelvin
1.-setParent-of-excel.xlsm

Author

Commented:
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.
You can also issue
oIE.Quit
Set oIE = Null
which is better way of the cleanup.

Set oIE = Nothing
could also work

Author

Commented:
Thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial