vba 如何从批处理文件中仅打开 excel 宏的用户窗体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20603599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:24:31  来源:igfitidea点击:

How to Open only UserForm of an excel macro from batch file

excelvbaexcel-vbabatch-file

提问by Samraan

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 opened not the excel. Below is my approach :

我正在尝试通过批处理文件打开 excel 宏的 UserForm1。我可以打开它,但 excel 也随之打开。我只想打开 UserForm1 而不是 excel。以下是我的方法:

I have written a macros to open the UserForm1

我写了一个宏来打开 UserForm1

Sub open_form()
   UserForm1.Show
End Sub

In batch File:

在批处理文件中:

@echo off
cd "c:\Test\"
openFormTest.xlsm

By the above approach, When I'm running the batch file both UserForm1 and excel are getting open, but I want to open only UserForm1. Kindly help me out

通过上述方法,当我运行批处理文件时,UserForm1 和 excel 都打开了,但我只想打开 UserForm1。请帮帮我

回答by Siddharth Rout

You need to show the UserFormin modelessmode and then hide the application.

您需要显示UserForminmodeless模式,然后隐藏应用程序。

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_QueryCloseevent

并且在按钮中您需要将其设置回 true 或者您可以使用该UserForm_QueryClose事件

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Application.Visible = True
    ThisWorkbook.Close SaveChanges:=False
End Sub

回答by hammus

There are several reasons (such as unhandled exceptions crashing your code before Application.Visibleis reset to True) that it is not a good idea to do this but I'll assume you have considered these:

有几个原因(例如未处理的异常使您的代码在Application.Visible重置为之前崩溃True)这样做不是一个好主意,但我假设您已经考虑过这些:

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 

回答by Axn40

In case someone wants to run a userform "alike" a stand alone application:

如果有人想运行一个“类似”的用户表单,一个独立的应用程序:

Issues I was facing:

我面临的问题:

  1. I did not want to use the Workbook_Open Event as the excel is locked in read only.
  2. The batch command is limited that the fact that (to my knowledge) it cannot call the macro.
  1. 我不想使用 Workbook_Open 事件,因为 excel 被锁定为只读。
  2. 批处理命令的限制在于(据我所知)它不能调用宏。

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):

然后我创建了一个 vbs 来启动这个宏(用相对路径来做这件事很棘手):

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...

我终于做了一个批处理文件来执行VBS ...

@echo off
pushd %~dp0
cscript Add_Client.vbs

Note that I have also included the "Set back to visible" in my Userform_QueryClose:

请注意,我还在我的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