Using ActiveX Calendar to Insert a Date in a Field in Word

D

dgspencer

I'm likely out of my league here, but I'm working on a form in MS Word on and
I need to know how to implement an idea I have (considering I just started
learning VB two weeks ago)

Anyway, I'm creating a form in Word that will require the user to input both
a START DATE and an END DATE in five different columns. Of course, there is
always the (likely) possibilty that the user will enter an incorrect date.
It would be SO COOL if when the user clicked in the field or cell for the
START date, that nice ActiveX calendar would pop up and the user could simply
click on a date and it be put into the field for him; same for the END date.
OK...how do-able is this for a person who is still in the 4 chapter of a book
on how to learn VB?
 
J

Jezebel

Select 'More controls' on the Control Toolbox; select 'Microsoft Date and
Time Picker Control' from the list.
 
D

dgspencer

Tried that, but it returns the below error:
========================
Error! Not a valid embedded object.
========================
 
J

Jay Freedman

I'm likely out of my league here, but I'm working on a form in MS Word on and
I need to know how to implement an idea I have (considering I just started
learning VB two weeks ago)

Anyway, I'm creating a form in Word that will require the user to input both
a START DATE and an END DATE in five different columns. Of course, there is
always the (likely) possibilty that the user will enter an incorrect date.
It would be SO COOL if when the user clicked in the field or cell for the
START date, that nice ActiveX calendar would pop up and the user could simply
click on a date and it be put into the field for him; same for the END date.
OK...how do-able is this for a person who is still in the 4 chapter of a book
on how to learn VB?

Yep, you probably are aiming a bit far into the deep end, but here's
the stuff anyway.

First, you need the ActiveX calendar control. It's on your computer if
you installed a version of Office Professional with Access, or
standalone Access. Otherwise, visit
http://www.fontstuff.com/mailbag/qvba01.htm and follow the
instructions.

Next you need to create a "userform" or custom dialog box to hold the
calendar. General instructions for creating a userform are at
http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm, but they
don't cover calendars. So follow these instructions:

1. Open the VBA editor. In the Project pane (View > Project Explorer),
select the project for the template of your form. Click Insert >
UserForm. This creates a blank dialog box and, next to it, a Controls
Toolbox containing a bunch of buttons for standard controls.

2. Right-click a blank area of the Controls Toolbox and choose
Additional Controls. Check the box next to "Calendar Control" and
click OK. A new button will appear in the toolbox to represent the
calendar control.

3. In the Properties pane (View > Properties Window), change the name
of the userform from UserForm1 to frmCalendar, and change the caption
to Choose a Date.

4. Click the calendar button in the toolbox, then click on the
userform to insert it. Adjust its location and the size of the
userform (dragging the edges) so you can see all of it, and leave room
at the bottom for a couple of command buttons. While the calendar is
selected, go to the Properties window and rename it from Calendar1 to
ctlCalendar.

5. Click the Command button in the toolbox, then click in the userform
to insert one. In the Properties dialog, rename it to cmdOK and change
its caption to OK.

6. Click the Command button in the toolbox again, then click in the
userform to insert another one. In the Properties dialog, rename it to
cmdCancel and change its caption to Cancel.

7. Click View > Code. Copy the following code and paste it into the
window:

' ------------------------------------
Option Explicit
Public ChosenDate As String
Public Canceled As Boolean

Private Sub cmdCancel_Click()
Canceled = True
Me.Hide
End Sub

Private Sub cmdOK_Click()
Canceled = False
ChosenDate = ctlCalendar.Value
Me.Hide
End Sub

Private Sub UserForm_Activate()
If ChosenDate = "" Then
ctlCalendar.Value = Now
Else
ctlCalendar.Value = ChosenDate
End If
End Sub
' ------------------------------------

8. Click Insert > Module. This creates a new blank code window. Copy
the following code and paste it into the window:

' ------------------------------------
Option Explicit

Sub ShowCalendar()
Dim dlg As frmCalendar
Set dlg = New frmCalendar
With dlg
.ChosenDate = Trim(ActiveDocument.FormFields( _
Selection.Bookmarks(1).Name).Result)
.Show
If Not .Canceled Then
ActiveDocument.FormFields(Selection.Bookmarks(1).Name) _
.Result = .ChosenDate
End If
End With
Set dlg = Nothing
End Sub
' ------------------------------------

9. Save and close the VBA window.

10. In the template body, double-click the StartDate text form field
to open its properties dialog. Under the "Run macro on" label, click
the Entry dropdown and select ShowCalendar. Click OK. Repeat for the
EndDate text form field.

11. Protect the template for forms, save, and close. Create a new
document based on the template, and tab or click into each of the
fields. If the field is empty, the userform will appear with the
current date selected on the calendar. If the field isn't empty, the
calendar will appear with the given date selected (assuming it is a
valid date).

Here's some reading that may help you to understand the steps and the
code I supplied:

http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm
http://www.dragondrop.com/wordcoding/word011a.asp
http://www.word.mvps.org/FAQs/TblsFldsFms/GetCurFmFldName.htm

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
D

dgspencer

Almost got there I think. When I click in the text box, the following error
comes up in the debugger:

=======================
Compile Error:

Method or data member not found
=======================

The debugger highlights the variable and assignment operator

.chosendate =

In Module1, Option Explicit

Wow...I really appreciate the assist on this....
 
D

dgspencer

OK...maybe I should learn how to type or maybe just go to bed 'cause I'm worn
out. But the darn thing works like a charm!!! You are ABSOLUTELY THE BEST!
Would you prefer for me to keep this code to myself or can I share it with
someone else?...I especially want to show it to a guy who swears he is the
best VB programmer on the planet.
 
J

Jay Freedman

OK...maybe I should learn how to type or maybe just go to bed 'cause I'm worn
out. But the darn thing works like a charm!!! You are ABSOLUTELY THE BEST!
Would you prefer for me to keep this code to myself or can I share it with
someone else?...I especially want to show it to a guy who swears he is the
best VB programmer on the planet.

I'm glad you got it working. Just a hint: when I said copy and paste,
I really meant it. Retyping code, especially when you're tired, is a
waste of time and an invitation to errors.

Share the code all you like -- since it's posted in the public
newsgroup, and it'll soon be harvested into the Google Groups archive,
it's available to anyone.

And thanks for the compliment. :)

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top