Looping through form fields located within a table

D

dave.cuthill

I have kind of slapped the following together to loop through the
formfields in a table to find which field has the value "Production"
but from there I need to know the value of the cell offset by one
column to the right but cannot figure how I can determine position of
this formfield within the table. Also rather than have the loop go
through the complete table is it possible to limit the search to only
the first column of the table but it did not seem to function
For Each oFormfield In
ActiveDocument.Tables(1).columns(1).Range.FormFields


Dim vName As Variant
Dim sResult As String
Dim oFormfield As FormField


For Each oFormfield In ActiveDocument.Tables(1).Range.FormFields
If oFormfield.Type = wdFieldFormDropDown Then
vName = oFormfield.Name
sResult = oFormfield.Result
if sResult="Production" then
'Figure out cell location within the table and value of cell
offset by one column to the right
End If
End if
Next
Set oFormfield = Nothing


End Sub
 
H

Helmut Weber

Hi Dave,

like that:

Sub Macro8()
Dim lRow As Long
Dim oCll As Cell
Dim oTbl As Table
Set oTbl = ActiveDocument.Tables(1)
For Each oCll In oTbl.Columns(1).Cells
lRow = lRow + 1
If oCll.Range.FormFields.Count > 0 Then
If oCll.Range.FormFields(1).Result = "xxx" Then
MsgBox lRow ' found
MsgBox oTbl.Cell(lRow, 2).Range.FormFields(1).Result
End If
End If
Next
End Sub

Some error handling might be still required.
Also, it is assumed, that there is a formfield
in the cell right next to the cell in column 1
in which a formfield with result "xxx" was found.

Methods like Information(wdEndOfRangeRowNumber) failed.
Don't know why.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

dave.cuthill

Helmut:
Thanks this seems to do the trick. Before reading your post I found
the following seems to also work but not sure if the approach is as
good as yours ... I guess what I have doesn't guarantee that the
selection is in the first column.

Dim iIndex As Integer
Dim vName As Variant
Dim sResult As String
Dim oFormfield As FormField
Dim i As Integer
Dim myrange As Range

For Each oFormfield In ActiveDocument.Tables(1).Range.FormFields
If oFormfield.Type = wdFieldFormDropDown Then
vName = oFormfield.Name
sResult = oFormfield.Result

If sResult = "Production" Then
oFormfield.Select
i = Selection.Cells(1).RowIndex
Set myrange = ActiveDocument.Tables(1).Cell(i, 2).Range
myrange.End = myrange.End - 1
MsgBox myrange

End If
End If
Next
Set oFormfield = Nothing


End Sub
 
F

fumei via OfficeKB.com

A possible alternative. Note: I bookmark all my tables so I can make calls
to them by name. In this case, the table in question has been bookmarked
with the name "Production". So I can set a table object for specifically
THAT table.

Sub GetNextCellIF()
Dim oTable As Table
Dim oCol As Column
Dim j As Long
Dim oCell As Cell
Set oTable = ActiveDocument.Bookmarks("Production") _
.Range.Tables(1)
Set oCol = oTable.Columns(1)
For Each oCell In oCol.Cells
If oCell.Range.FormFields.Count = 1 And _
oCell.Range.FormFields(1).Result = _
"Production" Then
j = oCell.RowIndex
Set oCell = oTable.Cell(j, 2)
MsgBox CellText(oCell)
Exit For
End If
Next
Set oCell = Nothing
Set oTable = Nothing
End Sub


Function CellText(oCell As Cell) As String
Dim r As Range
Set r = oCell.Range
r.MoveEnd Unit:=wdCharacter, Count:=-1
Celltext = r.Text
Set r = Nothing
End Function

The For Each looks through each cell in the column object, and if the cell
has ONE formfield AND its Result = "Production", it sets the cell object to
be the cell one column over, and then uses the CellText function to display
the text.

I have a question though. Are you Unprotecting the document to execute this?
 
F

fumei via OfficeKB.com

Just want to add that, generally speaking, whenever possible it is better to
not use Selection. By using the cell object within the For Each logic there
is no need to Select.
 
H

Helmut Weber

Hi Dave,
I guess what I have doesn't guarantee that the
selection is in the first column.

It doesn't, indeed.

It just returns what is in the cell 2 of the row
of the cell containing the field.

Also rowindex and columnindex might fail
in not uniform tables, but thats another story.


Hi www.officekb.com/Uwe/,

yes, avoid the selection, as I did.

But please don't start yet another discussion
about range vs. selection. Sometimes, rarely, yes,
there is no alternative to the selection,
and in tables, the selection my be 10 times
faster than ranges.

See:
"4 seconds instead of 56"
http://tinyurl.com/c5nq8


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
D

dave.cuthill

Thank you both for your responses - I have learned much. Yes I
realized that I must unprotect the document to run this. I am however
getting an error at the following line - "The requested member of the
collection does not exist" - Any ideas about what it is referencing?

If oCell.Range.FormFields.Count = 1 And _
oCell.Range.FormFields(1).Result = _
"Production" Then
 
H

Helmut Weber

Hi Dave,

don't know what you're doing, :)
If oCell.Range.FormFields.Count = 1 And _
oCell.Range.FormFields(1).Result = _
"Production" Then

not "and" !
but

If oCell.Range.FormFields.Count = 1 then
if ocell.range.formfields(1).result = "Production" then
' ...
endif
endif

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jay Freedman

Unlike some other languages, VBA tries to evaluate all the clauses in the IF
statement's condition before executing the branch. If
oCell.Range.FormFields.Count is 0, there is no member of the FormFields
collection named oCell.Range.FormFields(1), so the error occurs.

You'll have to split this into two IF statements, so that the second one
doesn't execute if the first one is false:

If oCell.Range.FormFields.Count = 1 Then
If oCell.Range.FormFields(1).Result = _
"Production" Then
...
End If
End If

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
D

dave.cuthill

Unlike some other languages, VBA tries to evaluate all the clauses in the IF
statement's condition before executing the branch. If
oCell.Range.FormFields.Count is 0, there is no member of the FormFields
collection named oCell.Range.FormFields(1), so the error occurs.

You'll have to split this into two IF statements, so that the second one
doesn't execute if the first one is false:

If oCell.Range.FormFields.Count = 1 Then
If oCell.Range.FormFields(1).Result = _
"Production" Then
...
End If
End If

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
Thanks I figured it out after manipulating it a bit further.
 
R

Russ

Helmut,
See below.
Sub Macro8()
Dim lRow As Long
Dim oCll As Cell
Dim oTbl As Table
Set oTbl = ActiveDocument.Tables(1)
For Each oCll In oTbl.Columns(1).Cells
lRow = lRow + 1
If oCll.Range.FormFields.Count > 0 Then
If oCll.Range.FormFields(1).Result = "xxx" Then
MsgBox lRow ' found
MsgBox oTbl.Cell(lRow, 2).Range.FormFields(1).Result
End If
End If
Next
End Sub

Some error handling might be still required.
Also, it is assumed, that there is a formfield
in the cell right next to the cell in column 1
in which a formfield with result "xxx" was found.

Methods like Information(wdEndOfRangeRowNumber) failed.
Don't know why.

I'm curious what you were using for a selection or range where
Information(wdEndOfRangeRowNumber) failed? It seems to work for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top