×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Add new row in form table only when a field is not blank

Add new row in form table only when a field is not blank

Add new row in form table only when a field is not blank

(OP)
Hi All;

I am trying to add a new row in a table only when the last field in the table is not blank. Currently I have the code for adding the line and blank fields. I can run the code when the field is exited without any issues.

Several things that I would like to do:

1) I want to only activate this on the last field on the last row of the table only if it is not blank
2) I don't know how to do this based on a random field bookmark name that is created from the following code
3) How do I de-activate the code in any lines above the new blank table row and field where this code has already been been run

The code currently activates on the exiting of form field. There are some issues.

1) If statement is not correctly activating due to me not knowing the proper formula to activate the if statement
2) In the second with statement, I don't know how to update the form field name from the current name "Text52" to the newly created form field
3) Other issues are also occuring

any assistance would be appreciated.

Mike

CODE

Sub test()

If Selection.FormFields > 0 Then

Selection.InsertRowsBelow 1

    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
        
    With Selection.FormFields(1)
    With Selection.FormFields
    
        '.Name = "Text52"
        .EntryMacro = ""
        .ExitMacro = "test"
        .Enabled = True
        .OwnHelp = False
        .HelpText = ""
        .OwnStatus = False
        .StatusText = ""
        With .TextInput
            .EditType Type:=wdRegularText, Default:="", Format:=""
            .Width = 0
        End With
    End With
    

Else: End If

End Sub 

RE: Add new row in form table only when a field is not blank

Hi,

Not a Word guru, but here’s a tack to take.

Sub test()
   If [Table Cell Row] = [Table Row Count] Then
      ‘Put code to insert row here
   End If
End Sub
 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Add new row in form table only when a field is not blank

(OP)
Hi Skip,

I updated the code per your recommendation and I am now getting a stupid error. Else without if statement or end if without if. Any ideas on how to correct it?

CODE

Sub test3()

'If number_of_Rows = ActiveDocument.Tables(1).Rows.Count Then  <-- equals 15 (correct)

If Selection.FormFields(1) = ActiveDocument.Tables(1).Rows.Count Then

Selection.InsertRowsBelow 1

    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
        
    'With Selection.FormFields(1)
        With Selection.FormFields(1)
        
            '.Name = "Text52"
            .EntryMacro = ""
            .ExitMacro = "test"
            .Enabled = True
            .OwnHelp = False
            .HelpText = ""
            .OwnStatus = False
            .StatusText = ""
            With .TextInput
                .EditType Type:=wdRegularText, Default:="", Format:=""
                .Width = 0
        End With
    'End With
    

Else

If Selection.FormFields(1) < ActiveDocument.Tables(1).Rows.Count Then

End If


End Sub 

RE: Add new row in form table only when a field is not blank

You do not need...

CODE

Else
If Selection.FormFields(1) < ActiveDocument.Tables(1).Rows.Count Then

End If 

You also seem to have a missing End With.

...and does this return a row number???

CODE

Debug.Print Selection.FormFields(1) 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Add new row in form table only when a field is not blank

(OP)
I am getting a compile error now. "Else without if" and it is again highlighting the Else above.

I created a mini macro that returns the proper row count.

CODE

number_of_Rows = ActiveDocument.Tables(1).Rows.Count 

RE: Add new row in form table only when a field is not blank

Did you take care of the missing End With?

Need to see your current code.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Add new row in form table only when a field is not blank

(OP)

CODE

Sub test4()

'If number_of_Rows = ActiveDocument.Tables(1).Rows.Count Then  <-- equals 15 (correct)

If Selection.FormFields(1) = ActiveDocument.Tables(1).Rows.Count Then

Selection.InsertRowsBelow 1

    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
        

        With Selection.FormFields(1)
        
            .EntryMacro = ""
            .ExitMacro = "test"
            .Enabled = True
            .OwnHelp = False
            .HelpText = ""
            .OwnStatus = False
            .StatusText = ""
            With .TextInput
                .EditType Type:=wdRegularText, Default:="", Format:=""
                .Width = 0
        End With

Else: End If

End Sub 

RE: Add new row in form table only when a field is not blank

This does not return a Row Number!

You are missing this

CODE

Sub test4()

'If number_of_Rows = ActiveDocument.Tables(1).Rows.Count Then  <-- equals 15 (correct)

If Selection.FormFields(1) = ActiveDocument.Tables(1).Rows.Count Then

Selection.InsertRowsBelow 1

    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
        

        With Selection.FormFields(1)
        
            .EntryMacro = ""
            .ExitMacro = "test"
            .Enabled = True
            .OwnHelp = False
            .HelpText = ""
            .OwnStatus = False
            .StatusText = ""
            With .TextInput
                .EditType Type:=wdRegularText, Default:="", Format:=""
                .Width = 0
            End With
        End With

Else: End If

End Sub 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Add new row in form table only when a field is not blank

(OP)
I am now getting another error:

5941 - The requested member of the collection does not exist. Error occurs on line 1 of the code

current code:

CODE

Sub test4()

'If number_of_Rows = ActiveDocument.Tables(1).Rows.Count Then  <-- equals 15 (correct)

If Selection.FormFields(1) = ActiveDocument.Tables(1).Rows.Count Then

Selection.InsertRowsBelow 1

    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
        

        With Selection.FormFields(1)
        
            .EntryMacro = ""
            .ExitMacro = "test"
            .Enabled = True
            .OwnHelp = False
            .HelpText = ""
            .OwnStatus = False
            .StatusText = ""
            With .TextInput
                .EditType Type:=wdRegularText, Default:="", Format:=""
                .Width = 0
        End With
    End With

Else: End If

End Sub 

RE: Add new row in form table only when a field is not blank

