I\'m trying to open the UserForm1 of an excel macro through batch file. I\'m able to open that but excel is also getting opened along with that. I want only UserForm1 to be
In case someone wants to run a userform "alike" a stand alone application:
Issues I was facing:
I first wrote a macro to launch my userform while hiding the application (based on your comments above):
Sub open_form()
Application.Visible = False
frmAddClient.Show vbModeless
End Sub
I then created a vbs to launch this macro (doing it with a relative path has been tricky):
dim fso
dim curDir
dim WinScriptHost
set fso = CreateObject("Scripting.FileSystemObject")
curDir = fso.GetAbsolutePathName(".")
set fso = nothing
Set xlObj = CreateObject("Excel.application")
xlObj.Workbooks.Open curDir & "\Excels\CLIENTES.xlsb"
xlObj.Run "open_form"
And I finally did a batch file to execute the VBS...
@echo off
pushd %~dp0
cscript Add_Client.vbs
Note that I have also included the "Set back to visible" in my Userform_QueryClose
:
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
ThisWorkbook.Close SaveChanges:=True
Application.Visible = True
Application.Quit
End Sub
There are several reasons (such as unhandled exceptions crashing your code before Application.Visible
is reset to True
) that it is not a good idea to do this but I'll assume you have considered these:
Private Sub UserForm_Initialize()
Application.Visible = False
End Sub
Private Sub UserForm_Terminate()
Application.Visible = True
End Sub
Private Sub Workbook_Open()
UserForm1.Show vbModeless
End Sub
You need to show the UserForm
in modeless
mode and then hide the application.
try this
Sub open_form()
Application.Visible = False
UserForm1.Show vbModeless
End Sub
and either in a button you need to set it back to true or you can use the UserForm_QueryClose
event
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Application.Visible = True
ThisWorkbook.Close SaveChanges:=False
End Sub