PDA

View Full Version : Auto-fill document from form



dzigner
03-16-2012, 10:55 AM
Hi
I have a document I need to be able to fill the information automatically from a Word form.
I know the simple bookmark way to do this. But now I also have a questionnaire the answers to which would be needed to be inserted into the document automatically. I think this is possible via VB code. But can someone guide me here?
thanks:help

gmaxey
03-16-2012, 12:42 PM
Try: http://gregmaxey.mvps.org/word_tip_pages/create_employ_userform.html

dzigner
03-16-2012, 03:30 PM
ok, thanks I will check that out.

dzigner
03-23-2012, 11:33 AM
Hi
I tried the various methods shown in the gregmaxey website, but have decided to use the form controls on the document itself, bookmark them, add cross-references to them in the other parts of document, and protect to use as a form.
Using userform and docvariables didnot work for me somehow.
Now I am onto the other part of the document, which is where I am using a userform, where when the checkbox is checked the answer is displayed in the document. There are 14 questions with 14 answers. How do I best do this?
Using this code:

Private Sub CommandButton1_Click()
With ActiveDocument
If chkquest1.Value = True Then
ActiveDocument.Variables("varquest1").Value = "No temp text, temp text etc herein."
Else
ActiveDocument.Variables("varquest1").Value = " "
End If
End With
End Sub


I can do the first checkbox somewhat correctly, using docvariable in the document. The problem here is if I uncheck the show field codes instead of their values under Options, the answer is displayed.
Secondly, how do I do this for 13 other questions?

Can you help please?
Thanks

fumei
03-23-2012, 02:00 PM
Huh?

Using userform and docvariables didnot work for me somehow.
Yet your code is usding document variables and a userform. So what is not working? And why are you using document variables anyway?

Please explain what you want to happen fully.
uncheck the show field codes instead of their values under Options, the answer is displayed.
What answer? Where?

Are the answers part of a list the user does NOT see on the userform? It sorts of seems that way, as the checkbox seems to make the variable a certain value. Are the answers somehow part of a checkbox? Are the checkbox values always returning a certain string (or nothing)?

Please try to answer all questions. The more information you give, the easier it is to help.

I do not know why you say you are using form controls in the document...yet do not appear to be using forms. Formfields are for user input IN the document. Using a userform to put text into formfield is not bad, but it is sort of pointless. At least not unless you are doing validation on the userform. Are you? I am guessing you are not.

You can not attach anything until you have at least 5 posts. perhaps do a few posts to explain some more, then attach a sample document. That would help a lot.

Definitely explain why you are using DOCVARIABLES.

dzigner
03-23-2012, 02:46 PM
Hello
I will try to answer as coherently as I can. As you said I cannot yet attach a file. :(

Huh?
Yet your code is usding document variables and a userform. So what is not working? And why are you using document variables anyway? OK, I started to use the userform for all entries in the document - name, address, case numbers, and more. I had these same entries bookmarked at the first instance, and later I used cross-reference to them elsewhere in the document. But the code I had for using userform did not update the cross-reference fields, only the bookmarked one. I think the reason was that the bookmark got written over, rather than into the bookmark. So I used the form controls available in the development tab and completed this part of the document.



Please explain what you want to happen fully.What answer? Where?

Are the answers part of a list the user does NOT see on the userform? It sorts of seems that way, as the checkbox seems to make the variable a certain value. Are the answers somehow part of a checkbox? Are the checkbox values always returning a certain string (or nothing)?

Please try to answer all questions. The more information you give, the easier it is to help.

Ok, so this is the second part of the same document.
Here, the user will ask some questions(14), which will have checkboxes beside them. Each question has its own answer. When the checkbox is checked, that answer should appear in the document, otherwise not. Then the completed document will be printed and sent to the customer whose name appears elsewhere in the document(first part).
Now the question and the checkbox should not appear in the document, so I used the userform for this part. when each question is asked, and if the corresponding checkbox is checked, then the corresponding answer should appear in the document. So, the same checkbox will always show the same answer. That part I can do for the first question, but as soon as I unclick the Show field codes instead of values, the answer pops up in the document at the start.
I hope I am being clear here.
So my two questions:
how do I stop the answers from being visible to the customer?
How do I use the code for multiple questions and answers.



I do not know why you say you are using form controls in the document...yet do not appear to be using forms. Formfields are for user input IN the document. Using a userform to put text into formfield is not bad, but it is sort of pointless. At least not unless you are doing validation on the userform. Are you? I am guessing you are not.

You can not attach anything until you have at least 5 posts. perhaps do a few posts to explain some more, then attach a sample document. That would help a lot.

Definitely explain why you are using DOCVARIABLES.
I am not doing any validation. I use DocVariables, because what else should I use? I got one code using tables, but that had question and the checkbox right on the document, which I donot want.

Thanks for any help.

fumei
03-23-2012, 04:51 PM
"as soon as I unclick the Show field codes instead of values"

Ummm...so don't unclick it. Why are you having Show fieldf code anyway???

In any case, try the example I am attaching (it only has three checkboxes). the code is simple.
In the userform module:

Option Explicit

Private Sub cmdDone_Click()
Dim oFld As Field

If chkQ1.Value = True Then
Call UpdateBookmark("Q1", "Blah blah Question 1 text.")
Else
Call UpdateBookmark("Q1", " ")
End If
If chkQ2.Value = True Then
Call UpdateBookmark("Q2", "Yadda yadda Blah blah Question 2 text.")
Else
Call UpdateBookmark("Q2", " ")
End If
If chkQ3.Value = True Then
Call UpdateBookmark("Q3", "Whatever, whataever Blah blah Question 3 text.")
Else
Call UpdateBookmark("Q3", " ")
End If
For Each oFld In ActiveDocument.Fields
oFld.Update
Next
Unload Me
End Sub

In a standard module module (this code preserves the bookmark).Option Explicit

Sub UpdateBookmark(BmkNm As String, NewTxt As String)
Dim BmkRng As Range
With ActiveDocument
If .Bookmarks.Exists(BmkNm) Then
Set BmkRng = .Bookmarks(BmkNm).Range
BmkRng.Text = NewTxt
.Bookmarks.Add BmkNm, BmkRng
End If
End With
Set BmkRng = Nothing
End Sub
Notice that the cross-reference fields have the same value as the checkbox answer bookmarks. And they are updated.

Show field codes is UNCHECKED.

dzigner
03-23-2012, 05:16 PM
Hi
cross refrences didnt update on my pc,
windows 2008 server
msoffice 2010 prof.
thnaks

gmaxey
03-23-2012, 05:25 PM
Sounds like you need a line of code:

ActiveDocument.Fields.Update.

dzigner
03-23-2012, 05:26 PM
Hi,
**Thank you for the code. I could do it with the questions and answers, but where I have to use crossreference, they still do not update with the userform.**
Is there any shorter way to group the questions/answers, like an array or something? the answers are really big.
Thanks

** sorry, had not refreshed my browser, so saw your reply after i posted this.
where do i put that line?

dzigner
03-23-2012, 05:33 PM
oh, yes!!!
i figured out where to put the line.
thank you soooooooooo much, this was making me crazy.

gmaxey
03-23-2012, 07:42 PM
You are welcome.

dzigner
03-23-2012, 08:04 PM
Hi, thanks again

How would I update the date field? the bookmark is text, but in the userform, I have used the DTPicker control
thanks

gmaxey
03-23-2012, 08:42 PM
What date field, what bookmark? If you mean a { DATE } field that returns the system date then you don't update it. It updates itself tomorrow and the next day and so on.

fumei
03-23-2012, 11:04 PM
dzigner, as I asked...please state everything going on exactly, clearly. You can not expect decent answers when you toss out things out of the blue with no background. You never mentioned a date field before. How can we help if you do not tell us what is going on?

You never mentioned a DTPicker, or what you did with it. Please state - again - clearly everything you expect to happen.

If a date is picked by the user, where do you put it, and WHY would you want to "update" it. Would not any update change what the user picked? In which case why have them pick one?

BTW: did what I posted and attached even help a bit? You do not mention anything at all.

fumei
03-24-2012, 01:18 AM
Ah, sorry, I realize now you did in fact use my example. I am surprised the cross-references did not update.


Is there any shorter way to group the questions/answers, like an array or something? the answers are really big.
The answers, certainly. The questions? Who knows? You do not state if the questions are attached as Captions to the checkboxes (although one would assume this). And would not the questions be actually ON the userform? And the checkboxes are checked (or not) in response to those questions?

So the questions do not need to be in an array. But sure, the answers can be. or even as text in another file and pulled in as INCLUDETEXT.

Again, hard to say as we do not know what you are doing.

fumei
03-24-2012, 02:11 AM
Array could be done like this.Private Sub cmdDone_Click()
Dim Ans

Ans = Array("Blah blah Answer Question 1 text.", _
"Yadda yadda Blah blah Answer Question 2 text.", _
"Whatever, whatever Blah blah Answer Question 3 text.")


If chkQ1.Value = True Then
Call UpdateBookmark("Q1", Ans(0))
Else
Call UpdateBookmark("Q1", " ")
End If
If chkQ2.Value = True Then
Call UpdateBookmark("Q2", Ans(1))
Else
Call UpdateBookmark("Q2", " ")
End If
If chkQ3.Value = True Then
Call UpdateBookmark("Q3", Ans(2))
Else
Call UpdateBookmark("Q3", " ")
End If
ActiveDocument.Fields.Update
Unload Me
End Sub


You would have to change the update bookmark procedure to use a Variant.
Sub UpdateBookmark(BmkNm As String, NewTxt As Variant)

dzigner
04-01-2012, 02:00 PM
Hello

Thank you for all the help. The document works perfectly, though I did not use the array code.

I figured out how to use the DTPicker control, and insert its value into the document.

Thanks

dzigner
04-02-2012, 04:40 PM
hi
I have saved this document as a macro-enabled word template, and this works fine on my computer. But this is not working on my colleague's pc. he creates a new document based on this template, and the document just opens, without the userform appearing.
Any ideas why this should happen to him and work fine with me?
thanks

Frosty
04-02-2012, 04:55 PM
Look at Trust Center settings... best guess is that the document you give him is having its macros automatically disabled.

dzigner
04-02-2012, 05:24 PM
hi,
i walked him through it, the trust center, enabling the macros and so on. Then realised he might not have the DTPicker form control I am using. So, I just added a simple text field for the date, and edited the code for that. I have sent him the file, and hopefully, this will work as we want it to.
Thanks