Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 21

Thread: Auto-fill document from form

  1. #1
    VBAX Regular
    Joined
    Mar 2012
    Posts
    11
    Location

    Auto-fill document from form

    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

    dzigner
    Tasneem Rangoonwala

    D.zigns Enterprise Solutions

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Mar 2012
    Posts
    11
    Location
    ok, thanks I will check that out.

    dzigner
    Tasneem Rangoonwala

    D.zigns Enterprise Solutions

  4. #4
    VBAX Regular
    Joined
    Mar 2012
    Posts
    11
    Location

    autofill forms

    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:
    [VBA]
    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
    [/VBA]

    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

    dzigner
    Tasneem Rangoonwala

    D.zigns Enterprise Solutions

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  6. #6
    VBAX Regular
    Joined
    Mar 2012
    Posts
    11
    Location

    formfields and DocVariables

    Hello
    I will try to answer as coherently as I can. As you said I cannot yet attach a file.
    Quote Originally Posted by fumei
    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.

    dzigner
    Tasneem Rangoonwala

    D.zigns Enterprise Solutions

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "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:
    [vba]
    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[/vba]

    In a standard module module (this code preserves the bookmark).[vba]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
    [/vba]Notice that the cross-reference fields have the same value as the checkbox answer bookmarks. And they are updated.

    Show field codes is UNCHECKED.
    Attached Files Attached Files

  8. #8
    VBAX Regular
    Joined
    Mar 2012
    Posts
    11
    Location
    Hi
    cross refrences didnt update on my pc,
    windows 2008 server
    msoffice 2010 prof.
    thnaks

    dzigner
    Tasneem Rangoonwala

    D.zigns Enterprise Solutions

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Sounds like you need a line of code:

    ActiveDocument.Fields.Update.
    Greg

    Visit my website: http://gregmaxey.com

  10. #10
    VBAX Regular
    Joined
    Mar 2012
    Posts
    11
    Location
    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
    Tasneem Rangoonwala

    D.zigns Enterprise Solutions

  11. #11
    VBAX Regular
    Joined
    Mar 2012
    Posts
    11
    Location
    oh, yes!!!
    i figured out where to put the line.
    thank you soooooooooo much, this was making me crazy.

    dzigner
    Tasneem Rangoonwala

    D.zigns Enterprise Solutions

  12. #12
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    You are welcome.
    Greg

    Visit my website: http://gregmaxey.com

  13. #13
    VBAX Regular
    Joined
    Mar 2012
    Posts
    11
    Location
    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

    dzigner
    Tasneem Rangoonwala

    D.zigns Enterprise Solutions

  14. #14
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

  15. #15
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  16. #16
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.
    Last edited by fumei; 03-24-2012 at 01:30 AM.

  17. #17
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Array could be done like this.[vba]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
    [/vba]

    You would have to change the update bookmark procedure to use a Variant.
    [vba]Sub UpdateBookmark(BmkNm As String, NewTxt As Variant)[/vba]
    Attached Files Attached Files

  18. #18
    VBAX Regular
    Joined
    Mar 2012
    Posts
    11
    Location
    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
    Tasneem Rangoonwala

    D.zigns Enterprise Solutions

  19. #19
    VBAX Regular
    Joined
    Mar 2012
    Posts
    11
    Location
    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

    dzigner
    Tasneem Rangoonwala

    D.zigns Enterprise Solutions

  20. #20
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Look at Trust Center settings... best guess is that the document you give him is having its macros automatically disabled.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •