execute a vbs every 30 minutes

11,245

Solution 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.

Solution 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
Share:
11,245
user1803052
Author by

user1803052

Updated on June 15, 2022

Comments

  • user1803052
    user1803052 almost 2 years

    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