input date from calendar in textbox

E

erik

hello everybody,

On the internet I found a program to enter a date in my document by clicking
on a calendar userform. It works fantastically, but I would like to use it a
little different and don't exactly know how.

In my document I have textboxes from the forms toolbar (grey fields you have
to fill out). I want to open the calendar when I click on the textbox (i use
a macro when assessing the box) and when I select the date it has to be
inserted within the grey field, keeping the grey field and bookmark.

A few issues: When I have locked the page my program says it cannot write to
a locked position. When I have the page 'open' the date overwrites the grey
field and the date cannot be altered by using the macro as I meant it.

Who has a suggestion for me? The code I use is:

Private Sub Calendar1_Click()
' Transfer date selected on calendar to the insertion
' insertion and close UserForm.
Selection.Text = Format(Calendar1.Value, "dd mmmm yyyy")
Unload Me
End Sub

Private Sub cmdClose_Click()
' Close the form without entering a date if the user
' presses the [ESCAPE] key
Unload Me
End Sub

Private Sub UserForm_Initialize()
' If selection is a date show the same date, otherwise show
' today's date.
If IsDate(Selection.Text) Then
Calendar1.Value = DateValue(Selection.Text)
Else
Calendar1.Value = Date
End If
End Sub

Hope you can help me!!!
thnx,
Erik
 
J

Jay Freedman

Hi Erik,

The problem is that your code's "Selection.Text =" statement is trying
to replace the field itself, where it should only be trying to set its
data. Try it this way instead:

Private Sub Calendar1_Click()
' Transfer date selected on calendar to the insertion
' insertion and close UserForm.
Dim FFname As String
FFname = Selection.Bookmarks(Selection.Bookmarks.Count).Name
ActiveDocument.FormFields(FFname).Result = _
Format(Calendar1.Value, "dd mmmm yyyy")
Unload Me
End Sub

If you only have the one field in the document where you need to
insert the date, you can simplify it by just hard-coding the field
name into the macro, something like this:

Private Sub Calendar1_Click()
' Transfer date selected on calendar to the insertion
' insertion and close UserForm.
ActiveDocument.FormFields("DateField").Result = _
Format(Calendar1.Value, "dd mmmm yyyy")
Unload Me
End Sub
 

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