vba 在不打开excel的情况下运行宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12759229/
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
Running Macros without opening excel
提问by serhat
I wonder how would you assign VBA codes written on Excel VBA to a sort of procedure/programme or maybe dos related filepath, which you can directly without opening excel. In other word, i want to have a desktop icon i can stir up a vba code i assigned to.
我想知道您如何将在 Excel VBA 上编写的 VBA 代码分配给某种程序/程序或与 dos 相关的文件路径,您无需打开 excel 即可直接使用。换句话说,我想要一个桌面图标,我可以激发我分配给的 vba 代码。
回答by Julien Kronegg
You can do it easily.
你可以轻松做到。
Add the following content in a VBS file (e.g. example.vbs). This is only a text file that you can write using Notepad:
在 VBS 文件中添加以下内容(例如 example.vbs)。这只是您可以使用记事本编写的文本文件:
'Code should be placed in a .vbs file
Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'C:\path\to\my\excel\file\myExcelMacroFile.xlsm'!MyModule.MyFunctionName"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing
Then you can double clic on the VBS file to execute it.
然后你可以双击 VBS 文件来执行它。
Source: http://wellsr.com/vba/2015/excel/run-macro-without-opening-excel-using-vbscript/
资料来源:http: //wellsr.com/vba/2015/excel/run-macro-without-opening-excel-using-vbscript/
回答by KekuSemau
IfI understand that right, you can simply write VBS code in a text file and rename it to .vbs (make sure file endings are visible in Windows). On doubleclick the file is executed by the Windows Scripting Host. VBS lacks some functionality of VBA but you can do a lot with CreateObject/GetObject.
如果我理解正确,您可以简单地在文本文件中编写 VBS 代码并将其重命名为 .vbs(确保文件结尾在 Windows 中可见)。双击时,该文件由 Windows 脚本宿主执行。VBS 缺少 VBA 的某些功能,但您可以使用 CreateObject/GetObject 做很多事情。
回答by Robert Co
If the VBA in the Excel macro doesn't reference Excel objects, you can just copy the code into a text file and change the extension to .VBS. However, VB script doesn't like it when you use types, just delete the "AS something" from your Dim statement.
如果 Excel 宏中的 VBA 未引用 Excel 对象,则只需将代码复制到文本文件中并将扩展名更改为 .VBS。但是,当您使用类型时,VB 脚本不喜欢它,只需从您的 Dim 语句中删除“AS something”即可。
I do this often to get the benefit of Intellisense, which I wouldn't have using Notepad.
我经常这样做是为了获得 Intellisense 的好处,我不会使用记事本。
If my assumption is correct, then you probably want to change your tags to VB Scripting instead of Excel to get appropriate help.
如果我的假设是正确的,那么您可能希望将标签更改为 VB 脚本而不是 Excel 以获得适当的帮助。
回答by Robert Ilbrink
Though the learning curve may be a bit steep, but you could consider using AutoHotKey. This allows you to create your own scripts and if so desired turn them into (rather large) .exe files. AutoHotKey is free!
虽然学习曲线可能有点陡峭,但您可以考虑使用 AutoHotKey。这允许您创建自己的脚本,并在需要时将它们转换为(相当大的).exe 文件。AutoHotKey 是免费的!
回答by Serdar
write a cmd or ps1 that opens an excel and in that excel's startup run your macro... and then when finished close it.
编写一个 cmd 或 ps1 来打开一个 excel 并在该 excel 的启动中运行你的宏......然后完成后关闭它。
this can be a solution but you are probably doing something which is unnecessary in correct planned environments.
这可能是一个解决方案,但您可能正在做一些在正确规划的环境中不必要的事情。