Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


April 2001

More Fun with Dates


RSS
Subscribe to Windows IT Pro | See More Visual Basic (VB) Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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.

End of Article



Reader Comments
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.

Richard July 19, 2001


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.

Kevin Herman January 15, 2002


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

Sue Mosher March 03, 2004


Check out this tutorial:

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



mal4mac September 17, 2004 (Article Rating: )


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.

Mark B October 21, 2004 (Article Rating: )


You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...


Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Take Control of Your Email: Understand the Business Reasons for Email Storage Management

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

Bail Out Your Exchange Environment

Continuous Application Virtualization: An Answer to Exchange Recovery Problems

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement