Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disconnect VBA UserForm from parent application

I'm using a UserForm spawned by Excel that modifies a PowerPoint presentation (it's a roundabout way to avoid needing a macro-enabled spreadsheet). The form works just fine, but every time I focus to it the Excel application takes focus (since Excel is the parent window).

Is there any way to stop this from happening? I'd like to prevent Excel from taking focus when the UserForm is used.

like image 709
Tsaukpaetra Avatar asked Jan 26 '26 03:01

Tsaukpaetra


1 Answers

Would something like this work? This will hide/make invisible the parent Excel Application while the UserForm is displayed. Or at least get you started:

Example subroutine that "Shows" the userform:

Sub Test()

Dim ppt As Object
Dim xl As Object
Set ppt = GetObject(, "PowerPoint.Application")
Application.Visible = False
UserForm1.Show vbModeless

End Sub

Use this in the form's Terminate event:

Private Sub UserForm_Terminate()
    'Ensures the Excel Application is visible after the form closes
    Application.Visible = True
End Sub

You could add a button/etc on the form, if you want to allow the user to unhide the Excel Application

Private Sub CommandButton1_Click()
    'Displays the Excel Application:
    Application.Visible = True
End Sub
like image 100
David Zemens Avatar answered Jan 28 '26 19:01

David Zemens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!