Excel VBA - 电脑锁定时不发送电子邮件

问题描述:

我在使用Excel VBA发送Outlook电子邮件时遇到问题。我有代码来做到这一点 - Sendupdate - 当我手动运行宏时它工作正常。我的第二个宏StartTimer旨在在我不在我的办公桌的时候执行上述操作。Excel VBA - 电脑锁定时不发送电子邮件

但是,当计算机被锁定时,电子邮件不会发送。当我回到办公桌时,电子邮件挂在那里作为草稿,我需要点击send按钮。

这里是我的代码:

Sub SendUpdate() 
Recipient = "[email protected]" 
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 

有没有办法编写宏以确保电子邮件被推开?

+0

对于这些类型的操作,我从不使用Excel。我使用Windows任务计划程序和一个VBScript文件从Excel发送电子邮件。如果你有兴趣,那么我可以给你一个例子。并且绝对不是'Application.SendKeys“%s”',因为当你的系统被锁定时它不起作用。 – 2012-04-18 14:29:12

+0

嗨Siddharth,谢谢你的反馈。如果你不介意,我希望看到你如何做到这一点的例子。非常感谢! – keynesiancross 2012-04-18 14:36:10

+0

好吧,我需要至少30分钟来写它,测试它,然后在这里上传截图:) – 2012-04-18 14:37:47

我会打破这个 “指南”,在3个步骤

1)写你的Excel宏

2)准备您的VBScript文件

3)设置任务在Windows任务调度


书面说明,Excel宏


在Excel中打开和模块中的新文件,粘贴此代码

Option Explicit 

Const strTo As String = "[email protected]" 
Const strCC As String = "[email protected]" '<~~ change "[email protected]" to "" if you do not want to CC 
Const strBCC As String = "[email protected]" '<~~ change "[email protected]" 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如果你是,如果你使用的是Excel 2003中使用Excel 2007年起或C:\Tester.Xls文件为C:\Tester.Xlsm和出口


制备的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并关闭它

enter image description here


建立任务Windows任务调度


您能否确认您的Windows操作系统? - Siddharth Rout 36分钟前

Windows XP。它是我的工作电脑(所以有通常的登录等)。 - keynesiancross 18分钟前

单击开始按钮|所有程序|配件|系统工具|计划任务得到这个窗口

enter image description here

双击 “添加任务计划” 得到这个窗口

enter image description here

单击下一步

enter image description here

点击“浏览“并选择我们之前创建的vbs文件,然后单击o N“打开”

,你得到的是至关重要的,因为它是在这里,我们需要在脚本需要运行

enter image description here

你已经完成了要紧后,点击下一步要提的一个窗口。

enter image description here

在这个窗口中,输入你的登录信息,这样当屏幕被锁定的脚本甚至可以运行。

完成后点击“下一步”,然后在下一个窗口中点击“完成”。你的任务调度程序现在看起来像这样

enter image description here

和你做


锁定您的PC和去休息一下喝杯咖啡;)当你回来(取决于您所设置的时间在任务计划程序中以及您的休息时间)中,电子邮件将被发送。

HTH

+0

这是伟大的,会给它一个去。再次感谢 - 非常感谢。 – keynesiancross 2012-04-18 15:33:21

+0

不是说我有问题,但你在短短几秒钟内选择了我的答案:)你至少应该自己测试一下;) – 2012-04-18 15:34:39

+0

哈哈,我有信心 – keynesiancross 2012-04-18 15:38:08

SendKeys可能是罪魁祸首。我仍然使用CDO通过Excel VBA发送电子邮件。这应该让你开始:http://www.rondebruin.nl/cdo.htm

我用HTML和JavaScript setInterval运行vba代码来克服这样的问题。 代码:

<html> 
<script> 

var flag = false; 
var xlApp; 
var xlBook; 
var i = 0; 

function processExcel() 
{ 
//Open the excel containing macro for the first time 
if(flag==false) 
{ 
    xlApp = new ActiveXObject ("Excel.Application"); 
//Your Excel containing VBA code 
    xlBook=xlApp.Workbooks.Open("Count1.2.xlsm") 

} 
// "a" is the VBA macro 
xlApp.Run("a"); 

flag=true; 


i++; 
window.status="Last Updated " + new Date(); 


} 

var w 
function run() 
{ 

processExcel(); 

w= setInterval("processExcel()",120000); 


} 
function stop() 
{ 

clearInterval(w); 


} 

</script> 

<body > 

<input type="button" value="Start" onclick="run()" /> 
<input type="button" value="Stop" onclick="stop()"/> 


</body> 
</html>