Results 1 to 5 of 5

Thread: Word: Setting Form Field Bookmarks

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218

    Word: Setting Form Field Bookmarks

    I have two macros. One does what I need it to. The other doesn't. There is a single line of code difference, and I can't figure out why that line is causing a problem.

    I need to build a Word form where I can set the bookmark field of each form field.

    Code:
    Sub Bad()
    
      Documents.Add.Activate
      ActiveDocument.Paragraphs.Add
      Selection.Tables.Add Selection.Range, 2, 2
    
      Selection.Tables.Item(1).Cell(1, 1).Range.InsertAfter "Text"
    
      Selection.Tables.Item(1).Cell(1, 2).Select
      Selection.FormFields.Add Selection.Range, wdFieldFormTextInput
      ActiveDocument.FormFields.Item(1).Name = "fldName"
      ActiveDocument.FormFields.Item("fldName").Result = "Result"
      ActiveDocument.Protect wdAllowOnlyFormFields, True
      ActiveDocument.SaveAs "C:\THG\Bad.doc"
      ActiveDocument.Close
    
    End Sub
    
    Sub Good()
    
      Documents.Add.Activate
      ActiveDocument.Paragraphs.Add
      Selection.Tables.Add Selection.Range, 2, 2
    
      Selection.Tables.Item(1).Cell(1, 2).Select
      Selection.FormFields.Add Selection.Range, wdFieldFormTextInput
      ActiveDocument.FormFields.Item(1).Name = "fldName"
      ActiveDocument.FormFields.Item("fldName").Result = "Result"
      ActiveDocument.Protect wdAllowOnlyFormFields, True
      ActiveDocument.SaveAs "C:\THG\Good.doc"
      ActiveDocument.Close
    
    End Sub
    I hope this makes sense.
    Travis, Kung Foo Journeyman

    Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
    Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    OSS: Mozilla, MySQL (Manual)

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    You've recorded a macro & this doesn't work?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218
    I didn't record these macros, I wrote them. There are two subs. Good() works. Bad() doesn't. The difference is, Bad() is trying to write text into another cell of the table. For some reason, the form field bookmark value doesn't save when I run Bad(), but it does save when I run Good().

    But, a table with empty cells does me no good, and form fields with no bookmark value creates horrors I'm trying to avoid.

    Now mind you, there are a dozen ways to do any single thing in VBA. Some methods are deprecated, some are just redundant. Some have side effects, some don't. If you see any section of my code that you would do differently, let me know. I don't mind changing it if it will fix the problem. Some of the code is how I would do things, some of it was how the guy before me was doing it.
    Travis, Kung Foo Journeyman

    Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
    Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    OSS: Mozilla, MySQL (Manual)

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Ah, makes sense - I'm not a word guru myself & not too keen on it, but I normally record a macro when I'm dealing with ord & try to change the code it's created for me - I don't know if this will help you at all here....

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218
    This works. I have no idea why. I'm really starting to think there is an error in Word or VBA.

    Code:
    Sub Good()
    
      Documents.Add.Activate
      ActiveDocument.Paragraphs.Add
      Selection.Tables.Add Selection.Range, 2, 2
    
      Selection.Tables.Item(1).Cell(1, 2).Select
      Selection.FormFields.Add Selection.Range, wdFieldFormTextInput
      ActiveDocument.FormFields.Item(1).Name = "fldName"
      ActiveDocument.FormFields.Item("fldName").Result = "Result"
      
      Selection.Tables.Item(1).Cell(1, 1).Select
      Selection.TypeText "Text"
    
      ActiveDocument.Protect wdAllowOnlyFormFields, True
      ActiveDocument.SaveAs "C:\THG\Good.doc"
      ActiveDocument.Close
    
    End Sub
    Using .TypeText failed if I tried to do that before creating the FormField. It is simply doing that after the FormField. Here is something weird, though.

    Code:
    Sub Bad()
    
      Documents.Add.Activate
      ActiveDocument.Paragraphs.Add
      Selection.Tables.Add Selection.Range, 2, 2
    
      Selection.Tables.Item(1).Cell(1, 1).Range.InsertAfter "Text"
    
      Selection.Tables.Item(1).Cell(1, 2).Select
      Selection.FormFields.Add Selection.Range, wdFieldFormTextInput
      ActiveDocument.FormFields.Item(1).Name = "fldName"
      'Pause Here
      ActiveDocument.FormFields.Item("fldName").Result = "Result"
      ActiveDocument.Protect wdAllowOnlyFormFields, True
      ActiveDocument.SaveAs "C:\THG\Bad.doc"
      ActiveDocument.Close
    
    End Sub
    If you step through this code (F8), stop on the line after the pause comment and check the properties on the form field, then the .Result line will fail. If you don't check on the properties, but keep stepping, the .Result line will succeed.

    Though the .Name is still blank after you are done.
    Travis, Kung Foo Journeyman

    Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
    Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    OSS: Mozilla, MySQL (Manual)

Posting Permissions

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



Click Here to Expand Forum to Full Width