Changing date formats in forms

G

gouved

I need to find a way to reformat a date value from a user formfield formatted
as m/d/yyyy to a bookmark which should have the date reformatted to mmmm d,
yyyy. Why would I want to do this, you may rightly ask. I have a userform
that I use to generate a hardcopy of information for a colleague who then has
to reenter all the data. To make it simpler for her (she would get lost in a
very large document), I pass the data to the tail section of my document
containing a table of bookmarks to store the data and its own autofillable
document following the table. I then split off the tail and she gets the
fragment. It works, albeit she has to edit when the form is unprotected and
autofill the document when it is protected. The onlyrub is the issue of
reformatting the dates. Can anyone offer suggestions how I can read the
dates from my formfield and assign the reformat values to the bookmark?
 
G

Graham Mayor

There are a several of ways of doing this, not all of which require vba.

eg Assuming form field Text1 has the input and MyDate is the target bookmark
then

Sub FormatDate()
Dim oRng As Range
Dim sDate As String
Dim bProtected As Boolean
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
Selection.GoTo What:=wdGoToBookmark, name:="MyDate"
Set oRng = Selection.Range
sDate = ActiveDocument.FormFields("Text1").Result
sDate = format(sDate, "MMMM d, yyyy")
Selection.TypeText sDate
oRng.End = Selection.Range.End
ActiveDocument.Bookmarks.Add "MyDate", oRng
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If
End Sub

will write the date into that bookmark in the required format,
OR
It would be simpler to replace the bookmark with a ref field and check the
calculate on exit check box of the date form field thus {Ref Text1 \@ "MMMM
d, yyyy"}
OR
you could insert another form field (Text2) in place of the bookmark and
populate that field with a macro run on exit from Text1

Sub FormatDate()
Dim oRng As Range
Dim sDate As String
sDate = ActiveDocument.FormFields("Text1").Result
sDate = format(sDate, "MMMM d, yyyy")
ActiveDocument.FormFields("Text2").Result = sDate
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

gouved

Wow, Graham, the code is awsome. I ran your draft--no problems. I tried to
add to it, and spent hours trying to get it to work before finding that I
confused input and target.

Thank you!
 

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