i have developed an outlook macro and want this macro to run automatically without any prompt or mail from the user whether the outlook is on or not.

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
 bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Private Const KEYEVENTF_KEYUP = &H2
Private Const VK_SNAPSHOT = &H2C
Private Const VK_MENU = &H12
Private Const SW_SHOWMAXIMIZED = 3
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_MAXIMIZE = 3
Private Const SW_MINIMIZE = 6
Private Const SW_NORMAL = 1
Private Sub Application_Reminder(ByVal Item As Object)

If TypeName(Item) = "TaskItem" Then
    Dim myItem As TaskItem
    Set myItem = Item
    If myItem.Subject = "run macro Mail_workbook_Outlook_1" Then

        Call Mail_workbook_Outlook_1   '...your macro name here

    End If
End If
End Sub
Sub Mail_workbook_Outlook_1()
    ChDrive ("D:")
    ChDir ("D:\Eclipse\Workspaces\struts\Sonar\src\report")

    'Shell ("cmd.exe /S /K" & "java D:\Eclipse\Workspaces\struts\Sonar\src\reportOpenBrowser")

    'Const exeCmd = "java OpenBrowser"
    'Shell ("java OpenBrowser")

    Dim sdkCommand As String

    sdkCommand = "java Orange "
    Shell ("cmd.exe /c" & sdkCommand)

    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = "check"
        .CC = ""
        .BCC = ""
        .Subject = "Orange "
        .Body = "Hi all " _
                & "PFB the status of Sonar report for Trunk"
        .Attachments.Add ActiveWorkbook.FullName
        'You can add other files also like this
        .Attachments.Add ("d:\\Orange .jpg")
        .HTMLBody = .HTMLBody & "<br>Orange REPORT:<br>" _
                & "<br><img src='d:\\Orange .jpg'" & "width='900' height='600'><br>" _
                & "Orange .<br>" _
                & "google.com" _
                & "<br>Regards,<br>Gaurav</font></span>"
        .Send   'or use .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub
  • its possible to schedule an Excel macro so why not an Outlook macro – Gaurav Sood Nov 25 '14 at 9:56
  • I am using Outlook 2010 – Gaurav Sood Nov 26 '14 at 5:20

You can use the /autorun switch:

outlook.exe /autorun <macroname>

For example, create the following in Visual Basic Editor (Alt+F11):

Sub Hello()
    MsgBox "Hello World!"
End Sub

Create a shortcut on your desktop that has a target of:

"C:\Program Files\Microsoft Office\OFFICE11\OUTLOOK.EXE"  /autorun hello

Double-click on the shortcut and you'll be greeted with a message when you start Outlook.

Note that the path to outlook.exe may vary depending on your version and installation.


You can also define a Application_Startup macro that should run each time Outlook starts:

Private Sub Application_Startup() 

    MsgBox "Welcome, " & Application.GetNamespace("MAPI").CurrentUser 

    Application.ActiveExplorer.WindowState = olMaximized 

End Sub

The above is from the Microsoft Developer Reference site.

You may have to configure your security settings for this to work:

File -> Options -> Trust Center -> Trust Center Settings -> Macro Settings -> Enable All

Note that I don't have Outlook 2010 to confirm the above :-)

  • Sir the new outlook window pops up but the macro does not run automatically – Gaurav Sood Nov 25 '14 at 11:29
  • @GauravSood - I've added a very basic example which I can confirm works on Outlook 2003 (yes, 2003 - don't laugh). If that works, but your script doesn't then it's an issue with your script. – garethTheRed Nov 25 '14 at 11:43
  • @garethTheRed, the security in Outlook 2003 did permit this, but for newer versions its not that easy. Then again, Gaurav Sood did not provide which outlook version he is using. – LPChip Nov 25 '14 at 11:57
  • Sorry for that i am using Outlook 2010 – Gaurav Sood Nov 26 '14 at 5:19
  • Sorry to say but the code doesn't work for outlook 2010 – Gaurav Sood Nov 27 '14 at 4:19

This is impossible. You cannot run an outlook macro without running outlook, in the same way that you cannot play a cd without cd-player.

  • its possible to schedule an Excel macro so why not an Outlook macro – Gaurav Sood Nov 25 '14 at 9:56
  • 1
    You say: to run an outlook even if outlook is not running. That is not possible. By scheduling a macro, you start outlook first, so outlook is always running for this macro. Do note that macro's work very differently for outlook compared to excel, compared to word. – LPChip Nov 25 '14 at 10:14
  • yes it does that's why i am asking how can we autorun the outlook macro – Gaurav Sood Nov 25 '14 at 11:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.