简体   繁体   English

使用Excel vba宏通过Windows计划任务运行

[英]Using Excel vba Macro to be run through Windows Schedule Task

I have a Excel spreadsheet which I've set up a timer to run code into a database. 我有一个Excel电子表格,我已经设置了一个计时器来将代码运行到数据库中。 If the spreadsheet is open and the time now is the time set within the timeslot then it exports the data into the database 如果电子表格是打开的,现在时间是在时间段内设置的时间,那么它会将数据导出到数据库中

I use this line in both my subroutine and the workbook_open Application.OnTime TimeValue("22:00:00"), "ExportOpenJobs" 我在我的子例程和workbook_open Application.OnTime TimeValue("22:00:00"), "ExportOpenJobs"中都使用了这一行。

This is great for when the spreadsheet is open, but I want to be able to set it through the Windows Schedule task. 这非常适合电子表格打开时,但我希望能够通过Windows计划任务进行设置。

I'm using Windows Server 2012 as my host pc and where the file is stored. 我使用Windows Server 2012作为我的主机pc和存储文件的位置。 Within the Task Scheduler I set the Action to Start a program and the Program script to the location and the actual *.xlsm file along with the start time for the Task. 在任务计划程序中,我将操作设置为启动程序,将程序脚本设置为位置和实际的* .xlsm文件以及任务的开始时间。 I set this task 30seconds before the time within the Excel VBA. 我在Excel VBA中的时间之前30秒设置此任务。

My problem is that the Windows Task Scheduler runs at the time set, after looking at the Task History I can see the Task Started/Completed and Action Started/Completed often taking around 50mins to complete, but when I check the database the Excel VBA hasn't run. 我的问题是Windows任务计划程序在设置的时间运行,在查看任务历史记录后,我可以看到任务已启动/已完成,操作已启动/已完成,通常需要大约50分钟才能完成,但是当我检查数据库时Excel VBA没有跑了。

How can I get my Task Scheduler to run the Excel VBA code? 如何让我的任务计划程序运行Excel VBA代码?

Within the Windows Server do you actually need to have Excel installed, therefore should it be done on another machine? 在Windows Server中你真的需要安装Excel,因此应该在另一台机器上完成吗?

Hello what I would do is create this .VBS file and then use Windows Task Scheduler to execute this vbs file at the desired interval. 您好我要做的是创建此.VBS文件,然后使用Windows任务计划程序以所需的时间间隔执行此vbs文件。

Set fso = CreateObject("Scripting.FileSystemObject")
curDir = fso.GetAbsolutePathName(".")

Set myxlApplication = CreateObject("Excel.Application")
myxlApplication.Visible = False
Set myWorkBook = myxlApplication.Workbooks.Open( curDir & "\myTest.xlsm" ) 'Change to the actual workbook that has the Macro
myWorkBook.Application.Run "Module1.HelloWorld" 'Change to the Module and Macro that contains your macro
myxlApplication.Quit

Application.Run Method (Excel) Application.Run方法(Excel)

This example does require Excel to be installed on to the host running the scheduled task. 此示例确实要求将Excel安装到运行计划任务的主机上。 If that host is a Server or a Desktop computer is your choice. 如果该主机是服务器或台式计算机是您的选择。

The Windows Task Scheduler should be done as such for the Action portion of the task: 对于任务的Action部分,Windows任务计划程序应如此完成:

Action: Start a program

Program/script: C:\\Windows\\SysWOW64\\cscript.exe

Add arguments (optional): C:\\Path_to_your_vbs\\Your.vbs

Start in (optional): C:\\Path_to_your_vbs\\

In Windows Task Scheduler set: Action: Start a Program Program Script: The Excel.exe with path in full quotes. 在Windows任务计划程序集中:操作:启动程序程序脚本:带有完整引号路径的Excel.exe。 Add Arguments: The workbook you want to open with full path in quotes 添加参数:要使用引号中的完整路径打开的工作簿

Check out this link: https://answers.microsoft.com/en-us/windows/forum/windows_8-performance/excel-files-can-not-be-opened-in-task-scheduler-in/aa6cf065-09ac-44d8-b6fb-a2f2ecd9b8cc 看看这个链接: https//answers.microsoft.com/en-us/windows/forum/windows_8-performance/excel-files-can-not-be-opened-in-task-scheduler-in/aa6cf065-09ac -44d8-b6fb-a2f2ecd9b8cc

Worked for me after banging my head for a couple hours. 我敲了几个小时后为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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