Pop-Up Calendar

Home Up Search This Site What's New? Audio On CDR Favourites Downloadable files Photo Gallery 2002 Photo Gallery 2003 Photo Gallery 2004/5 Photo Gallery 2006/7 Photo Gallery 2008 Photo Gallery 2009/10 UK Photo Gallery Ireland Photo Gallery Cats Photo Gallery 

 

 

Google
 

 

Many people access the material from this web site daily. Most just leech what they want and run. That's OK, provided they are not selling on the material as their own; however if your productivity gains from the material you have used, a donation from the money you have saved, however small, would help to ensure the continued availability of this resource.

Click the appropriate button above to access PayPal.

Use a pop-up calendar to select and insert a date

 

There are several web sources to be found that demonstrate the insertion of a date using a pop-up calendar. All those I have read are rather heavy going for the Word user who may wish to simply add a a calendar, from which he/she can select a date and insert it into a document or form field. This page does not offer any new or innovative techniques, but hopefully I have been able to explain in simple terms with illustrations, how to prepare such a calendar for use.

In the following example the illustrations are from Word 2007, but the function will work equally well with earlier versions and the relevant dialog boxes virtually identical.

The calendar is a Word userform run from the macro below. If you are unfamiliar with using macro code examples, then first see my Idiots Guide to Installing Macros.

Sub InsCalendar()
     frmCalendar.Show
End Sub

That macro, reproduced above, can be run from a toolbar button, or on entry to a form field (a legacy form field in Word 2007)

Before you start

 

The function will require the installation of Microsoft's ActiveX Calendar Control, filename mscal.ocx. This was supplied in some versions of Office and so may be present, but if not, it is available from the downloads page of my web site.

 

Open the vba editor (ALT+F11) and from the main menu select Insert > Userform

 

Right click the control toolbox and select 'Additional controls'

 

Locate the Calendar Control (the one shown below is the Office 2003 - Office 11.0 -version which may also be used with Word 2007). If not present, mscal.ocx is not installed - see the note at the start of this section

 

This adds the calendar control to the controls toolbox palette.

 

Create the calendar dialog

 

Change the name of the userform from UserForm1 to a meaningful name. I have used frmCalendar  which you will see reflected in the macro that calls the calendar at the head of this page.

 

Then drag the calendar control from the toolbox to the Userform and drag the handles that surround the calendar tool to produce a calendar of the required size. Then drag the handles of the userform itself to remove the surrounding space to produce a calendar similar to that shown below.

 

The layout of the calendar is user configurable e.g. click the calendar to select it then right click and choose 'properties'. You can play around with the various settings in the customisation dialog to give the effect you desire.

 

Add the vba coding to make it work
  The first requirement is to make the userform respond to pressing the ESC key on the keyboard, to close the form without inserting the date. To do this, add a command button to the userform with its cancel property set to true. This button doesn't need to be on view, so it can be located behind the calendar. It will be located behind the calendar by default, though its outline can be seen when selected - as in the illustration below.

Double click the 'hidden' button and the associated code dialog will open

Add the code associated with that button thus:

Private Sub CommandButton1_Click()
Unload Me
End Sub

You could have first renamed the command button to give it a more meaningful name, but we only have one button on this form and the default will do the job.

All that remains is to add the code to insert the selected date into the document. In the following example, the code inserts the date at the cursor, This is the type of code you would use if the calling macro (at the top of this page)  was run from a toolbar button or keyboard shortcut with the current date shown selected.

Private Sub Calendar1_Click()
With Selection

    .Text = Format(Calendar1.Value, "dd mmmm yyyy")
    .MoveRight Unit:=wdCharacter, Count:=1

End With
Unload Me
End Sub

Private Sub UserForm_Initialize()
Calendar1.Value = Date
End Sub

 

The next example is the type of code you might employ in a protected form. Here the calling macro is run on entry to a form field with the default name Text2. This calendar has an initial display with 30 days added to the current date and inserts the selected date into the form field.

Private Sub Calendar1_Click()
Dim oFld As FormFields
Set oFld = ActiveDocument.FormFields
oFld("Text2").Result = Format(Calendar1.Value, "dd mmmm yyyy")
Unload Me
End Sub

Private Sub UserForm_Initialize()
Calendar1.Value = Date + 30
End Sub