MS Word - Adding FormFields dynamically through VBA - Help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grant Gardiner

    #1

    MS Word - Adding FormFields dynamically through VBA - Help!

    This is an interesting one...

    You have a Word document with a parent table. Inside this is another
    table. This subtable has 4 columns, where I need to dynamically
    insert FormFields into each cell when I add a new row.

    Problem. Using the function below the oFormField object only ever
    points to the first of the newly inserted FormFields!?! The First
    cell in the newly appended row ends up being named Text2_4 instead of
    Text2_1.

    Public Sub AddRow()
    'unprotect file
    ActiveDocument. Unprotect Password:=""

    'get current number of Rows:
    Dim iCount As Integer
    Dim iCell As Integer

    'Add row
    ActiveDocument. Tables(1).Table s(1).Rows.Add
    iCount = ActiveDocument. Tables(1).Table s(1).Rows.Count
    Dim oFormfield As FormField
    Dim sName As String
    Dim sText As String
    Dim oRange As Range

    For iCell = 1 To 4
    sName = "Text" & iCount & "_" & iCell
    sText = ""
    Set oRange =
    ActiveDocument. Tables(1).Table s(1).Rows(iCoun t).Cells(iCell) .Range
    Set oFormfield = ActiveDocument. FormFields.Add( Range:=oRange,
    Type:=wdFieldFo rmTextInput)
    With oFormfield
    .TextInput.Defa ult = sText
    .Name = sName
    'Each cell has different formatting - this is simplified.
    End With

    Set oFormfield = Nothing
    Set oRange = Nothing
    Next

    'Set Cursor
    Application.OnT ime When:=Now + TimeValue("00:0 0:01"),
    Name:="SetFormF ieldFocus"

    'Reprotect file
    ActiveDocument. Protect wdAllowOnlyForm Fields, NoReset:=True
    End Sub

    Any help would be really appreciated.

    Thanks

    Grant
Working...