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 / February 2005

Tip: Looking for answers? Try searching our database.

Index Value of FormField

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg Maxey - 26 Feb 2005 06:38 GMT
Last week I thought that I had figured out a way to get the index value of a
formfield using VBA.  Today I noticed that it fails if I have two or more
formfields in a table row.  For some reason if I have a row of formfields in
a table, all of the formfields in the same row return the same value.  Can
anyone explain why this isn't working:

Sub GetIndex()

Dim oDoc As Document
Set oDoc = ActiveDocument
oDoc.Variables("Index").Value = _
    oDoc.Range(0, Selection.Bookmarks(1).Range.End).Bookmarks.Count
MsgBox oDoc.Variables("Index").Value
End Sub

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

Doug Robbins - 26 Feb 2005 12:22 GMT
Hi Greg,

I haven't found a work aroung yet, but the problem is caused by the way in
which Word handles Selections/Ranges that include information in a table and
information before a table.

Place a couple of paragraph returns in a document, then a table and then try
and select the paragraph returns and the first cell of the table.  You
cannot do it, the whole row is selected.  As a result, the count that is
being returned, includes all of the bookmarks in the row.

Signature

Please respond to the Newsgroup for the benefit of others who may be
interested.   Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP

> Last week I thought that I had figured out a way to get the index value of
> a formfield using VBA.  Today I noticed that it fails if I have two or
[quoted text clipped - 10 lines]
> MsgBox oDoc.Variables("Index").Value
> End Sub
Greg Maxey - 26 Feb 2005 15:38 GMT
Doug,

Yes, interesting.  I couldn't find a way around it and opted to use the
formfield name instead of the index.
http://word.mvps.org/FAQs/TblsFldsFms/GetCurFmFldName.htm

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> Hi Greg,
>
[quoted text clipped - 27 lines]
>> Greg Maxey/Word MVP
>> A Peer in Peer to Peer Support
Tony Jollans - 27 Feb 2005 12:11 GMT
There are (at least) two issues here.

Firstly, whatever happens when selecting via the GUI, this is clearly a bug,
Range(0, 100).Bookmarks, say, does NOT contain a bookmark which starts at 110
and it is wrong that such a bookmark is included in the Count.

Knowing that this is what happens, however, at least means that code can be
written to handle the situation. HOWEVER, your basic logic is flawed because
Bookmarks(3), say, is not necessarily the third bookmark from the start of
the document; they just aren't numbered that way.

Enjoy,
Tony

> Doug,
>
[quoted text clipped - 33 lines]
> >> Greg Maxey/Word MVP
> >> A Peer in Peer to Peer Support
Tony Jollans - 27 Feb 2005 23:49 GMT
Apologies, before someone else corrects me! Bookmarks in a Document aren't  
numbered sequentially, but bookmarks in a Range are, so here's an amendment
to your code to work round Word's peculiarity:

Dim oDoc As Document
Set oDoc = ActiveDocument
oDoc.Variables("Index").Value = _
   oDoc.Range(0, Selection.Bookmarks(1).Range.End).Bookmarks.Count
Dim i As Long
For i = oDoc.Variables("Index").Value To 1 Step -1
   If oDoc.Range(0, Selection.Bookmarks(1).Range.End).Bookmarks(i).Start >
Selection.End Then
       oDoc.Variables("Index").Value = oDoc.Variables("Index").Value - 1
   Else
       Exit For
   End If
Next
MsgBox oDoc.Variables("Index").Value

> Doug,
>
[quoted text clipped - 33 lines]
> >> Greg Maxey/Word MVP
> >> A Peer in Peer to Peer Support

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.