我将把这个“教程”分成3个步骤:
1)编写你的Excel宏
2)准备你的VBScript文件
3)在Windows任务计划程序中设置任务
编写Excel宏
在Excel中打开一个新文件,然后在模块中粘贴此代码。
Option Explicit
Const strTo As String = "abc@abc.com"
Const strCC As String = "def@abc.com" '<~~ change "def@abc.com" to "" if you do not want to CC
Const strBCC As String = "ghi@abc.com" '<~~ change "ghi@abc.com" to "" if you do not want to BCC
Sub Sample()
Dim OutApp As Object, OutMail As Object
Dim strbody As String, strSubject As String
strSubject = "Hello World"
strbody = "This is the message for the body"
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = strTo
.CC = strCC
.BCC = strBCC
.Subject = "This is the Subject line"
.Body = strbody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
如果您使用的是Excel 2007或更高版本,请将Excel文件保存为C:\Tester.Xlsm
,如果您使用的是Excel 2003,请将其保存为C:\Tester.Xls
,然后退出。
准备VBScript文件
打开记事本,然后粘贴此代码。根据需要更改扩展名“ .xls”。
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Tester.xls", 0, True)
xlApp.Run "Sample"
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
将文件保存为Tester.vbs
,然后关闭它。
![图片描述](https://istack.dev59.com/oTlIO.webp)
在Windows任务计划程序中设置任务
请确认您的Windows操作系统。-Siddharth Rout 36分钟前
Windows XP。这是我的工作电脑(因此具有通常的登录等信息)。 - keynesiancross 18分钟前
单击"开始"按钮|所有程序|附件|系统工具|计划任务以获取此窗口
![图片描述](https://istack.dev59.com/FiYqR.webp)
双击“添加计划任务”以获取此窗口
![图片描述](https://istack.dev59.com/VFpvB.webp)
单击下一步
![图片描述](https://istack.dev59.com/BW6uU.webp)
单击“浏览”,选择我们之前创建的vbs文件,然后单击“打开”
接下来得到的窗口非常重要,因为我们需要在其中提到脚本需要运行的时间
![图片描述](https://istack.dev59.com/5TxND.webp)
完成必要的操作后,单击下一步。
![图片描述](https://istack.dev59.com/qZIcg.webp)
在此窗口中,输入您的登录详细信息,以便即使屏幕锁定,脚本也可以运行。
完成后单击“下一步”,然后在下一个窗口中单击“完成”。 您的任务计划程序现在看起来像这样
![图片描述](https://istack.dev59.com/ZksQ9.webp)
这就完成了
锁定您的PC并去喝杯咖啡 ;)当您回来时(取决于您在任务计划程序中设置的时间和休息时间长短),电子邮件已经发送。
希望对您有所帮助
Application.SendKeys“%s”
,因为当您的系统被锁定时它无法工作。 - Siddharth Rout