Testing if selection is in field result text

  • Thread starter christophercbrewster via OfficeKB.com
  • Start date
C

christophercbrewster via OfficeKB.com

My software needs to insert a field in text, so I'd like to make sure the
cursor isn't already in a field result. When the selection is an insertion
point, I expected Selection.Fields.Count to equal 1, but it's 0. I can't find
another way to detect that I'm in a field result, probably select it, and
move to the left or right of it. Is there a way to do it?
 
T

Tony Jollans

I'm afraid that, although there are various ways to do it, this isn't at all
easy. Here is a Function that will return True or False indicating whether
the Selection includes any part of a Field - it's probably more than you
want but it will give you something to work from:

Function SelectionInField() As Boolean

Dim Discrepancy As Long

With ActiveDocument.StoryRanges(Selection.StoryType)
Discrepancy = -.Fields.Count
.SetRange ActiveDocument.StoryRanges(Selection.StoryType).Start,
Selection.Start
Discrepancy = Discrepancy + .Fields.Count
.SetRange Selection.End,
ActiveDocument.StoryRanges(Selection.StoryType).End
Discrepancy = Discrepancy + .Fields.Count
End With

SelectionInField = Discrepancy <> 0

End Function
 
C

christophercbrewster via OfficeKB.com

Thanks-- This works, when the field codes are displayed in the text (and once
I deciphered the web site's garbled line breaks). It seems that a function
like this would be needed enough to have a built-in function for it.

Tony said:
I'm afraid that, although there are various ways to do it, this isn't at all
easy. Here is a Function that will return True or False indicating whether
the Selection includes any part of a Field - it's probably more than you
want but it will give you something to work from:

Function SelectionInField() As Boolean

Dim Discrepancy As Long

With ActiveDocument.StoryRanges(Selection.StoryType)
Discrepancy = -.Fields.Count
.SetRange ActiveDocument.StoryRanges(Selection.StoryType).Start,
Selection.Start
Discrepancy = Discrepancy + .Fields.Count
.SetRange Selection.End,
ActiveDocument.StoryRanges(Selection.StoryType).End
Discrepancy = Discrepancy + .Fields.Count
End With

SelectionInField = Discrepancy <> 0

End Function

--
Christopher Brewster
Lockheed Martin, Eagan MN

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200901/1
 
T

Tony Jollans

Sorry about the line breaks.

It should work whether field codes or results are shown; does it not always
do so for you?
 
C

christophercbrewster via OfficeKB.com

You're right, it does work on the result text. I'm not sure why my first test
failed. It didn't matter because I simply displayed the codes, did what I
needed, and hid the codes.

As to the line breaks, I've seen quite a bit of code come out strangely on
this site. One common effect is that a comments at the right end of a line
becomes a new line appearing following the code line-- looks like a line wrap
but always seems to be at the ' character.

Tony said:
Sorry about the line breaks.

It should work whether field codes or results are shown; does it not always
do so for you?
Thanks-- This works, when the field codes are displayed in the text (and
once
[quoted text clipped - 35 lines]

--
Christopher Brewster
Lockheed Martin, Eagan MN

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200901/1
 

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