excel可以启动.bat文件吗?(VBA,宏等)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7470916/
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
Can excel launch .bat file? (VBA, macro etc)
提问by john
How I can launch a .bat
file from excel? For example via an event or a button?
如何.bat
从excel启动文件?例如通过事件或按钮?
And is it possible to build a .bat
file into Excel? For example, I build a launchable .bat
file from Excel, a user downloads my Excel file from a server, and then this Excel file can launch the .bat
file?
是否可以将.bat
文件构建到 Excel 中?例如,我.bat
从 Excel构建了一个可启动文件,用户从服务器下载我的 Excel 文件,然后这个 Excel 文件可以启动该.bat
文件吗?
回答by JMax
Running a bat file
运行bat文件
To call a .bat
file from vba, you can use the shell
function:
要从.bat
vba调用文件,可以使用以下shell
函数:
Sub test()
Call Shell(Environ$("COMSPEC") & " /c C:\Path.bat", vbNormalFocus)
End Sub
Note:the /c closes the DOS prompt when finished.
注意:/c 完成后关闭 DOS 提示符。
Creating a .bat file from Excel
从 Excel 创建 .bat 文件
You can create a file (a .bat
or whatever) with VBA so it could be a txt, a bat or a log file, code is seemingly the same.
您可以.bat
使用 VBA创建一个文件(a或其他),因此它可以是 txt、bat 或日志文件,代码似乎相同。
Here are some links to begin with:
以下是一些链接:
- create a log file
- exporting a text filefrom Chip Pearson
- read file
回答by Gleno
Think of the .xlsx
file as a .zip
archive. This archive houses the various XMLcomponents that build up the Excel file. Note that VBA exposes a special container to work with your custom XMLcode, through the CustomXMLPart
object.
将.xlsx
文件视为.zip
档案。此存档包含构建 Excel 文件的各种XML组件。请注意,VBA通过对象公开了一个特殊的容器来处理您的自定义XML代码 CustomXMLPart
。
I once stored thumbnails in encoded XML inside an .xlsx
file, so storing any text file such as your .bat
should be no problem at all.
我曾经将缩略图以编码的 XML 格式存储在一个.xlsx
文件中,因此存储任何文本文件(例如您的).bat
应该完全没有问题。
This MSDN articleshows you how to work with CustomXMLPart
object from VSTO. Working directly from VBA should be similar.
这篇 MSDN 文章向您展示了如何使用CustomXMLPart
来自 VSTO 的对象。直接从 VBA 工作应该是类似的。