Using a Macro to Insert a Custom Formatted Date

ID: Q121983

The information in this article applies to:

SUMMARY

This article describes how to write a macro that prompts you to enter a number of days from the current day and then inserts the resulting date in a custom format.

MORE INFORMATION

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code "as is" without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

1. From the Tools menu, choose Macro. Type a name for the new macro and

   choose the Create button.

2. In the macro editing window, enter the text below. (Sub MAIN and End Sub
   are already listed.)

   Word 7.0
   --------

   Sub MAIN
    Defaultdate$=GetPrivateProfileString$(
     "HKEY_CURRENT_USER\software\Micr soft\Word\7.0\Options","dateformat",
      "")
    SetPrivateProfileString(
     "HKEY_CURRENT_USER\software\Microsoft\Word\7.0 Options", "dateformat",
     "MMMM d,yyyy", "")
    Days$ = InputBox$("Please enter the number of days from Today ")
    NumDays = Val(days$)
    serialEndDate = Now() + numDays
    EndDate$ = Date$(serialEndDate)
    Insert enddate$
    SetPrivateProfileString
     "HKEY_CURRENT_USER\software\Microsoft\Word\7.0\Options", "dateformat",
     "defaultdate$", ""
   End Sub

   This macro prompts you to enter a number of days from today and then
   inserts the resulting dat in "MMMM d, yyyy"(December 31, 2000) format.

   Word 6.0
   --------

   Sub MAIN
    Defaultdate$ = GetPrivateProfileString$("microsoft word",
     "dateformat","winword6.ini")
    SetPrivateProfileString "microsoft word", "dateformat", "MMMM d,
     yyyy","winword6.ini"
    Days$ = InputBox$("Please enter the number of days from today")
    NumDays = Val(days$)
    SerialEndDate = Now() + numDays
    EndDate$ = Date$(serialEndDate)
    Insert enddate$
    SetPrivateProfileString "microsoft word", "dateformat", defaultdate$,
     "winword6.ini"
   End Sub

   This macro prompts you to enter a number of days from today and then
   inserts the resulting date in "MMMM d, yyyy"(December 31, 2000) format.

3. From the File menu, choose Close, and be sure to save the changes to the
   macro.

4. To run the macro, choose Macro from the Tools menu, select the macro
   name you assigned in step 1, and choose the Run button.

To insert a date for 30 days from today, use the following macro:

   Word 7.0
   --------

   Sub MAIN
    Defaultdate$ = GetPrivateProfileString$
     (HKEY_CURRENT_USER\software\Microsoft\Word\7.0\Options", "dateformat",
     "")
    SetPrivateProfileString
     "HKEY_CURRENT_USER\software\Microsoft\Word\7.0\Options", "dateformat",
      MMM d, yyyy", ""
    NumDays = 30
    SerialEndDate = Now() + NumDays
    EndDate$ = Date$(serialEndDate)
    Insert enddate$
    SetPrivateProfileString
     "HKEY_CURRENT_USER\software\Microsoft\Word\7.0\Options", "dateformat",
      "defaultdate$", ""
   End Sub

Word 6.0

 Sub MAIN
  Defaultdate$ = GetPrivateProfileString$("microsoft word",
   "dateformat","winword6.ini")
  SetPrivateProfileString "microsoft word", "dateformat", "MMMM d,
   yyyy", "winword6.ini"
  NumDays = 30
  SerialEndDate = Now() + numDays
  EndDate$ = Date$(serialEndDate)
  Insert enddate$
  SetPrivateProfileString "microsoft word", "dateformat", defaultdate$,
   "winword6.ini"
 End Sub

KBCategory: kbusage kbmacro KBSubcategory: kbmacroexample Additional reference words: 6.0 6.0a 6.0c word6 7.0 word95 word7 winword
Keywords          : kbmacroexample 
Version           : 6.0 6.0a 6.0c 7.0
Platform          : WINDOWS

Last Reviewed: February 6, 1998