ktselios
04-23-2012, 11:33 PM
Hi!
This is my first post here. I am a newbie to VBA.
What I try to do:
I want to delete empty rows in every table in a word protected form.
The table cantains some cells with explanation text and the other cells usually contain text formfiels.
I found a code from another post which I used it according to my needs.
Take a look:
Sub DeleteEmptyRows()
ActiveDocument.Unprotect Password:="**"
Dim oCell As Word.Cell
Dim oTable As Word.Table
Dim oForm As Word.FormField
Dim oRange As Range
Dim TextInForm As Boolean
Dim oRows As Integer
Dim i As Integer
Dim lastRow As Integer
Dim lastColumn As Integer
For Each oTable In ActiveDocument.Tables
oRows = oTable.Rows.Count
Set oRange = oTable.Rows(1).Range
For i = 1 To oRows
TextInForm = False
For Each oCell In oRange.Rows(1).Cells
For Each oForm In oCell.Range.FormFields
If oForm.Result <> "" Then
TextInForm = True
Exit For
End If
Next oForm
Next oCell
If TextInForm Then
Set oRange = oRange.Next(wdRow)
Else
oRange.Rows(1).Delete
End If
Next i
lastRow = oTable.Rows.Count
lastColumn = oTable.Columns.Count
oTable.Cell(lastRow, lastColumn).Range.FormFields(1).ExitMacro = "addRow"
Next oTable
ActiveDocument.Protect Password:="**", Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
The problem is that this code deletes all rows when the text formfield is empty, AND every row that contains TEXT (I don't want this!! :banghead: )
What I want is to just delete empty rows and keep the rows with text and not empty formfields.
Any suggestion?
Thanks
:hi:
This is my first post here. I am a newbie to VBA.
What I try to do:
I want to delete empty rows in every table in a word protected form.
The table cantains some cells with explanation text and the other cells usually contain text formfiels.
I found a code from another post which I used it according to my needs.
Take a look:
Sub DeleteEmptyRows()
ActiveDocument.Unprotect Password:="**"
Dim oCell As Word.Cell
Dim oTable As Word.Table
Dim oForm As Word.FormField
Dim oRange As Range
Dim TextInForm As Boolean
Dim oRows As Integer
Dim i As Integer
Dim lastRow As Integer
Dim lastColumn As Integer
For Each oTable In ActiveDocument.Tables
oRows = oTable.Rows.Count
Set oRange = oTable.Rows(1).Range
For i = 1 To oRows
TextInForm = False
For Each oCell In oRange.Rows(1).Cells
For Each oForm In oCell.Range.FormFields
If oForm.Result <> "" Then
TextInForm = True
Exit For
End If
Next oForm
Next oCell
If TextInForm Then
Set oRange = oRange.Next(wdRow)
Else
oRange.Rows(1).Delete
End If
Next i
lastRow = oTable.Rows.Count
lastColumn = oTable.Columns.Count
oTable.Cell(lastRow, lastColumn).Range.FormFields(1).ExitMacro = "addRow"
Next oTable
ActiveDocument.Protect Password:="**", Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
The problem is that this code deletes all rows when the text formfield is empty, AND every row that contains TEXT (I don't want this!! :banghead: )
What I want is to just delete empty rows and keep the rows with text and not empty formfields.
Any suggestion?
Thanks
:hi: