Can excel launch .bat file? (VBA, macro etc)
How I can launch a .bat
file from excel? For example via an event or a button?
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开发者_StackOverflow中文版 this Excel file can launch the .bat
file?
Running a bat file
To call a .bat
file from vba, you can use the shell
function:
Sub test()
Call Shell(Environ$("COMSPEC") & " /c C:\Path.bat", vbNormalFocus)
End Sub
Note: the /c closes the DOS prompt when finished.
Creating a .bat file from Excel
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.
Here are some links to begin with:
- create a log file
- exporting a text file from Chip Pearson
- read file
Think of the .xlsx
file as a .zip
archive. This archive houses the various XML components that build up the Excel file. Note that VBA exposes a special container to work with your custom XML code, through the CustomXMLPart
object.
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.
This MSDN article shows you how to work with CustomXMLPart
object from VSTO. Working directly from VBA should be similar.
精彩评论