Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

Looping through form fields located within a table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dave.cuthill@computalog.com - 24 Sep 2007 23:23 GMT
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
Helmut Weber - 25 Sep 2007 01:06 GMT
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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

dave.cuthill@computalog.com - 25 Sep 2007 15:46 GMT
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"

Russ - 08 Oct 2007 08:00 GMT
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


Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.