JodieM
Mon Feb 19 16:08:32 CST 2007
On Feb 14, 5:04 pm, "Jean-Guy Marcil" <DontEvenTry@NoSpam> wrote:
> JodieM was telling us:
> JodieM nous racontait que :
>
> >> JodieM was telling us:
> >> JodieM nous racontait que :
>
> <snip>
>
> > Thank You for that Jean-Guy, I had seen the Header and Footer example,
> > I just did not look at it closely enough to see that it would help for
> > this also.
>
> > Can someone please help me with the bit of code that finds out what
> > text is selected.
> > eg if I want to see if the text selected is a Field, what code should
> > I use?
> > I tried selection.information and selection.type but neither of them
> > will tell me that my selection is a field? I could use row and column
> > positions but I think that is quite cumbersome.
>
> Try
>
> '_______________________________________
> If Selection.Range.Fields.Count > 0 Then
> MsgBox "There is at least one field in the currently selected text."
> Else
> MsgBox "There are no fields in the currently selected text."
> End If
> '_______________________________________
>
> But you will need to fine tune, for example, if only part of the field is
> selected, the above will not work.
>
> This will:
> '_______________________________________
> Dim rgeSelected As Range
> Dim rge1 As Range
> Dim rge2 As Range
>
> Set rgeSelected = Selection.Range
>
> With Selection
> .Fields.ToggleShowCodes
> .Fields.ToggleShowCodes
> .Expand wdWord
> 'If the original selection was inside a field but didn't fully enclose
> 'the field, then running Selection,Fields.Count would return 0.
> 'The above 3 lines force the selection to enclose the field,
> 'if it is inside one.
>
> If .Fields.Count = 0 Then
> rgeSelected.Select
> MsgBox "Selection contains no fields"
> ElseIf .Fields.Count > 1 Then
> rgeSelected.Select
> MsgBox "Selection contains more than one field" _
> & " (and therefore it is not inside a field)"
> Else
> 'We now know it contains 1 field - but may also contain some text _
> 'outside of the field, so we need to check
> Set rge1 = .Range
> Set rge2 = .Fields(1).Result
> If rge1 = rge2 Then
> rgeSelected.Select
> MsgBox "Selection is inside a field"
> Else
> rgeSelected.Select
> MsgBox "Selection contains a field but contains" _
> & " text as well"
> End If
> End If
> End With
> '_______________________________________
>
> Test it with different fields of different length.
>
> --
>
> Salut!
> _______________________________________
> Jean-Guy Marcil - Word MVP
> jmarcilREM...@CAPSsympatico.caTHISTOO
> Word MVP site:
http://www.word.mvps.org
Thanks Jean-Guy - Beautiful Code - I think it will work well!!!
Jodie