Running Macros without opening excel

旧巷老猫 提交于 2019-12-18 18:22:44

问题


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.


回答1:


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:

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

Source: http://wellsr.com/vba/2015/excel/run-macro-without-opening-excel-using-vbscript/




回答2:


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




回答3:


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.

I do this often to get the benefit of Intellisense, which I wouldn't have using Notepad.

If my assumption is correct, then you probably want to change your tags to VB Scripting instead of Excel to get appropriate help.




回答4:


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!




回答5:


write a cmd or ps1 that opens an excel and in that excel's startup run your macro... and then when finished close it.

this can be a solution but you are probably doing something which is unnecessary in correct planned environments.



来源:https://stackoverflow.com/questions/12759229/running-macros-without-opening-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!