(OP)
Not much actually happened... it looks like it is just having an error...

expression: Selection.FormFields(1)
Value = <Out of context>
Type = empty
context: New:Macros, Test4

I am not sure what this might mean...

RE: Add new row in form table only when a field is not blank

To see how to add a new row to a table in a document with formfields, go to:
http://www.msofficeforums.com/word-vba/13955-macro...
All you need do is add a test for your last formfield's content.

Cheers
Paul Edstein
[MS MVP - Word]

RE: Add new row in form table only when a field is not blank

(OP)
Hi marcropod,

I don't have any issue with adding a row with fields. It is the conditions in which the row is to be added.

I need to only add the new line and fields when the last field in the last row is filled in. While the macro will launch on exiting the field, I need it to check that it is both not empty and that it is in the last row of the table.

If the last box in the row is filled in, but it isn't in the last row, I don't want a new row added.

RE: Add new row in form table only when a field is not blank

Plz upload your document.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Add new row in form table only when a field is not blank

I can’t seem to find the FormField ID of the Selection.

Maybe marcropod can help. As I conceive it, you have ActiveDocument.FormFields.Count and the Index of the FormField in question, which I cannot seem to access. When those values are equal, you’ve exited the bottom-right cell of your table.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Add new row in form table only when a field is not blank

remeng: You really should have looked at the code I directed you to.

Try replacing all of it with:

CODE

Sub AddRow()
Application.ScreenUpdating = False
'This macro adds another row to the table.
' If you need the formfields named, activate the commented-out code.
' As coded, the formfield names assume each column uses a different formfield
' name ending in a two-digit number corresponding to the formfield row. For
' example, formfield names on tabe row 2, which is formfield row 1, end with 01.
Dim i As Long, j As Long, FmFld As FormField, Prot As Variant
'Dim StrNms As String
Const Pwd As String = "" 'Insert password here
With Selection
  i = .Tables(1).Range.FormFields.Count
  j = ActiveDocument.Range(.Tables(1).Range.Start, .Range.End).FormFields.Count
End With
If i = j Then
  With ActiveDocument.FormFields(ActiveDocument.Tables(1).Range.FormFields.Count)
    If .Result = .TextInput.Default Then Exit Sub
  End With
  If MsgBox("Add new row?", vbQuestion + vbYesNo) = vbYes Then
    With ActiveDocument
      Prot = .ProtectionType
      If .ProtectionType <> wdNoProtection Then
        Prot = .ProtectionType
        .Unprotect Password:=Pwd
      End If
      With Selection.Tables(1).Rows
        i = .Count
        With .Last.Range
          'For Each FmFld In .FormFields
            'StrNms = StrNms & "|" & Left(FmFld.Name, Len(FmFld.Name) - 2) & Format(i, "00")
          'Next
          .Next.InsertBefore vbCr
          .Next.FormattedText = .FormattedText
        End With
        With .Last.Range.FormFields
          For i = 1 To .Count
            With .Item(i)
              If .Type = wdFieldFormCheckBox Then .CheckBox.Value = False
              If .Type = wdFieldFormTextInput Then .TextInput.Clear
              If .Type = wdFieldFormDropDown Then .DropDown.Value = .DropDown.Default
              '.Select
              'With Dialogs(wdDialogFormFieldOptions)
                '.Name = Split(StrNms, "|")(i)
                '.Execute
              'End With
            End With
          Next
          .Item(1).Select
        End With
      End With
      .Protect Type:=Prot, Password:=Pwd, Noreset:=True
    End With
  End If
End If
Application.ScreenUpdating = True
End Sub 
and making 'AddRow' the on-exit sub called by your initial last formfield.

Cheers
Paul Edstein
[MS MVP - Word]

RE: Add new row in form table only when a field is not blank

CODE

Dim iFldCnt As Integer
iFldCnt = ActiveDocument.FormFields.Count
If Trim(ActiveDocument.FormFields(iFldCnt).Result) <> “” Then
‘Your add row code here
End If 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Add new row in form table only when a field is not blank

(OP)
Hi skip,

I tried adding your code. The code just jumps to the End If statement at the end. Maybe I did something incorrect...

CODE

Sub create_row2()

' create_row Macro

Dim iFldCnt As Integer
iFldCnt = ActiveDocument.FormFields.Count
If Trim(ActiveDocument.FormFields(iFldCnt).Result) <> “” Then

' Add row code

    Selection.InsertRowsBelow 1
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveUp Unit:=wdLine, Count:=1
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.MoveRight Unit:=wdCell
    Selection.FormFields.Add Range:=Selection.Range, Type:= _
        wdFieldFormTextInput
    Selection.PreviousField.Select
    With Selection.FormFields(1)
       ' .Name = "Text55"
        .EntryMacro = ""
        .ExitMacro = "create_row2"
        .Enabled = True
        .OwnHelp = False
        .HelpText = ""
        .OwnStatus = False
        .StatusText = ""
        With .TextInput
            .EditType Type:=wdRegularText, Default:="", Format:=""
            .Width = 0
        End With
    End With

' end add row code

End If

End Sub 

RE: Add new row in form table only when a field is not blank

Well was there a Value in the last FormField?

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Add new row in form table only when a field is not blank

(OP)
Hi Skip,

Sometime the basics are missed. Thanks for the help. The extra line was added.

I think I should be able to proceed with this code.

I will also add a button to add the extra line just in case.

Thanks for the help.

RE: Add new row in form table only when a field is not blank

Even if you don’t use it, you ought to look at macropod’s code and try to understand what he’s doing. I’m sure that it will be enlightening. I’ve learned an awful lot just by looking at other people’s solutions and tucking little gems away in my toolbox.

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close