
Signature
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
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
> Hi Dave,
>
[quoted text clipped - 31 lines]
> Win XP, Office 2003
> "red.sys" & Chr$(64) & "t-online.de"
fumei - 25 Sep 2007 18:01 GMT
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?
fumei - 25 Sep 2007 18:05 GMT
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.
dave.cuthill@computalog.com - 25 Sep 2007 19:43 GMT
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
> 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
[quoted text clipped - 40 lines]
> --
> Message posted viahttp://www.officekb.com
Helmut Weber - 25 Sep 2007 20:17 GMT
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

Signature
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
Jay Freedman - 25 Sep 2007 21:04 GMT
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

Signature
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.
> 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
[quoted text clipped - 50 lines]
>> --
>> Message posted viahttp://www.officekb.com
dave.cuthill@computalog.com - 25 Sep 2007 22:58 GMT
> Unlike some other languages, VBA tries to evaluate all the clauses in the IF
> statement's condition before executing the branch. If
[quoted text clipped - 61 lines]
> >> Celltext = r.Text
> >> Set r = Nothing
Thanks I figured it out after manipulating it a bit further.
> >> 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
[quoted text clipped - 8 lines]
>
> - Show quoted text -
Helmut Weber - 25 Sep 2007 19:04 GMT
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

Signature
Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
Helmut,
See below.
> Sub Macro8()
> Dim lRow As Long
[quoted text clipped - 19 lines]
> 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.

Signature
Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID