Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help: Need assistance with Word document + forms. 2

Status
Not open for further replies.

NeteruNeko

Technical User
Dec 29, 2004
20
US
Since I seem to be the most technically adept person here, I have been "enlisted" to create a new Word document for all the employees to use in order to create daily reports. I have made the layout, set up mostly text form fields, with a smattering of drop boxes, check boxes, and number fields for date and times. My problem is that I can't get my head wrapped around the idea of Object-Oriented Programming, since I started with text-only pascal and basic.

My goal is to have a simple button added either to the toolbar or to the word document itself that will allow a "one-click" reset of most fields in the document, and also insert the current date into a field. I would appreciate if someone would take the time to step-by-step explain how to do it, so I can make needed edits in the future.

Thanks for your time.

Neteru Neko
 
Howdy;

This can be tricky.

You'll want to set up your document as a template, then have users create their forms based on the template.

Two things you asked for. The second one regarding the current date field should be fine. Just INSERT > FIELD, then select DATE and the format you wish. By using a template (.dot), you will be able to carry this through.

For the first, it's a little more complex.

You'll need to code a simple macro that loops through the elements in your form and sets all their values to "false" or blank.

It would go like this

[formobject].value = FALSE
- or -
check_box.value = FALSE

Use the above methods for all of your form fields you wish to reset.

Hope this starts you off correctly.



~wmichael

"small change can often be found under seat cushions
 
Please clarify "most fields". If you are being selective, then you need a selective processess. The following does NOT do a selective process, it resets all text formfields to default text (if there is default), and resets all checkboxes to unchecked, and all drop downs to item #1.
Code:
Sub ClearAllFormFields()
Dim myFormField As FormField
Dim aDoc As Document
Dim response
Dim msg As String

Set aDoc = ActiveDocument
msg = "Do you want to clear all formfield results?"
response = MsgBox(msg, vbYesNo)
If response = vbYes Then
  For Each myFormField In aDoc.FormFields()
    Select Case myFormField.Type
      Case 70  ' text
        ' checks to see if there is default text
        ' if yes, removes user input and
        ' reverts to default; if no, removes user input
        If myFormField.TextInput.Default <> "" Then
          myFormField.Result = myFormField.TextInput.Default
        Else
          myFormField.Result = ""
        End If
      Case 71  ' check
        myFormField.Result = False
      Case 83   ' dropdown
        myFormField.DropDown.Value = 1
    End Select
  Next
Else
End If
Set aDoc = Nothing
End Sub

Regarding a "step-by-step...."
1. variables declared
2. create and set the aDoc object variable as the activedocument, mostly because I am lazy and I hate typing "ActiveDocument" a lot.
3. write text for a message string variable ("msg")
4. display the message string variable, with the result (Yes or No) = a variable ("response")
5. if yes (response = vbYes), loop through all the formfields
5 (a) if formfield is a text type (type 70) check is there is default text, if there is, revert back to it, if not, make it blank.
5 b) if checkbox, make it unchecked
5 c) if dropdown, make it the first item in the list
6. if user clicked No...then the statement IF response = vbYes is False, and nothing happens.
7. destroy the document object (aDoc).

As for the date inserting, you can use a field, as wmichael suggests. There are other options, depending on exactly what and why you require the date for.

Gerry
 
Thank you for responding. Now I have some additional questions. :)

Can someone point me at a resource that will define all the "built-in" functions in the VBA? This will help me understand what is being said with terms such as "dim" and "case". I believe it is also rather basic, but still I need to know how to create the button in the first place, and where to put the code.

Second, it looks like I will have to call each field seperately in order to achieve what I need. I am borrowing from my limited programming knowledge to ask this, but is there a method to creating a function which can be called repeatedly? An example of this would be:

Function ReplaceField
Code to replace field X with default Y;
End Function;

Set Variables XY;
Call ReplaceField;
Set Variables XY;
Call ReplaceField;
wash and repeat

Even easier, are data stacks available here? IE, create a list of string values to be used as variables XY, and code it as:

Set stack;
While value stack(or variable) =! 0;
ReplaceField code;
Pop value XY;

Lastly, regarding the date assignation: I was just trying to figure out what command will return the current date, so I can extract Date, Day of Week, Month, Year, and put them each in their respective spots. The only command that I know of, the current date assignation, would return a different date even if the document was opened to review, at say a different day.

I am trying to work in steps here, where the person would open their accustomed document, click the "reset form" button, have it assign all the information for the day the report is filed when the button is pressed while leaving personal information such as name and position, and eventually have a button that they would press to induce a "save" where the file would be returned a "uniform" save code, ie ReportDate.doc, then open a SendTo - EmailRecipient dialog window.

I can do it a little simpler, using the officer's names as the defaults, but then I would be forced to make edits for each officer to use a separate document designed for each. The method I am trying to use would automate it so the same document could be copied and used for each person without any prior editing.

I know this is large in scope for a beginner to use, but I figure if I outline the needs, and have a general idea of how to do it, I could get assistance for the rest from either reference or from forum assistance.

Thanks for the help.

NeteruNeko
 
I release I could give all the forms I need to be erased a number title, and do a For statement, but was looking for a method more descriptive than "field1, field2, ect" to call each of the fields (for personal debugging). This was the reason to ask about string stacks.

Normally I would do the searching myself, but I am working on low-end pentium work computers. With 8 megs of RAM, considerable chunking occurs when I try to browse the web, slowing research to a crawl.

Thanks again for the time and assistance.

Neteru Neko
 
Bah. I should never try to type a message when I can't concentrate fully.

The last message should have started "I Realize", not "release". BTW, am I missing something, or is the option of "edit post" not available here? I think I would like to edit the topic to make it easier for people to know what I am asking for assistance with.

-NeteruNeko
 
I believe I understand how the code is entered, I have created the button on a custom toolbar, now I am just looking for the info on organizing the code, and about string stacks. Can Sub reference another sub?

Just to make sure, the part of code regarding:
Case 70 ' text
' checks to see if there is default text
' if yes, removes user input and
' reverts to default; if no, removes user input
If myFormField.TextInput.Default <> "" Then
myFormField.Result = myFormField.TextInput.Default

How do I substitute particular fields instead of the more general "all fields". Do I reference the "FieldSettings:bookmark" value, and what syntax would I use?

For the other questions regarding the date/day of week and the save function, I'll open a new thread.

Thank you again for your assistance.

-Neteru Neko
 
Oh....you are not asking for much are you.

#1 suggestion. #1 suggestion. #1 suggestion.

USE the F1 key. It is called Help. If you are working at this level, there is simply no way you are going to get very far without it. This forum hands out a lot of free help, and even a fair amount of freely given code. I know I do, but what you are asking is too much, at least for now. A specific problem sure, for example if you post an actual Select Case that does not seem to be working...sure we will all pinch in and help. However, we will not, oooops there I go again, speaking in the third person....ahem, I can not see giving specific tutorials on the use of Select Case.

Type in Select Case and put your cursor on it and press F1. Try and work it out from there. Do a bunch of examples - make up your own.

The same applies for the question - can a "Sub referenece another sub". Sorry, but if you need to ask that, - and i am absolutely not denying it is a legitimate question, and it requires a legitimate answer. It is, in fact, an important question and vital to know so you can continue on. I encourage you to do so. However, the answer is in Help. Find it.

The same applies to how do you:
substitute particular fields instead of the more general "all fields". Do I reference the "FieldSettings:bookmark" value, and what syntax would I use

Study the code closely. The answer is in fact right there. The code loops through all fields...think it through. If you want a particular field....what makes it particular?? What identifies THIS field from THAT field? Maybe a name?

Regarding form fields, there are my two FAQs on this forum on FormFields. There are other FAQs on this forum dealing with general issues, as well as particular issues.

Right now though, use Help, use it a lot, play, play a lot. Try and keep some level of fun in it, and for sure, we will help with situations when you get stuck.

Gerry
 
subs, functions can all be called from anywhere and from each other etc.

vba is pretty much a fully blown language. as you can see from the dev environment you can create your own classes so i would say the sky is the limit.

arrays, hash tables are all standard offerings.

user settings like names etc can be preened from AD or NT domain credetials.



 
fumei: Thanks for all the assistance. I was able to get most of the piddling around with it done over the weekend. I am just going to be working on the rest of the "bugs" later.

I didn't mean to be too much of a burden. Just that my computer at home is down, and the only computer I am supposed to have access to at work is a windows 95 dinosaur that makes searches REALLY dang slow. Also, the help file wasn't installed, so I had to find another computer with office 97 and yank the .hlp files in order to do the work.

Really appreciate the assistance. You have all been great help.
 
Oh you are not being a bother. I was just trying to encourage you to dive in yourself.

Crap...Help is not installed? Bummer. Unfortunately, 'tis true, Help is not installed as a default. Bottom line is that you really can not build up your skill levels without Help. There are some very good books out there, and by all means take a good look at the FAQ here at Tek-Tips; and the Microsoft MVP sites...but the only real way is to start throwing stuff at the wall and, yup, some of it will stick.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top