Set Today as Default Value for Form Field

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Set Today as Default Value for Form Field

Post by arroway »

I'm adding some functionality to a form which is not working and I'm not sure why. The code works in a blank .dot but when I add it to my form, I get a "parts of the form are locked". HansV from the Answers website. Here's the original post and answer...
--------------------------------------------------------------
Is there a way of setting the current day's date as default for a form field? Not automatically updating (the date needs to remain the same), just the day's date as date formatted text. We have a comlex, locked, Word03, form-template with many form fields, a good handful of which are "today's date". I'd like this to autofill by default so the date is editable in case the form is actually being filled out on another day. Is there a way of doing that? I'm guessing I need to put something in the Default date textbox but I don't know what. I've tried TODAY() but that doesn't work. Date format needs to be M/d/yyyy Other than that I haven't a clue. Any help?
--------------------------------------------------------------
The code would be very simple:

Private Sub Document_New()
Me.FormFields("Date1").Result = Date
Me.FormFields("Date2").Result = Date
Me.FormFields("Date3").Result = Date
End Sub

where Date1, Date2 and Date3 are the names of the bookmarks associated with the form fields - modify as needed.

<<<file deleted>>>>
Last edited by arroway on 06 Jan 2012, 16:30, edited 3 times in total.
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
HansV
Administrator
Posts: 78788
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Set Today as Default Value for Form Field

Post by HansV »

Welcome to Eileen's Lounge!

The reason for the error is that you use different code than I suggested. You actually have:

Me.FormFields("bDater").Range = Date

which fails, instead of

Me.FormFields("bDater").Result = Date

which works. So if you change .Range to .Result for all the formfields that you want to set, it will work.
Best wishes,
Hans

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Re: Set Today as Default Value for Form Field

Post by arroway »

Woops! Yes that was one of my tests which I forgot to switch back. Error msg resolved. I did switch it back and I believe I have the correct code there now but it still does not behave correctly. So...I fixed the code in the ThisDocument module of the .dot, saved, closed. Opened a New .doc from the template, enabled macros, and nothing. When I open the VBE from the .doc I see that the code is present in the ThisDocument module of the TemplateProject but there's nothing in the ThisDocument module of the Project(Document1) module. Is that how it's supposed to be? My updated code is:
Private Sub Document_New()
Me.FormFields("bDater").Result = Date
Me.FormFields("ciCallDate").Result = Date
Me.FormFields("ciPtEvalDate").Result = Date
Me.FormFields("dReqDayFrom").Result = Date
Me.FormFields("dInitDaysAuthFrom").Result = Date
Me.FormFields("eDetermDate").Result = Date
End Sub
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Re: Set Today as Default Value for Form Field

Post by arroway »

Grrr. I don't know what it is. So, the sample file I sent you is a copy of the "real" file with a bunch of the text taken out. When I changed the code (.Range to .Result) in the Private Subroutine it actually works in the sample file but doesn't in the real one. Could it be something with the fact that the real one lives on the server?

Something else I notice is in the sample .dot, when I open it, the dates are there. When I open the real.dot, they aren't.
Last edited by arroway on 05 Jan 2012, 16:56, edited 1 time in total.
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
HansV
Administrator
Posts: 78788
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Set Today as Default Value for Form Field

Post by HansV »

Word doesn't copy code from the template to a document based to the template. The document maintains a link to the template and uses the code from the template.
When you open the document and activate the Visual Basic Editor, you should see the template loaded in the Project Explorer on the left hand side.

It shouldn't matter whether the template is on the local hard disk or on a server.

Which version of Word are you using?
Best wishes,
Hans

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Re: Set Today as Default Value for Form Field

Post by arroway »

Word03
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
HansV
Administrator
Posts: 78788
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Set Today as Default Value for Form Field

Post by HansV »

When you create a new document based on the "real" template, do you see the template listed in the Visual Basic Editor?
Best wishes,
Hans

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Re: Set Today as Default Value for Form Field

Post by arroway »

In lieu of better judgement (and because I see that I can delete the file later) I'm uploading the 'real' file. Hans, would you mind please taking a look at it?
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Re: Set Today as Default Value for Form Field

Post by arroway »

Actually it says it's too big. Max allowed is 256 KiB, so it's not allowing me.

