简体   繁体   中英

Is it possible to run macro only when excel file is opened by Task Scheduler?

I have scheduled a file to open, run macro and close after that. Everything works fine, except if I want to open the file manually, it closes on opening as it runs the macro. Is it possible to set macro to run only when file is opened automatically?

Sounds like your Sub is using the Open Event. Just have it as a regualr Sub, inside a module, and use your vbs script to open the worksheet and run the macro.

I used to do this all the time but can't find my old scripts. Found this and I know it's similar to my old ones.

Dim xlApp
Dim xlWkb
Set xlApp = CreateObject("excel.application")
Set xlWkb = xlApp.Workbooks.Open("PATH TO YOUR FILE")
xlApp.Visible = True
xlWkb.RunAutoMacros 1 'enables macros to be run on open
xlApp.Run ("YOUR PROCEDURE")
xlApp.Workbooks("YOUR WORKBOOK NAME").Save 'Save the workbook
xlApp.Quit 'quits excel

Save as .vbs and schedule. Example found here . This way you can open and edit your script any time you want without it auto running.

如果您一天中自己打开文件的时间与调度程序打开文件的时间是不重叠的,则只需在运行代码之前添加当前时间的检查,并且它位于调度程序窗口中即可运行宏,其他明智的不要。

Excel can't detect if it is opened manually or by scheduler. But if you open it manually you can open it in protected view and disable macros this way. In the 'Open..' dialog click the small arrow on the 'Open' button and then select 'protected view'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM