A Pop-up Calendar for Word Part 1: A Simple Calendar


Did you ever need to check a date before you typed it into a document? This tutorial shows you how to create a pop-up calendar using the Microsoft ActiveX Calendar Control that is installed with Office. You will be able to call up the calendar with a keyboard shortcut, from a toolbar button or menu, or from the right-click context menu. When you select a date it is automatically entered into your document at the insertion point...

A pop-up calendar for entering dates into Word documents

The calendar will be build on a VBA UserForm and powered by VBA code. It will insert a date in a pre-defined format which you decide when you create the tool. If you want to add further refinements, such as allowing the user to choose the date format and set it as the default if they wish, you can continue with the second part of the tutorial: A Pop-up Calendar for Word Part 2: Customizing the Calendar.

Already Made a Pop-up Calendar for Excel?

If you have already made a calendar in Excel using my tutorial A Pop-up Calendar for Excel you will find that the UserForm used here is the same as the one in the Excel tutorial. However, the code behind the form is different so you might like to skip the first part of this tutorial and jump straight to the coding section.

Isn't There Already a Date Tool?

Microsoft Word is equipped with a tool for inserting the current date into a document. The command Insert > Date displays a dialog box inviting you to select a format:

Word's Insert Date and Time command

You can also choose whether to have the date update itself automatically (it gets inserted as a Word field) or not (it gets inserted as regular text). You can set your chosen format to be the default. It's a useful tool but it doesn't help you find the date you want!

The Date and Time dialog box

Where Does the Code Go?

You can build the calendar in Normal.dot which is Word's "Normal" template. It is loaded whenever Word opens so if placed here, you calendar will always be available to you. If you plan to distribute the calendar tool to other users it would be better to create it in a new, empty document which can be saved as a template. This template can be loaded automatically like an Add-In, so that it is available whenever Word opens, without the need to alter individual users' copies of Normal.dot.

Building the Calendar

Step 1: Open a New UserForm

Open Microsoft Word. If you are planning to create your calendar in a template you will need a new, empty document. Next, open the Visual Basic Editor by choosing Tools > Macro > Visual Basic Editor (or keys: Alt+F11).

In the Project Explorer window, normally in the upper left corner of the Visual Basic editor) locate and click on the document in which you want to build the calendar, either Normal (the Normal Template) or Project (Document1) (where "Document1" is the name of your new, empty document in Word). If the Project Explorer is not visible, open it from the View menu.

Select the document in the Project Explorer

In this example I am going to create the calendar in the new document (shown here as "Document1").

Insert a UserForm   With your chosen host document selected, choose Insert > UserForm to open a new blank UserForm [click the thumbnail below to see a full-sized image of the Visual Basic Editor window]...

Click the thumbnail to see a full-sized image

The Properties Window (usually located in the lower left corner of the Visual Basic Editor) displays all the properties of the the currently selected object. If the Properties Window is not visible open it from the View menu. Click on the UserForm to select it and make  the following changes in the Properties Window:

      Name:   frmCalendar
Caption:   Select a Date

Step 2: Locate the Calendar Control Tool

You need to draw a Calendar Control (active objects on forms are called "controls") on the UserForm, but the one you need is not normally included in the Toolbox. If you haven't already installed the Calendar Control tool, go to Tools > Additional Controls. If you find that the Additional Tools option is greyed-out, it's because the Toolbox is not visible. Switch it on with View > Toolbox. In the dialog box find Calendar Control 10.0 (10.0 for Word 2002, 9.0 for Word 2000 and 8.0 for Word 97) and select it. Click the <OK> button to close the dialog. This adds a new button to the toolbox, labelled Calendar:

Open the Additional Controls dialog  >>  Select the Calendar Control  >>  The new Calendar tool appears in the Toolbox


NOTE: The Calendar Control is an ActiveX control (actually a file called mscal.ocx) supplied with Microsoft Office. It is normally installed with a standard installation of Microsoft Office Professional or Microsoft Access, but if you can't find it on the list you will need to get hold of a copy. If you are distributing your file, or planning to use it on more than one computer, you will also need to make sure that the host computer has the mscal.ocx file installed. You will find it on the CD that your copy of Microsoft Office Professional came on, or you can download a copy from here.

[Follow this link for full instructions on downloading and installing the mscal.ocx ActiveX Calendar Control file.]

Step 3: Add a Calendar Control to the UserForm

Click the Calendar tool then click on the UserForm near its upper left corner to place a calendar on the UserForm.

Click on the form near the upper left corner...  >>>  An ActiveX Calendar Control on a UserForm

Make sure the calendar is selected then take a look at the choices in the properties window. Click on the Custom category (near the top of the list) and click the [...] button to open the calendar control's custom properties dialog. You can format the calendar any way you like. I made the following changes...

      General - Show Month/Year Title:   No
      Font - DayFont Size:   Size 8, Not Bold 
      Font - GridFont:   Size 8

Omitting the title and making the text smaller allowed me to resize the calendar to a smaller rectangle by dragging the resize handles (white rectangles)...

Resize the Calendar Control

Now click on the UserForm itself so that you can see its resizing handles and resize it to fit the calendar...

Resize the UserForm to fit the calendar

To get a preview of how the calendar will look click the F5 key. When you do this the UserForm will open in Word as it would in use. Click the UserForm's close button ([x]) to return to the Visual Basic Editor.

Step 4: Add a Command Button

If the user opens the calendar by mistake, they will be able to close it by clicking the [x] button without inserting a date on the document. Most users also expect a form or dialog box to close without making changes if they press the [ESCAPE] key on their keyboard. This doesn't happen by itself. You have to program the UserForm to respond to this event. To do this you will place a command button on the UserForm which closes it when clicked. By setting the button's Cancel property to True the effect of the user hitting the [ESCAPE] key will be to programmatically click the button. The user doesn't need to see the button, so you can hide it behind the calendar.

Click the Command Button tool on the toolbox then click on the calendar (just left of centre) to place a command button on the UserForm. Note that the command button places itself behind the calendar by default, although you can still see its outline when it is selected...

Place a Command Button on the UserForm

With the command button selected, make the following changes in the properties window...       Name:   cmdClose
Cancel:   True

Step 5: Write Code for the Command Button

This step adds some code to the Click event of the command button so that when clicked (i.e. when the user hits the [ESCAPE] key) the UserForm closes without anything else happening.

Go to View > Code (Keys: F7) to open the UserForm's code window. If the command button was still selected there will already be an entry. If not, choose cmdClose from the left hand (General) drop-down list at the top of the code window. It should automatically select Click from the right-hand (Declarations) list. Place your cursor between the Sub... and End Sub lines and press the [TAB] key to indent your code. Type Unload Me. Your line of code will look like this:

Code to close the Userform

Use [CTRL]+[TAB] (or go to Window > frmCalendar(UserForm)) to switch back to the UserForm window and test the code. Press [F5] to open the form in Word. Notice that the command button is invisible (it is behind the calendar). Press the [ESCAPE] key and see that the UserForm closes.

Step 6: Write Code for the Calendar

This step builds the code that will transfer the date that the user chooses to the insertion point on the Word document. If anything is selected the selection will be replaced by the date.

Return to the UserForm's code window. Open the left-hand (General) drop-down list at the top of the code window and choose Calendar1. If it is not already selected, choose Click in the right-hand list. This enters the start and end lines of theCalendar1_Click procedure.

Place the cursor between the two lines and press [TAB] to indent your code (indenting blocks of code is good practice - it makes the code easier to read) then type:

      Selection.Text = Format(Calendar1.Value, "dd mmmm yyyy")

...which transfers the date selected on the calendar to the insertion point on the document.

[NOTE: You can use any date format you choose. If you are unfamiliar with date format codes, see the explanation at the end of this tutorial.]

On the next line type:

      Selection.MoveRight Unit:=wdCharacter, Count:=1

...which moves the insertion point to the end of the date text, ready for the user to continue typing. On the next line type:

      Unload Me

...which closes the form. Your code should look like this:

Code to insert the date into the document and close the UserForm

To make the calendar a bit more user-friendly you will now add some code that checks to see if the user's selection already contains a date. If it does, then the calendar will show the same date when it opens. If there is no text selected (or if the selected text is not a date) the calendar will show today's date. To do this we make use of the UserForm's Initialize event, which happens as the UserForm opens.

Open the left-hand drop-down list at the top of the code window and choose UserForm, then open the right-hand list and choose Initialize. (You will notice that when you chose "Initialize" the start and end lines for the UserForm_Click procedure were created. You don't need these so it is safe to delete them.)

Place your cursor between the start and end lines of the UserForm_Initialize procedure and type the following:

      If IsDate(Selection.Text) Then
            Calendar1.Value = DateValue(Selection.Text)
            Calendar1.Value = Date
      End If

This IF Statement checks the current selection for a date. If it is a date then the calendar displays the same date. If not it uses the VBA Date function to display the current date. Your completed code should look like this:

Code to set the calendar's initial date

Now you need to write some code to display the UserForm on demand:

  • If you are working in a new document, double click the ThisDocument entry in the Project Explorer. This opens the document's own code module in the Visual Basic Editor window.
  • If you are working in Normal.dot you may already have a code module (if you already have some Word macros of your own). Otherwise, or if you want to use a new module, go to Insert > Module to create one. Double-click the name of the module in the Project Explorer window to open its code window (or right-click it and choose View Code).

Type the line Sub OpenCalendar and press enter. The Visual Basic Editor places a pair of brackets at the end of the line and adds the line End Sub, placing your cursor between them. Press [TAB] then type frmCalendar.Show. Your code should look like this:

Code to display the UserForm

This simple procedure will display the UserForm when run from Word.

Step 7: Test the Code

At this point it is a good idea to save your file in case something goes wrong! In the Visual Basic Editor window select your file in the Project Explorer and choose File > Save Document1 (or whatever your file is called) and give it a suitable name. Save it as a document for now - you will convert it to a template later.

If you are working in Normal.dot select Normal in the Project Explorer and choose File > Save Normal.

Switch to Word and place the cursor where you want to insert a date. Choose Tools > Macro > Macros (Keys: Alt + F8) to open the Macro dialog. Make sure that the macro OpenCalendar is selected and click <Run>.

The Macro dialog ready to run the OpenCalendar procedure

When the calendar opens it will display today's date. Choose a date (NOTE: always choose month and year before choosing day because clicking on a day fires the Calendar1_Click event). When you click the day the calendar closes and enters your date into the document.

Now type a date into your document, select it, and run the macro. If the program recognises that your selection is a date, the calendar will display that date when it opens. If you wish you can choose another date, or you can press the [ESCAPE] key to close the calendar without making any changes.

Step 8: Install the File

If you have been working in Normal.dot you don't need to do anything else, but remember to save the changes to the Normal Template if Word asks you when you close it down. If you intend to distribute your calendar, you will have followed my advice earlier to create it in a new document and later save it as a template. If you want to add the extra features of a keyboard shortcut and right-click menu option, continue with the tutorial. Otherwise jump to the final section "Install the Calendar" below...

Adding Extra Features

Adding a Toolbar Button or Menu Item

It is usually convenient to assign your macros to custom toolbar buttons or menu items. The method for doing this was covered in detail in my tutorial A Pop-up Calendar for Excel so I won't repeat it here. That tutorial also describes how to add a command to the context (right-click) menu and set a keyboard shortcut programmatically. The method for doing that in Word is slightly different from that in Excel so here's how to do it...

Assign a Keyboard Shortcut with Code

In the Visual Basic Editor double-click the ThisDocument entry in the Project Explorer for the document that contains your calendar. Open the left-hand (General) drop-down list at the top of the code window and choose Document and in the right-hand (Declarations) list choose Open. This creates the start and end lines for the Document_Open procedure. This is a special macro that runs automatically whenever the document is opened. Between the start and end lines type the lines:

      CustomizationContext = NormalTemplate
      KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _
            wdKeyShift, wdKeyC), KeyCategory:=wdKeyCategoryMacro, _

NOTE: To make the code easy to read, some of the code lines have been broken using the VBA line-break character which is a Space followed by an Underscore (  _). This character ensures that the code is read as a single line. Without it the code would not be accepted.

These statements assign the keyboard shortcut Ctrl+Shift+C to the macro that opens the calendar UserForm. You can choose a different combination of keys if you wish. If your OpenCalendar procedure is in a different module you must enter the module name instead of "ThisDocument" or omit it (and the dot separator) altogether. Here's how the code should look:

Code to assign a keyboard shortcut

This keyboard shortcut is assigned whenever the document is opened. It is a good idea to create a corresponding procedure to cancel the shortcut when the document closes.

In the same way as you did above, create a Document_Close procedure and enter the line:

      On Error Resume Next

