Using a Macro to Insert a Custom Formatted Date

Last reviewed: February 5, 1998
Article ID: Q121983
The information in this article applies to:
  • Microsoft Word for Windows, versions 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows 95, version 7.0

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


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 5, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.