Results 1 to 3 of 3

Thread: If Statement for Outlook VBA: Time & User defined columns

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2015
    Posts
    7

    If Statement for Outlook VBA: Time & User defined columns

    I have revised my code, it's much better now, thank you. I am now receiving an error message for the number of hours line.

    My new code only runs without
    Code:
    + ReceivedTime > 4
    i receive an error message with this statement.The error i receive is "Run Time Error 91, Object variable or with block variable not set"

    So far the overdue column is only populated with "Yes" as the 4 hours are not included.


    Can anyone help with this,
    Code:
    + ReceivedTime > 4
    doesn't seem correct for finding out if the ReceivedTime is more than 4 Hours? I have included my revised code below

    Code:
    For Each msg In myCollection
       '
            'check if received time is > than 4 hours
          If str2 = "A" Then
            'If str2 = "A" + ReceivedTime > 4 Then
            Set objProperty = msg.UserProperties(udf(4))
            objProperty.value = "Yes"'   '"Overdue column to say Yes
    
          Else
            Set objProperty = msg.UserProperties(udf(4))
            objProperty.value = "No" 'if this is not over 4 hours; display No
          End If
    
             msg.Save
    Next
    End If
    End Sub
    Should the hour statement be something like the following?

    Code:
    If str2 = "Create Customer" And ReceivedTime.MailItem = Hour > 4 Then 
    Set objProperty = msg.UserProperties(udf(4))
        objProperty.value = "Yes"
    i receive a run time error for this too

    Thank you

    ____________________________________________________________________________________________________ _____________________________

    Orginal Question

    I am new to VBA and i am finding If/Then statements difficult. I have tried to create this, but i am not sure if this is the correct way.

    I appreciate any help/suggestions to help me understand this a little better.

    What i would like to do is create an If statement for a User Defined Field Column in Outlook. I have created a User defined field column called "Overdue".

    I want to create an if then statement where, if an email is selected and has the value of "A" in UDF 2 and the flag status is not marked as complete; then there is a timer of 4 hours for this task to be completed- but the task timer should start as soon as the mail item reaches the mailbox.

    If this is not overdue, then Column "Overdue"(UDF4) will stay as "No", if this is Overdue; >4 hours, this column will be set as the value "Yes"

    Below is the code that i have been working on:

    Code:
       For Each msg In myCollection
       If msg.FlagStatus = olFlagComplete Then
       End If
    
        If str2 = "A" + TaskDueDate = ReceivedTime > 4 Then 'check if received time is > than 4 hours
    
        ''''If Hour (?)()) < 4 Then (comment:alt line of code)
        msg.str4 = "Yes" 'if the received time for that message is more than 4 hours set Overdue column to say Yes
        Set objProperty = msg.UserProperties(udf(4))
    
        If TaskDueDate = ReceivedTime < 4 Then
        msg.str4 = "No"
            Set objProperty = msg.UserProperties(udf(4))
    
        If (objProperty Is Nothing) Then
           Set objProperty = msg.UserProperties.Add(udf(4), olKeywords)
    
     End If 
        objProperty.value = "No"`
    
         msg.Save
         Next
         End If
             End If
        End Sub
    Last edited by Soneeka; Oct 6th, 2015 at 01:01 PM. Reason: revised code included

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: If Statement for Outlook VBA: Time & User defined columns

    If msg.FlagStatus = olFlagComplete Then
    End If
    this does exactly nothing

    If str2 = "A" + TaskDueDate = ReceivedTime > 4 Then
    i presume this should be
    Code:
    If str2 = "A" And TaskDueDate = ReceivedTime + 4 Then
    but it is a guess

    both of the last 2 end if should be before the next

    msg.str4
    i doubt that a message has a str4 property, if it is a userdefined field, should be more like
    Code:
    msg.UserProperties(str4) = "YES"
    assuming str4 is a variable

    you can also assign a yes no value to a field like
    Code:
    msg.UserProperties(str4) = format(str2 = "A" And TaskDueDate = ReceivedTime + 4,"yes/no")
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2015
    Posts
    7

    Re: If Statement for Outlook VBA: Time & User defined columns

    Thank you

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width