[RESOLVED] Outlook VB Macro - Deffered Delivery Time-VBForums
Results 1 to 6 of 6

Thread: [RESOLVED] Outlook VB Macro - Deffered Delivery Time

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Posts
    3

    Resolved [RESOLVED] Outlook VB Macro - Deffered Delivery Time

    Trying to write a macro for Outlook 2003 that will check the current date/time and either send the current email immediately, or will delay it until the next business day at 8:30am. I'm a VB rookie, but have put together the following code (which isn't compiling correctly) to show where I'm at:

    Code:
    Public Sub CheckSendTime()
        Dim obj As Object
        Dim Mail As Outlook.MailItem
        Dim WkDay As String
        Dim SendHour As Integer
        Dim SendDay As Date
        Dim SendTime As Date
                
    SendDay = DateValue(Now)
    SendTime = TimeValue(Now)
    SendHour = Hour(Now)
    
    If SendHour < 8 Then
        SendHour = 8 - SendHour
        SendTime = SendTime.AddHours(SendHour)
    End If
    If SendHour > 18 Then
        SendHour = 32 - SendHour
        SendTime = SendTime.AddHours(SendHour)
                
    WkDay = Weekday(Today)
    If WkDay = 1 Then SendDay = SendDay.AddDays(1)
    End If
    If WkDay = 7 Then SendDay = SendDay.AddDays(2)
    End If
    
      Set obj = Application.ActiveInspector.CurrentItem
      If TypeOf obj Is Outlook.MailItem Then
        Set Mail = obj
        Mail.DefferedDeliveryTime = SendDay & SendTime
        Mail.Send
      End If
    
    End Sub
    Any help you can give to assist would be greatly appreciated. Thanks in advance!

    Mark

  2. #2
    Addicted Member
    Join Date
    Jun 2009
    Location
    C:\Windows\SysWOW64\
    Posts
    200

    Re: Outlook VB Macro - Deffered Delivery Time

    Does it show any error? If so, where does the yellow arrow point to?


  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,139

    Re: Outlook VB Macro - Deffered Delivery Time

    Thread moved to Office Development/VBA forum (note that the "VB Editor" in Office programs is actually VBA rather than VB, so the VB6 forum is not really apt)

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Posts
    3

    Re: Outlook VB Macro - Deffered Delivery Time

    Error is kicking out at SendTime = SendTime.AddHours

    Compile error:
    Invalid Qualifier

    Thanks for the help!

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Posts
    3

    Re: Outlook VB Macro - Deffered Delivery Time

    Believe I've figured out the problem(s). Here's the corrected code:

    Code:
    Public Sub CheckSendTime()
        Dim obj As Object
        Dim Mail As Outlook.MailItem
        Dim WkDay As String
        Dim MinNow As Integer
        Dim SendHour As Integer
        Dim SendDate As Date
        Dim SendNow As String
        
    'Set Variables
    SendDate = Now()
    SendHour = Hour(Now)
    MinNow = Minute(Now)
    WkDay = Weekday(Now)
    SendNow = Y
    
    'Check if Before 8am
    If SendHour < 8 Then
        SendHour = 8 - SendHour
        SendDate = DateAdd("h", SendHour, SendDate)
        SendDate = DateAdd("n", -MinNow, SendDate)
        SendNow = N
    End If
    'Check if after 7PM
    If SendHour > 19 Then           'After 7 PM
        SendHour = 32 - SendHour    'Send a 8 am next day
        SendDate = DateAdd("h", SendHour, SendDate)
        SendDate = DateAdd("n", -MinNow, SendDate)
        SendNow = N
    End If
                
    'Check if Sunday
    If WkDay = 1 Then
        SendDate = DateAdd("d", 1, SendDate)
        SendNow = N
    End If
    
    'Check if Saturday
    'If WkDay = 7 Then
    '    SendDate = DateAdd("d", 2, SendDate)
    '    SendNow = N
    'End If
    
    'Send the Email
      Set obj = Application.ActiveInspector.CurrentItem
      If TypeOf obj Is Outlook.MailItem Then
        Set Mail = obj
        'Check if we need to delay delivery
        If SendNow = N Then
          Mail.DeferredDeliveryTime = SendDate
        End If
        Mail.Send
      End If
    
    End Sub

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,139

    Re: Outlook VB Macro - Deffered Delivery Time

    That's the correction I would have recommended.


    As you now have it sorted out, could you please do us a little favour, and mark the thread as Resolved?
    (this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)

    You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.