I have been struggling with this question for quite some time now.
I have a userform that is called from an Excel add-in I've built which then tests whether the current active workbook contains a specific structure. If not, it creates a new workbook with that exact structure. I set this workbook as well as another workbook that I open as wb1 and wb2.
The issue is that the userform is initially called from the activeworkbook (could be any workbook) at the time of clicking the ribbon button and basically latches onto THAT workbook alone.
Is there any way I could detach the userform from that workbook and change it to show my wb2 in the background of my userform?
I have tried the following code, but it just closes my userform and doesn't work.
Option Explicit
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal clsName As String, ByVal wndName As String) As Long
Private Declare PtrSafe Function SetParent Lib "user32" (ByVal hChild As Long, ByVal hParent As Long) As Long
Private Sub CommandButton1_Click()
Static h As Long
If h <= 0 Then h = FindWindow("ThunderDFrame", Me.Caption)
If h <= 0 Then Exit Sub
Dim wb As Workbook: Set wb = Workbooks.Add
SetParent h, Application.Windows(wb.Name).Hwnd
wb.Activate
End Sub
Any ideas?