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

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

Can excel launch .bat file? (VBA, macro etc)

excelvba

提问by john

How I can launch a .batfile from excel? For example via an event or a button?

如何.batexcel启动文件?例如通过事件或按钮?

And is it possible to build a .batfile into Excel? For example, I build a launchable .batfile from Excel, a user downloads my Excel file from a server, and then this Excel file can launch the .batfile?

是否可以将.bat文件构建到 Excel 中?例如,我.bat从 Excel构建了一个可启动文件,用户从服务器下载我的 Excel 文件,然后这个 Excel 文件可以启动该.bat文件吗?

回答by JMax

Running a bat file

运行bat文件

To call a .batfile from vba, you can use the shellfunction:

要从.batvba调用文件,可以使用以下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 .bator 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:

以下是一些链接:

回答by Gleno

Think of the .xlsxfile as a .ziparchive. 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 CustomXMLPartobject.

.xlsx文件视为.zip档案。此存档包含构建 Excel 文件的各种XML组件。请注意,VBA通过对象公开了一个特殊的容器来处理您的自定义XML代码 CustomXMLPart

I once stored thumbnails in encoded XML inside an .xlsxfile, so storing any text file such as your .batshould be no problem at all.

我曾经将缩略图以编码的 XML 格式存储在一个.xlsx文件中,因此存储任何文本文件(例如您的).bat应该完全没有问题。

This MSDN articleshows you how to work with CustomXMLPartobject from VSTO. Working directly from VBA should be similar.

这篇 MSDN 文章向您展示了如何使用CustomXMLPart来自 VSTO 的对象。直接从 VBA 工作应该是类似的。