followed by the lines:

      CustomizationContext = NormalTemplate
      KeyBindings.Key(KeyCode:=BuildKeyCode(wdKeyControl, _
            wdKeyShift, wdKeyC)).Clear

The first line tells Word to ignore any error that might occur in this procedure (this is safe to do here because the only possible error is that there is no such keyboard shortcut, so if the instruction to delete it fails it's OK to ignore it). Then comes the statement which cancels the keyboard shortcut assignment as the document closes. The code should look like this:

Code to remove the keyboard shortcut

You will need to save and close the workbook and then reopen it before you can test your keyboard shortcut. If you are using Normal.dot you will need to close Word and reopen it. Save the file first!

Add an Item to the Right-Click (Context) Menu

It is also a simple matter to place a command on Word's text context menu. This will allow the user to right-click where the date is to be inserted and choose from the pop-up menu.

An "Insert Date" command added to the right-click menu

This is how you add an "Insert Date" entry (or whatever wording you choose) to the right-click menu. As with the procedure for assigning a keyboard shortcut, this requires two sets of code: one to create the menu item when the document opens, and another to remove it when the document closes.

First, find the Document_Open procedure in which you placed the code for creating a keyboard shortcut (or if you chose not to use a shortcut, create the procedure as described above). Insert the following line at the top of the procedure:

      Dim NewControl As CommandBarControl

Move to the end of the procedure and add the line:

      Set NewControl = Application.CommandBars("Text").Controls.Add

These two lines add the new control (i.e. menu item) to the menu (called in VBA a "command bar") that appears when the user right-clicks on text (i.e. on the editable part of a page). The next step is to describe the control's properties. Enter the following:

      With NewControl
            .Caption = "Insert Date"
            .OnAction = "ThisDocument.OpenCalendar"
            .BeginGroup = True
      End With

As before, if your OpenCalendar procedure is in a different module you must enter the module name instead of "ThisDocument" or omit it (and the dot separator) altogether.

The finished Document_Open procedure looks like this:

Code to assign a keyboard shortcut and add a new menu item

Finally, add this line to the end of the Document_Close procedure:

      Application.CommandBars("Text").Controls("Insert Date").Delete

This line removes the entry from the right-click menu as the document closes. The finished Document_Close procedure looks like this:

Code to remove the keyboard shortcut and the menu item

Help! I've Got Too Many Menu Items...

Multiple entries mean your code isn't working properly!   This usually happens because either you have forgotten to add the line that removes the menu item, or you have specified the wrong name, or simply made a typo. Don't panic! Just open the Visual Basic Editor and then open the Immediate Window (choose View > Immediate Window or Keys: Ctrl+G).

The Immediate Window has many uses but one of them is the ability to execute a line of code just by typing it into the window and pressing the Enter key.

Into the Immediate Window type the line:

      Application.CommandBars("Text").Controls("Insert Date").Delete

then press the [Enter] key. Go back onto Word and right-click on the page. One of the "Insert Date" entries should have disappeared. Do it as many times as necessary to remove the unwanted copies. Then look through your code and try to find the error that is causing the problem.

Installing the Calendar

Unless you have been working in the Normal template, your calendar will now have to be saved as a Word template (*.dot) file. So, the next step is to save your working document as a template in Word's Startup folder. The reason for storing it here is that it will open automatically (and invisibly) whenever Word is started so the pop-up calendar tool will be available to any document.

First you need to ascertain the location of the Word's Startup folder on your computer. This differs with different versions of Windows and Office, and several programs have their own Startup folders, so it is important to get the right one. In Word choose Tools > Options > File Locations and locate "Startup" in the list:

The location of the Startup folder in Tools > Options > File Locations

The path to the Startup folder You will probably find that the path is too long to be shown in full so select Startup in the list and click the Modify button (or double-click the Startup entry). In the Modify Location dialog click the down-arrow in the Look in: box to view the full path to the folder. Make a note of the path then click Cancel and Close to return to Word.

To save the file as a template choose File > Save As and change the Save as type: to Document Template (*.dot), give the template a suitable name (I called mine WordPopupCalendar) and navigate to the Startupfolder. Click the Save button.

To automatically install the Popup Calendar template for the first time you must close Word and open it again. After that it will load by itself each time Word opens.

Using the Pop-up Calendar

From now on each time you start Word the Pop-up Calendar will be available by using the keyboard shortcut you assigned or from the new item on the right-click menu.

If you chose not to add these enhancements you can open the calendar by running the OpenCalendar macro as described in Step 7: Test the Code above. You can also add a custom toolbar button or menu item if you wish.

Deactivating the Pop-up Calendar Template

You can temporarily deactivate the Pop-up Calendar template from Word by choosing Tools > Templates and Add-Ins where you will see it listed in the Templates and Add-Ins dialog. Remove the tick from the checkbox next to the template's name to unload the template for the current session. If you want to prevent the template from loading at startup you should also remove the template file from the Startup folder. [Click the thumbnail below to see a full-sized image of the Templates and Add-Ins dialog.]

Click the thumbnail to see a full-sized image

Modifying the Pop-up Calendar Template

The Visual Basic Editor will not allow you to view the code of a template that is loaded from the Startup folder. If you attempt to do so a message will be displayed notifying you that the "Project is unviewable". If you need to modify the code, first deactivate the template as described above, then open the file in Word using the File > Open command. You will then be able to work on the code as normal.

The code of the loaded template is locked

Different Versions of Word

The VBA code used in this tutorial is suitable for all current versions of Microsoft Word (97, 2000 and 2002). If you create the Pop-up Calendar template in one version of Word and plan to use it in another version you should not encounter any problems but it is a good idea to check first. Each version of Microsoft Office comes with its own version of the ActiveX Calendar Control (8.0 for Office 97, 9.0 for Office 2000 and 10.0 for Office 2002). The first time you activate the template in a version of Office different from the one in which you created it, the program should automatically substitute the existing Calendar Control to the one current for that version. I found that when converting back from Word 2002 to Word 2000 this resulted in a change in the font size of the calendar buttons. This was easily fixed by redrawing the calendar in the current version.

Date Codes Explained

There is a standard notation for specifying date formats using a combination of codes for day, month and year (also hour, minute and second of you wish to specify time):

Day Month Year
d = 1 m = 1 yy = 01
dd = 01 mm = 01 yyyy = 2001
ddd = Mon mmm = Jan  
dddd = Monday    mmmm = January     

Using these codes, in conjunction with slashes, spaces or other punctuation characters you can construct any date format you choose, for example:

Date Code Displays Date
d/m/yy 1/1/01
dd-mm yyyy 01-01 2001
dddd, dd mmmm yyyy    Monday, 01 January 2001

In different parts of the world there are different conventions for writing dates. In the US dates are normally written in the order Month, Day, Year but in Europe Day, Month, Year is used. If you are not sure where your users will be located you may not want to "hard code" a date format into your code as this might lead to misunderstandings. Instead you can display the date based on the computer's date settings in the Regional Settings section of the Windows Control Panel. In VBA you can still retain a degree of control by choosing one of the VBA date formats, for example:

Date Format in VBA US Example European Example
Format(Date,"Short Date") 09/27/2003 27/09/2003
Format(Date,"Medium Date") 27-Sep-03 27-Sep-03
Format(Date,"Long Date") Saturday, September 27, 2003    27 September 2003   
Format(Date,"General Date")    09/27/2003 27/09/2003

NOTE: The user can modify these settings so they might appear different from those shown here. The ISO standard date format is yyyy-mm-dd (ISO 8601) e.g.: 2003-09-27.

Download the File

You can download a fully working example of the Pop-up Calendar as a Word template (.dot) file. There is one for each version of Microsoft Word although they should convert easily between versions. If the template fails to run correctly, it will be because your system does not contain a copy of the ActiveX Calendar Control. See the note above for how to obtain a copy. To download one of the files right-click the link and choose Save Target As...

 WordPopupCalendar_v97.zip    [11KB]
 WordPopupCalendar_v2000.zip [11KB] 
 WordPopupCalendar_v2002.zip [14KB] 

Want to Build a Better Calendar?

Do you want to build a more fully-functioned calendar and learn some more useful VBA programming techniques? The second part of this tutorial shows you how to take the Pop-up Calendar project a step further by offering a the user a choice of formats and allowing a default date format to be set. Find out more in A Pop-up Calendar for Word Part 2: Customizing the Calendar.

Read 55 times Last modified on Thursday, 28 February 2019 21:14
Dylan Austin


Whenever I have a problem, I sing. Then I realize my voice is worse than my problem.

We use cookies to improve our website. By continuing to use this website, you are giving consent to cookies being used. More details…