Results 1 to 6 of 6
  1. #1
    Super Moderator WebGenii's Avatar
    Join Date
    Jan 2001
    Location
    Redcliff, Alberta, Canada
    Posts
    4,066
    Thanks
    2
    Thanked 5 Times in 5 Posts
    I'm using the following code to create a form field in Word 2007
    Code:
    Sub mytest()
    	With ActiveDocument.FormFields.Add(Range:=Selection.Range, _
     	Type:=wdFieldFormTextInput)
     	.Name = "fred"
     	.TextInput.EditType Type:=wdNumberText, Format:="0.00%"
    	End With
    	
    End Sub
    However, it isn't working consistently between two machines and I'm having trouble determining why.
    On one machine the name and number format are not set at all (and an object error is generated), although the field is created. On the other machine everything works fine.
    the references are the same between both machines.

    Any ideas?
    [b]Catharine Richardson (WebGenii)
    WebGenii Home Page
    Moderator: Spreadsheets, Other MS Apps, Presentation Apps, Visual Basic for Apps, Windows Mobile

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    It would be an extra step, but you could create an object reference when you insert the field and see whether that makes any difference.

    Set myNewField = Activedocument.FormFields.Add(Range:=Selection.Ran ge, Type:=wdFieldFormTextInput)
    myNewField.property = value


    Also, I suppose some other part of your code is checking that the document is unprotected. (!)

  3. #3
    Super Moderator WebGenii's Avatar
    Join Date
    Jan 2001
    Location
    Redcliff, Alberta, Canada
    Posts
    4,066
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Code:
     Dim FFMyField As FormField
     
     Set FFMyField = ActiveDocument.FormFields.Add(Range:=Selection.Range, _
    	Type:=wdFieldFormTextInput)
    	FFMyField.Name = "fred"
    	FFMyField.TextInput.EditType Type:=wdNumberText, Format:="0.00%"
    I have tried out the code above on both machines, again it doesn't work on the troublesome system. even after I've commented out the document protection lines.

    Again I am receiving a run-time error 91, about the "Object variable or With Block variable not set"

    Why would the document protection be a problem?
    [b]Catharine Richardson (WebGenii)
    WebGenii Home Page
    Moderator: Spreadsheets, Other MS Apps, Presentation Apps, Visual Basic for Apps, Windows Mobile

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by Catharine Richardson - WebGenii View Post
    I have tried out the code above on both machines, again it doesn't work on the troublesome system. even after I've commented out the document protection lines.

    Again I am receiving a run-time error 91, about the "Object variable or With Block variable not set"
    I can't think of any good reason for that. If you debug and then continue, does it work? If so, perhaps there is a delay or timing problem in inserting the field?

    Quote Originally Posted by Catharine Richardson - WebGenii View Post
    Why would the document protection be a problem?
    You can't insert a form field into a protected document. But you would get a very different error.

  5. #5
    Super Moderator WebGenii's Avatar
    Join Date
    Jan 2001
    Location
    Redcliff, Alberta, Canada
    Posts
    4,066
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by jscher2000 View Post
    If you debug and then continue, does it work? If so, perhaps there is a delay or timing problem in inserting the field?
    No, not a timing issue. The problem machine is running 64 bit XP.
    [b]Catharine Richardson (WebGenii)
    WebGenii Home Page
    Moderator: Spreadsheets, Other MS Apps, Presentation Apps, Visual Basic for Apps, Windows Mobile

  6. #6
    Super Moderator WebGenii's Avatar
    Join Date
    Jan 2001
    Location
    Redcliff, Alberta, Canada
    Posts
    4,066
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Found the reason for the problem:
    I am inserting the fields into a table, using
    Code:
     	tblAssignedMilestones.Cell(intRows, 5).Select
    to move to the appropriate row and cell.
    if I use
    Code:
     Selection.MoveRight Unit:=wdCell
    then all is well

    so it would seem that
    Code:
      Set FFMyField = Activedocument.FormFields.Add(Range:=Selection.Range, _
     Type:=wdFieldFormTextInput)
     FFMyField.Name = "fred"
     FFMyField.TextInput.EditType Type:=wdNumberText, Format:="0.00%"
    selecting the cell as a whole, prevents the object from being set.
    [b]Catharine Richardson (WebGenii)
    WebGenii Home Page
    Moderator: Spreadsheets, Other MS Apps, Presentation Apps, Visual Basic for Apps, Windows Mobile

Posting Permissions

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