execute a vbs every 30 minutes

放肆的年华 提交于 2020-01-14 02:09:52

问题


I want outlook to email a server every 30 mins in real time. is there a way i can do it in this current code or via batch file? making a scheduled task is unavailable and no third party software

set objOutlook = CreateObject( "Outlook.Application" )
set objMail = objOutlook.CreateItem(0)

strMessage = "Test"

ON ERROR RESUME NEXT
With objMail
    .From = "email"
    .To = "email"
    .Subject = "Test"
    .Body = strMessage
    .Save
end with

objMail.OriginatorDeliveryReportRequested = True
objMail.Display
objMail.Send

WScript.quit

回答1:


An example of sleeping with VBScript:

Dim waittime : waittime = 30 * 60 * 1000    

do 

    ' Insert your code here

    WScript.Sleep(waittime)
loop

Oh, and get rid of the WScript.Quit statement, that will... quit your script!

EDIT

Another way to do it and integrate the messagebox is using the WshShell.Popup:

do

    ' Insert your code here

    Set WshShell = CreateObject("WScript.Shell")
    If WshShell.Popup ( "Sending a mail every 30 minutes" & vbNewLine & _
                        "Press Cancel to stop or OK to send a mail right now.", _
                            waittime, "Automatic mail sender", 1 or 32 or 4096) = 2  Then
        exit do
    end if

loop

This snippet will execute the code every waittime, it stops when the Cancel button is pressed and it will execute the code immediately when OK is pressed.




回答2:


Either use a while true loop in vbs (preferred), or use a loop in batch file (dirty way).

:back
myscript.vbs
ping -n 1800 localhost >nul 2>nul 
REM ping -n is a dirty way of sleeping for system <= winxp
REM for win7+ systems, use command `timeout 1800`
goto back

Wscript approach:

while true
    set objOutlook = CreateObject( "Outlook.Application" )
    set objMail = objOutlook.CreateItem(0)

    strMessage = "Test"

    ON ERROR RESUME NEXT
    With objMail
        .From = "email"
        .To = "email"
        .Subject = "Test"
        .Body = strMessage
        .Save
    end with

    objMail.OriginatorDeliveryReportRequested = True
    objMail.Display
    objMail.Send
    wscript.sleep(30*60*1000)
wend


来源:https://stackoverflow.com/questions/13251397/execute-a-vbs-every-30-minutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!