Link to home
Create AccountLog in
Avatar of Marcia Morris
Marcia MorrisFlag for United States of America

asked on

How do I make a Microsoft Word form automatically add a form number?

I've created a more user friendly form for our tooling department to complete online (tabbed through when locked). I have two questions.

1. How do I add a drop down date field so they just pick the date versus typing it? I would like it to open up to the current date when the form is opened.

2. Is there a way to add a field that will automatically add a serial/form number (no particular sequence) to the form? The reason I want that is because the printed copies go in a binder and some of the key data is in a spreadsheet for the VP (he runs reports from the data). He wants to be able to add a serial number to the form so that he can easily locate the hard copy in his binder if he needs to.
F730.5-Tool-Repair-Improvement-Form-DRAF
F730.5-Tool-Repair-Improvement-Form-UNLO
F730.5-Tool-Repair-Improvement-Form-CARL
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

For the form number, you will need to keep a record of what numbers have been used, so sequential numbering is easiest. If there is only one user, the latest number can be saved in the registry. It would need some code like this in the ThisDocument module of the template.
Option Explicit

Private Sub Document_New()
    Dim lSerial As Long
    
    'Get latest serial number
    lSerial = CLng(GetSetting("Tool Repair", "Improvement", "Serial", 0))
    lSerial = lSerial + 1
    'Format and put into the new document
    ActiveDocument.FormFields("Serial").Result = Format(lSerial, "000000")
    'save the new latest serial number
    SaveSetting "Tool Repair", "Improvement", "Serial", CStr(lSerial)
End Sub

Open in new window

If there is more than one user, the data could be saved in a file on the server.
You should be able to add an ActiveX control to  the form to set the date.
Use the hammer and spanner symbol in the ActiveX controls  section to get a list of more controls. The 'Microsoft Date and Time Pickers' should be there.

You can add a line to the code to set it to today's date:

Option Explicit

Private Sub Document_New()
    Dim lSerial As Long
    
    'Get latest serial number
    lSerial = CLng(GetSetting("Tool Repair", "Improvement", "Serial", 0))
    lSerial = lSerial + 1
    'Format and put into the new document
    ActiveDocument.FormFields("Serial").Result = Format(lSerial, "000000")
    'save the new latest serial number
    SaveSetting "Tool Repair", "Improvement", "Serial", CStr(lSerial)
    ActiveDocument.DTPicker1.Value = Now
End Sub

Open in new window

Avatar of Marcia Morris

ASKER

GrahamSkan..how do I view the code in Word? I've added the Microsoft Date and Time Pickers' so thank you. Just can't figure out how to view the code to add the sequential numbering.
Alt + F11 will open the VBA editor.

You should see the Project explorer pane on the left. Locate  the project for your template, and open it and then the Microsoft Word objects folder to find the ThisDocument module. Usually code is put into other modules than might be added, but we need to use that module because it is a class module so that Document event code will run when the event occurs. In the case of Document_New, that is when a new document is created from the template.

Paste the code into the code window on the right. Note that I have invented a form field called "Serial", so that must be added to the document, though you can change the code and give the field a different name. I notice in your unlocked sample document that none of the form fields have names, not even the default ones like "Text1", "Text2" and "Check1" etc.

To view or change a form field name, right-click and choose 'Properties' from the context menu.  Enter the chosen name into the 'Bookmark' box and hit OK
To throw a curve ball, as they say, does it have to be in Word?

There are numerous templates on the MS Website for Excel forms; they can be stored as a template and linked to a list in another workbook for storing the serial number and related data.

Thanks
Rob
Rob Henson... it doesn't have to be in Word. I haven't had any experience with creating a form in Excel. I will do a google search and see what templates are available. Linking to a list would be ideal.
Rob...I added the code and the field and named it 'serial' but I'm doing something wrong as the field isn't adding a serial number upon opening. I've attached the file.
F730.5-Tool-Repair-Improvement-Form-UNLO
Code written specifically for Word won't work in Excel (if that's what you are doing).
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Hmm. I now see that the name was too long for here and has cut off after the .d.
GrahamSkan, forive me for the delayed response, got put on a project that required a significant amount of my time. The serialization worked great. I did run into an issue. I tried to add back in the 'Microsoft Date and Time Pickers' date and parts due date but I kept getting an error message that says 'date picker cannot be insert around the current selection'. Do you know of a reason why I wouldn't be able to add it back?

Also, when I tested the serialization it did exactly what I wanted it to do but once I implement it how do I get it to start the numbering over vs. from where I left off in this testing phase?
Sorry Marcia, I missed your last comment.

Have you resolved the issues?

I haven't see the error message before, so I think I'd need to know a few more details.

To restart the numbering, you would to have some code like this:
SaveSetting "Tool Repair", "Improvement", "Serial", 0

Open in new window