key*_*oss 12 email outlook vba scheduled-tasks excel-vba
我在使用Excel VBA发送Outlook电子邮件时遇到问题.我有代码来做 - Sendupdate
- 当我手动运行宏时,它工作正常.我的第二个宏StartTimer
是为了在我不在我办公桌前的设定时间执行上述操作.
但是,当计算机被锁定时,电子邮件不会发送.当我回到我的办公桌时,电子邮件挂在那里作为草稿,我需要点击send
按钮.
这是我的代码:
Sub SendUpdate()
Recipient = "x@y.com"
Subj = "update"
Dim msg As String
msg = "hello”
HLink = "mailto:" & Recipient & "?"
HLink = HLink & "subject=" & Subj & "&"
HLink = HLink & "body=" & msg
ActiveWorkbook.FollowHyperlink (HLink)
Application.Wait (Now + TimeValue("0:00:01"))
Application.SendKeys "%s"
End Sub
Sub StartTimer()
Application.OnTime TimeValue("18:00:00"), "SendUpdate"
End Sub
Run Code Online (Sandbox Code Playgroud)
有没有办法对宏进行编码以确保电子邮件被推送?
Sid*_*out 20
我将分3步打破这个"教程"
1)编写Excel宏
2)准备vbscript文件
3)在Windows任务计划程序中设置任务
写EXCEL MACRO
在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
Run Code Online (Sandbox Code Playgroud)
保存Excel文件,就像C:\Tester.Xlsm
使用Excel 2007以及C:\Tester.Xls
使用Excel 2003并退出一样
准备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
Run Code Online (Sandbox Code Playgroud)
将文件另存为Tester.vbs
并关闭它
在WINDOWS TASK SCHEDULER中设置任务
你能确认你的Windows操作系统吗? - Siddharth Rout 36分钟前
Windows XP.它是我的工作电脑(通常登录等). - keynesiancross 18分钟前
单击"开始"按钮| 所有课程| 配件| 系统工具| 安排任务以获取此窗口
双击"添加计划任务"以获取此窗口
点击下一步
单击"浏览"并选择我们之前创建的vbs文件,然后单击"打开"
您获得的下一个窗口至关重要,因为我们需要在脚本需要运行时提及
完成所需操作后,单击"下一步".
在此窗口中,输入您的登录详细信息,以便即使屏幕被锁定,脚本也可以运行.
完成后单击"下一步",然后在下一个窗口中单击"完成".您的任务计划程序现在看起来像这样
你完成了
锁定你的电脑然后休息喝咖啡;)当你回来时(取决于你在任务安排程序中设置的时间和休息时间),电子邮件将被发送.
HTH