Results 1 to 12 of 12
  1. #1
    New Lounger
    Join Date
    Jun 2002
    Location
    Richland, Washington, USA
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reference FormFields Data (Word97)

    Very long code fragment moved to attachment by HansV

    I have searched the threads and can not find anything that specifically helps me in my situation. I have word document with questions and formfields for the user to fill in answers for the questions and a command button at the end of the document that the user clicks on to grade the test. The formfields are bookmarked as "Q#A#" with the # being a sequential number for each Question (Q#) and Answer (A#) regardless if they are a text formfield or a checkbox formfield.

    The problem I am running into is referencing the formfield to compare what the user typed in to the actual answer stored in the code. The line is:

    If FormFields(QuestionID).Result = Answers(QuestionCnt, AnswerCnt) _

    This is the first test document of several dozen that I am attempting to automate. Once I have this one done, I will modify the code for the other test documents. THe number of questions/answers varies with each test.

    My code is pasted below and any assistance would be greatly appreciated.

    Thanks,

    -Drew

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Reference FormFields Data (Word97)

    The result of a check box form field is 1 if ticked, 0 if clear. (If you want True / False, you can use FormFields(...).CheckBox.Value, but you would have to check whether a formfield is a text box or a check box).

    Since you are working with a specific document object (doc), you should qualify all range object with doc. There is no need to calculate the end of the range, you can simply use doc.Range.InsertAfter.

    I have attached a modified version of your code; as far as I have been able to test it, it works OK.

  3. #3
    New Lounger
    Join Date
    Jun 2002
    Location
    Richland, Washington, USA
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reference FormFields Data (Word97)

    Hans,

    First, thank you for looking at my code and improving it.

    I am still having problems with "run-time error 5941...the requested member of the collection does not exist" when I get to the "If doc.FormFields(QuestionID).Result = Answers(QuestionCnt, AnswerCnt) Then" code line.

    Since you told me you didn't have any problems with the code I am including a modified version of the file I am working on to see if I am doing something else wrong.

    Thank you again for helping me.

    -Drew

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Reference FormFields Data (Word97)

    I don't get an error on that line in the document you posted; the grade is calculated and displayed correctly, so I'm afraid I don't know why it produces an error when you try it.

    I can run the macro (without errors) from the Visual Basic Editor, but clicking the button doesn't work for me - the focus immediately moves to the first form text field. If I define an accelerator key, I can use that, however.

  5. #5
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Reference FormFields Data (Word97)

    Drew

    It works for me without an error. When it errors on your machine, what is the value of QuestionID and does this formfield exist in the document at that time?

    Is this form for real life or is it a demonstration/test? Your logic is far too harsh - there needs to be considerable fuzziness added to allow near enough answers through. One such thing would be some more leeway in your marking to allow capitalisation differences through as well as errant spaces. Question 9 is going to prove impossible for even the author without looking at the code for the answers.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  6. #6
    New Lounger
    Join Date
    Jun 2002
    Location
    Richland, Washington, USA
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reference FormFields Data (Word97)

    Hmm...

    I wonder why I get the error then...

    Thanks for looking at it and letting me know that you don't have any problems with it.

  7. #7
    New Lounger
    Join Date
    Jun 2002
    Location
    Richland, Washington, USA
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reference FormFields Data (Word97)

    Yes the form is real not a demonstration/test. The value of QuestionID at the error time is Q1A1 which matches the bookmark of the first formfield that needs to be checked.

    I'm not too worried about adding the "fuzziness" at this time because the test is open book and the fill in the blank questions are word for word out of the book.

    I'm not sure what to do next since both you and Hans didn't get any errors at all, but I'll figure something out.

    Thanks,

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Reference FormFields Data (Word97)

    Perhaps someone else with Word 97 can test the code; I am using Word 2002, and I think Andrew Lockton also uses a later version than 97.

  9. #9
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Reference FormFields Data (Word97)

    Hans is right, we are both using later versions of Word.

    The value of Q1A1 matches the bookmark name but your code is testing for FormField names.

    I have a feeling that the code is not running because perhaps in Word 97 the formfields names are not the same as the bookmark names. What field names do you get in the immediate window if you run this test macro in Word 97?
    <pre>Sub atest()
    Dim aFF As FormField
    For Each aFF In ActiveDocument.FormFields
    Debug.Print aFF.Name
    Next aFF
    End Sub</pre>


    What result do you get if you modify the If chunk of code in your own macro to
    <pre> If doc.Bookmarks(QuestionID).Range.Text = Answers(QuestionCnt, AnswerCnt) Then
    AnswersCorrect = AnswersCorrect + 1
    Else
    If doc.FormFields(QuestionID).Result = Answers(QuestionCnt, AnswerCnt) Then
    AnswersCorrect = AnswersCorrect + 1
    Else
    AnswersIncorrect = AnswersIncorrect + 1
    End If
    End If</pre>


    I would also suggest changing the initial doc declaration to avoid the filename being explicitly in the code.
    Set doc = ActiveDocument
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  10. #10
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reference FormFields Data (Word97)

    <hr>I have a feeling that the code is not running because perhaps in Word 97 the formfields names are not the same as the bookmark names. <hr>
    I think they are. For example, in Word97-2003 you cannot test the existence of a FF directly:
    <font face="Georgia">If ActiveDocument.FormFields("FFname").Exists Then</font face=georgia> or <font face="Georgia">If ActiveDocument.FormFields.Exists("FFname") Then </font face=georgia>
    produces an error. But
    <font face="Georgia">If ActiveDocument.Bookmarks.Exists("FFname") Then</font face=georgia>
    works correctly.

  11. #11
    New Lounger
    Join Date
    Jun 2002
    Location
    Richland, Washington, USA
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reference FormFields Data (Word97)

    Nothing showed in the Immediate Window.

    The Bookmark reference worked fine as long as I specifically called out the bookmark.

    I have since converted everything over to activex controls now. The screen jumps when scrolling, but I'll live with that. However, I am still getting an error.

    I have come to the conclusion that Word 97 does not allow the use of a variable to call out bookmarks or activex controls. You have to explicitly name the activex control of bookmark for the code to work. So I guess I am going to have an If...Then...Else statement for each bookmark/activex control in the document instead of using a loop with a single If...Then...Else statement with a variable.

    The code worked fine on later versions of Word on my home computer, but alas I am limited to Word97 at work.

    Thank you for everyones help.

  12. #12
    4 Star Lounger
    Join Date
    Dec 2003
    Location
    Zoetermeer, Zuid-Holland, Netherlands
    Posts
    559
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Reference FormFields Data (Word97)

    I looked again at the code in your first post. It contains this snippet:
    <pre> Dim QuestionID As String
    QuestionID = "Q" & QuestionCnt & "A" & AnswerCnt

    If FormFields(QuestionID).Result = Answers(QuestionCnt, AnswerCnt) _ </pre>


    This fails in Word97 because you declared QuestionID as a String. Later versions of Word accept that, but in Word97 it has to be declared as a Variant if you want to use it as a variable for a FormField name.

Posting Permissions

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