Group:  Microsoft Word ยป microsoft.public.word.vba.general
Thread: Changing date formats in forms

Geek News

Changing date formats in forms
gouved 12/30/2008 9:00:01 AM
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?
Re: Changing date formats in forms
"Graham Mayor" <gmayor[ at ]REMOVETHISmvps.org> 12/30/2008 9:35:04 AM
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 \[ at ] "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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


gouved wrote:
[Quoted Text]
> 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?


Re: Changing date formats in forms
gouved 12/30/2008 10:07:00 PM
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!

"Graham Mayor" wrote:

[Quoted Text]
> 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 \[ at ] "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
>
> My web site www.gmayor.com
> Word MVP web site http://word.mvps.org
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>
>
> gouved wrote:
> > 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?
>
>
>

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net