Subscribe to Windows IT Pro
March 19, 2001 12:00 AM

More Fun with Dates

Windows IT Pro
InstantDoc ID #20078
Rating: (2)
Downloads
20078.zip

All my 2001 Outlook VBA on Demand columns have focused on date handling in Microsoft Outlook. The subject is popular—my Inbox and the Outlook newsgroups have recently been swamped with questions about using dates. Therefore, by popular demand, in this column I show you how to build a few more date procedures.

The first procedure, which you see in Listing 1, is a function that rounds any number of minutes up to the next quarter hour. I wrote the RoundUpToQuarter() function for someone who needed to track a support technician's time by the quarter hour on an Outlook form. You probably recognize the Mod operator from my March 15 column, in which you learned that Mod gives you the remainder of a division operation. The RoundUpToQuarter() function uses Mod to check whether the intMinutes argument is evenly divisible by 4. (If so, then the argument is already an exact quarter hour, and no changes are necessary.)

The RoundUpToQuarter() function also introduces the built-in Fix() function, which might be new to you. The function returns the integer portion of a number. As Table 1 shows, subtle differences exist between using Round() to round a number and using Fix()—or a similar function, Int()—to return the integer portion. Of course, Round() can round to any number of decimal places. If you use Round () without a decimal place argument, the function rounds up or down to the nearest integer.

The second procedure, which you see in Listing 2, calculates the number of whole years that have elapsed between two dates. You might use the YearsSinceDate() procedure to calculate a person's age on a particular date. Somewhat surprisingly, the DateDiff() function can't solely perform such a calculation.

For example, suppose today's date is April 1, 2001. If you have one friend who was born July 1, 1970, and another friend who was born March 1, 1970, the following DateDiff() expressions will both return 31 years, even though your younger friend will be only 30 years old when April Fool's Day rolls around.

DateDiff("yyyy", #3/1/1970#, #4/1/2001#)
DateDiff("yyyy", #7/1/1970#, #4/1/2001#)

DateDiff() rounds up to the nearest year. If you want an accurate count of the number of years that have passed between two dates, you must compensate for that rounding.

The YearsSinceDate() function compensates by first verifying whether the anniversary of the earlier of the two dates has already occurred during the year of the later of the two dates. If not, the function subtracts 1 from the result of the DateDiff() function that compares the two dates. Listing 1 and Listing 2 are suitable for use either in Outlook VBA or in VBScript on an Outlook form.

The StampDate() procedure, which you see in Listing 3, stamps today's date and the current user's name at the bottom of the currently open item's message box or notes box. The ideal way to use this procedure is to add the StampDate macro to a toolbar button.

You need to be aware of two effects of the StampDate() procedure. First, by changing the Body property, the procedure makes all the item's previous formatting disappear. The text reverts to the default font that the user set.

Second, the object model guard features in the Outlook Email Security Update and Office 2000 Service Pack 2 (SP2) block the CurrentUser property. When the code attempts to retrieve the CurrentUser property, Outlook displays a dialog box that asks the user to approve access to the address book. If the user doesn't approve address book access, StampDate() handles the resulting error and substitutes the text user name not available for the username.

Now you have several months' worth of Outlook date-handling procedures in your arsenal. You should be confident enough to take on any date challenge that Outlook might toss your way—even if you simply want to keep track of the ages of your friends.

Related Content:

ARTICLE TOOLS

Comments
  • Mark B
    8 years ago
    Oct 21, 2004

    Is there a function available for outlook to calculate "networkdays" without inserting an Excel spreadsheet into the form?

    I am trying to perfect a Vacation Request form to discount weekends but can only find the formula in Excel.

  • mal4mac
    8 years ago
    Sep 17, 2004

    Check out this tutorial:

    http://www.321books.co.uk/ebooks/outlook-vba-tutorial.htm

  • Sue Mosher
    8 years ago
    Mar 03, 2004

    Kevin, Outlook provides no way to set the cursor position.

  • Kevin Herman
    10 years ago
    Jan 15, 2002

    I just read your "More Fun with Dates" article and followed your instructions on Listing 3. How do I get the cursor to position at the end of the date stamp or on the line below. Currently the cursor returns to the upper left.

  • richard
    11 years ago
    Jul 19, 2001

    1. Quarter of an hour is 15 minutes - rounding to the nearest multiple of 4 minutes will *not* round to quarter of an hour!
    2. Using CDate(Month & "/" & Day & "/" & Year) will only work on a US system. Use DateSerial(Year,Month,Day) instead.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.