Yes, when I open the Editor from a New .doc, the TemplateProject is listed there, complete with the ThisDocument module with the Private Sub in the Microsoft Word Objects folder and all the rest of the macros in Module1 in the Modules folder. Above that is the Project (Document1) with a Microsoft Word Objects folder and a References folder which has a reference to the .dot.
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Re: Set Today as Default Value for Form Field

Post by arroway »

OK, so this is weird too. When I open a New .doc from the template, enable macros, the dates don't go. But if I open it a second time (Document2), the dates are there.
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
HansV
Administrator
Posts: 78788
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Set Today as Default Value for Form Field

Post by HansV »

You can create a zip file from the template and attach the zip file.
Best wishes,
Hans

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Re: Set Today as Default Value for Form Field

Post by arroway »

OK, let's try this...

<<<<<< file deleted >>>>>>>
Last edited by arroway on 06 Jan 2012, 16:30, edited 2 times in total.
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
HansV
Administrator
Posts: 78788
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Set Today as Default Value for Form Field

Post by HansV »

Ah, got it! It's my fault, so I apologize. In the ThisDocument module, Me refers to the file running the code, i.e. the template, not the new document! If you change Me to ActiveDocument, it should work:

Code: Select all

Private Sub Document_New()
    ActiveDocument.FormFields("bDater").Result = Date
    ActiveDocument.FormFields("ciCallDate").Result = Date
    ActiveDocument.FormFields("ciPtEvalDate").Result = Date
    ActiveDocument.FormFields("dReqDayFrom").Result = Date
    ActiveDocument.FormFields("dInitDaysAuthFrom").Result = Date
    ActiveDocument.FormFields("eDetermDate").Result = Date
End Sub
Best wishes,
Hans

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Re: Set Today as Default Value for Form Field

Post by arroway »

FABULOUSOO!!!! OMG! That totally made my brain HURT! Thank you SO much for all your help! I really appreciate it! ~Dax
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Re: Set Today as Default Value for Form Field

Post by arroway »

Hello oh wise one! Well, after all that work some are now complaining that the form "helps" too much by inserting the dates into the form fields and want some or all of them shut off. I tried commenting out the code but it still populates the form. I tried leaving the Sub and End Subs and commenting out the midde (to comment out just some of the dates), and I've even tried deleting all the code but the date fields are still populated. I'm not sure what else to do. I'm getting there by opening the .dot, Alt+11 to open the VBA editor, the Dbl+Click on ThisDocument under the MS Windows Objects for the TemplateProject. After I remove all code, it's blank, but the dates are still inserted for new documents from the saved template. WEIRD! I'm obviously not 'commenting out' the right code somewhere??? Any help finding the culprit?
~Dax
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
HansV
Administrator
Posts: 78788
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Set Today as Default Value for Form Field

Post by HansV »

Create a new document from the template. If I understand you correctly, the current date will still be entered in all those form fields.
Press Alt+F11 to activate the Visual Basic Editor.
You should see the new document in the Project Explorer on the left hand side and the template too. What do you see in the ThisDocument module of the document? And of the template? And do the document or template contain other modules?
Best wishes,
Hans

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Re: Set Today as Default Value for Form Field

Post by arroway »

I've taken the code out of the ThisDocument module and saved it. So when I open a new document from the template, in the VBE I do see the Project (Document1) and the TemplateProject in the Project Explorer but the ThisDocument module of BOTH of these are blank. The Project also has a refernce to the TemplateProject. The TemplateProject does have another module which contains a bunch of other stuff (Spellcheck, the other "AWESOME" piece that collects info from the form and inserts it into an Excel document that you helped me out with in another post, and some print and save macros) but nothing which includes the date stuff.
Last edited by arroway on 20 Mar 2012, 23:26, edited 1 time in total.
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
HansV
Administrator
Posts: 78788
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Set Today as Default Value for Form Field

Post by HansV »

Do you see Normal in the Project Explorer? If so, is there any relevant code there?
Best wishes,
Hans

User avatar
arroway
3StarLounger
Posts: 368
Joined: 04 Jan 2012, 22:43

Re: Set Today as Default Value for Form Field

Post by arroway »

Yep. Normal is there but nothing in any of the modules there either.
It takes 2 to tango; unless you speak binary; then it takes 10.

User avatar
HansV
Administrator
Posts: 78788
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Set Today as Default Value for Form Field

Post by HansV »

Could it be that the form fields have already been filled in in the template? What happens if you clear (not delete!) the form fields in the template, then save it? Do you still get the dates in a new document based on the template?
Best wishes,
Hans