Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  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 log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
No Jobs, No Excitement at Apple's Last Macworld Keynote

Apple CEO Steve Jobs made the right move in skipping out on his company's last appearance at Macworld: In a Tuesday keynote address at the conference, Apple had no interesting new products to sell, opting instead to spend mind-numbing amounts of time on ...

Where is Microsoft NetMeeting in Windows XP?

...

The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...


Exchange Server and Outlook Whitepapers Protecting (You and) Your Data with Exchange Server 2007

StoreVault SnapManagers for Microsoft Exchange and SQL Server

Related Events Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Cloud Computing Forum: Integrating Software, Server and Storage as a Service into Your Enterprise IT Delivery Model

Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

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 Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


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 Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing