2012-04-18 72 views
12

我在使用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

回答

18

我會打破這個 「指南」,在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

1

我用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>