It bugs the heck out of me that there's no easy way to determine if the Selection point is within a field.
I think.
The attached document holds some fairly simple field codes; at the foot of the document are some insane nested field codes.
I developed the macro "TestBothwards" from two simpler macros "TestLeftwards" and "TestRightwards".
My theory is that I can detect that I am, or am not, in a field by a series of ever-expanding circles (so to speak), and you can see me working my way leftwards only (back to the start of the document) in "TestLeftwards", and you can see me working my way rightwards only (on to the end of the document) in "TestRightwards".
The final version, "TestBothwards" is merely the merging of the inner-loop code of the two smaller macros.
I think my logic is correct.
I make no claim to execution speed.
I have previously tested for being within a field code by iterating through all fields in a document, testing each field's range to see if it embraced any part of the selection.
I haven't compared that method to this.
Is my selection point within a field code? (Word 2003)
-
- PlutoniumLounger
- Posts: 15641
- Joined: 24 Jan 2010, 23:23
- Location: brings.slot.perky
Is my selection point within a field code? (Word 2003)
You do not have the required permissions to view the files attached to this post.
He who plants a seed, plants life.
-
- Administrator
- Posts: 78574
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Is my selection point within a field code? (Word 2003)
If I position the insertion point within the field(s) at the end of the document and run any of the three macros, I get
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- 4StarLounger
- Posts: 508
- Joined: 17 Dec 2010, 03:14
Re: Is my selection point within a field code? (Word 2003)
How about something based on:
Code: Select all
Sub Demo()
Application.ScreenUpdating = False
Dim Fld As Field, Rng As Range, FldRng As Range, i As Long
With Selection
If .Fields.Count > 0 Then
MsgBox "The selection includes " & .Fields.Count & " field(s)."
Exit Sub
End If
Set Rng = .Paragraphs.First.Range
Rng.End = .Paragraphs.Last.Range.End
For Each Fld In Rng.Fields
Set FldRng = Fld.Result
FldRng.Start = FldRng.Start - 1
FldRng.End = FldRng.End + 1
If FldRng.Start <= .Start Then
If FldRng.End >= .Start Then
i = i + 1
End If
ElseIf FldRng.End >= .End Then
If FldRng.Start <= .End Then
i = i + 1
End If
ElseIf FldRng.Start >= .Start Then
If FldRng.End <= .End Then
i = i + 1
End If
End If
Next
End With
MsgBox "The selection includes " & i & " field(s)."
Application.ScreenUpdating = True
End Sub
Paul Edstein
[Fmr MS MVP - Word]
[Fmr MS MVP - Word]
-
- PlutoniumLounger
- Posts: 15641
- Joined: 24 Jan 2010, 23:23
- Location: brings.slot.perky
Re: Is my selection point within a field code? (Word 2003)
Ooops.HansV wrote:If I position the insertion point within the field(s) at the end of the document and run any of the three macros, I get
Sorry about that.
Field Codes must be visible! (My normal mode of operation)
He who plants a seed, plants life.
-
- PlutoniumLounger
- Posts: 15641
- Joined: 24 Jan 2010, 23:23
- Location: brings.slot.perky
Re: Is my selection point within a field code? (Word 2003)
Thanks macropod.macropod wrote:How about something based on:
I'll have to investigate this.
If my selection point is within an {XE} I get an error trying to access the fld.result. I think Hans's comment is relevant, that {XE} are different from other {}.
I suspect that an {XE} doesn't deliver a result.
If I place my text cursor within a nested {SEQ} I am told zero fields, which, annoyingly, is True.
In the attached image there are no fields within the selection; the selection is within a field.
You do not have the required permissions to view the files attached to this post.
He who plants a seed, plants life.
-
- 5StarLounger
- Posts: 689
- Joined: 27 Jan 2010, 16:45
- Location: Ottawa, Ontario, Canada
Re: Is my selection point within a field code? (Word 2003)
Hi Chris
Please take a look at the following code.
Please take a look at the following code.
Code: Select all
Option Explicit
Sub SelInField()
Dim lngCurr As Long
Dim lngSt As Long
Dim lngEnd As Long
Dim lngSel As Long
Dim ctr As Long
Dim msg As String
lngSt = Selection.Start
lngEnd = Selection.End
lngSel = lngSt
If lngSt = lngEnd Then
msg = "start and end of the selection are"
ElseIf lngSel = lngSt Then
msg = "start of the selection is"
Else
msg = "end of the selection is"
End If
With ActiveDocument
For ctr = 1 To .Fields.Count
ActiveDocument.Fields(ctr).Select
If Selection.End > lngSel Then
If Selection.Start <= lngSel Then
msg = "The " & msg & " within a field."
Exit For
Else
msg = "The " & msg & " not within a field."
Exit For
End If
End If
Next ctr
.Range(lngSt, lngEnd).Select
If .Fields.Count < ctr Then
msg = "The " & msg & " not within a field."
End If
End With
MsgBox msg
End Sub
Regards
Don
Don
-
- PlutoniumLounger
- Posts: 15641
- Joined: 24 Jan 2010, 23:23
- Location: brings.slot.perky
Re: Is my selection point within a field code? (Word 2003)
Hi Don, Thanks for this. This works (and I like your neat touch that satisfies my grammatical pedantry!).Don Wells wrote:Please take a look at the following code.
I think this is akin to the method we (members) explored at another time in another place:
ChrisGreaves wrote:I have previously tested for being within a field code by iterating through all fields in a document,
He who plants a seed, plants life.
-
- 5StarLounger
- Posts: 689
- Joined: 27 Jan 2010, 16:45
- Location: Ottawa, Ontario, Canada
Re: Is my selection point within a field code? (Word 2003)
Oops! re-inventing the wheel again.ChrisGreaves wrote:I have previously tested for being within a field code by iterating through all fields in a document,
Regards
Don
Don
-
- 5StarLounger
- Posts: 689
- Joined: 27 Jan 2010, 16:45
- Location: Ottawa, Ontario, Canada
Re: Is my selection point within a field code? (Word 2003)
My previous sumission was based on the principle that "One should never write a single line of code where the task casn be achieved with three or four." The following is based on KISS and should be fairly swift.
Note: The code has been revised through the addition of: Line 30; comments; and Line numbers.
Another Note: This code will not detect inclusion in an XE field. I should have paid more attention to Hans' post.
Note: The code has been revised through the addition of: Line 30; comments; and Line numbers.
Another Note: This code will not detect inclusion in an XE field. I should have paid more attention to Hans' post.
Code: Select all
Option Explicit
'---------------------------------------------------------------------------------------
' Procedure : SelInField
' Author : Wells
' Date : 5/29/2011
' Purpose : Determines whether or not the insertion point is within a field
' Approach : This procedure is based on the observation that irrespective of _
a field's ShowCodes state; when the field's ShowCodes state is _
toggled twice, if the insertion point (start of the selection), _
is within the field, the selection will be collapsed and the _
insertion point will move to the start of the field.
'---------------------------------------------------------------------------------------
'
Sub SelInField()
'
Dim lngPosStart As Long
Dim lngPosEnd As Long
Dim msg As String
'Determine the location of the current selection
10 lngPosStart = Selection.Start
20 lngPosEnd = Selection.End
30 Selection.Collapse ' This line is necessary in the event that the _
selection starts one character before the field _
and ends at the end of the field
' Toggle ShowCodes twice. If insertion point within _
field it will be moved tio the beginning of the field.
40 Selection.Fields.ToggleShowCodes
50 Selection.Fields.ToggleShowCodes
60 If Selection.Start = lngPosStart Then
70 Selection.MoveRight ' And try again in case the insertion point _
was already at the start of the field
80 Selection.Fields.ToggleShowCodes
90 Selection.Fields.ToggleShowCodes
100 If Selection.Start = lngPosStart + 1 Then
110 msg = "The start of the selection is NOT within a field."
120 Else
130 msg = "The start of the selection IS within a field."
140 End If
150 Else
160 msg = "The start of the selection IS within a field."
170 End If
180 ActiveDocument.Range(lngPosStart, lngPosEnd).Select
190 MsgBox msg
End Sub
Regards
Don
Don
-
- 5StarLounger
- Posts: 689
- Joined: 27 Jan 2010, 16:45
- Location: Ottawa, Ontario, Canada
Re: Is my selection point within a field code? (Word 2003)
I believe that I have solved it. Chris et. al. if you would be so kind as to test the following code, it would be appreciated.
Notes:
Notes:
- For a Standard Field the cursor is in the field if it is immediately _
before the opening curly bracket, through immediately before the _
closing curly bracket. - For an XE Field the cursor is in the field if it is immediately _
after the opening curly bracket, through immediately after the _
closing curly bracket. - That seems to be the manner in which Word interprets life. If we require the determination to be 'Only within the curly brackets', it is achievable with a litle more work.
Code: Select all
Dim lngPosStart As Long
Dim lngPosEnd As Long
Dim msg As String
Option Explicit
'---------------------------------------------------------------------------------------
' Procedure : SelInField
' Author : Wells
' Date : 5/29/2011
' Purpose : Determines whether or not the insertion point is within a field
' Approach : This procedure is based on the observation that irrespective of _
a non-XE field's ShowCodes state; when the field's ShowCodes state _
is toggled twice, if the insertion point (start of the selection), _
is within the field, the selection will be collapsed and the _
insertion point will move to the start of the field.
' Notes: 1 For a Standard Field the cursor is in the field if it is immediately _
before the opening curly bracket, through immediately before the _
closing curly bracket.
' 2 For an XE Field the cursor is in the field if it is immediately _
after the opening curly bracket, through immediately after the _
closing curly bracket.
'---------------------------------------------------------------------------------------
'
Sub SelInField()
Attribute SelInField.VB_Description = "Macro recorded 5/28/2011 by W.D Wells"
Attribute SelInField.VB_ProcData.VB_Invoke_Func = "Project.NewMacros.Macro1"
'
Dim InField As Boolean
'Determine the location of the current selection
10 lngPosStart = Selection.Start
20 lngPosEnd = Selection.End
30 Selection.Collapse ' This line is necessary in the event that the _
selection starts one character before the field _
and ends at the end of the field
' Toggle ShowCodes twice. If insertion point within _
field it will be moved tio the beginning of the field.
40 Selection.Fields.ToggleShowCodes
50 Selection.Fields.ToggleShowCodes
60 If Selection.Start = lngPosStart Then
70 Selection.MoveRight ' And try again in case the insertion point _
was already at the start of the field
80 Selection.Fields.ToggleShowCodes
90 Selection.Fields.ToggleShowCodes
100 If Selection.Start = lngPosStart + 1 Then
110 msg = "The start of the selection is NOT within a field."
120 Else
130 msg = "The start of the selection IS within a field."
140 InField = True
150 End If
160 Else
170 msg = "The start of the selection IS within a field."
180 InField = True
190 End If
200 If Not InField Then
210 Call SelIn_XE_Field
220 End If
230 ActiveDocument.Range(lngPosStart, lngPosEnd).Select
240 MsgBox msg
End Sub
'---------------------------------------------------------------------------------------
' Procedure : SelIn_XE_Field
' Author : Wells
' Date : 5/29/2011
' Purpose : This procedure is based on the observation that an XE field _
is hidden or exposed by the ShowAll setting of the active pane.
' Notes: 1 For a Standard Field the cursor is in the field if it is immediately _
before the opening curly bracket, through immediately before the _
closing curly bracket.
' 2 For an XE Field the cursor is in the field if it is immediately _
after the opening curly bracket, through immediately after the _
closing curly bracket.
'---------------------------------------------------------------------------------------
'
Public Sub SelIn_XE_Field(Optional Abort As Boolean)
Dim InitState As Boolean
10 InitState = ActiveWindow.ActivePane.View.ShowAll
20 ActiveDocument.Range(lngPosStart, lngPosStart).Select
' Turn ShowAll off
30 If InitState Then ActiveWindow.ActivePane.View.ShowAll = False
40 Selection.MoveRight wdCharacter, 1, wdExtend
50 ActiveWindow.ActivePane.View.ShowAll = True
' If the selection now envelops the field,the initial start of the selection _
point may have been within an XE field.
60 If Selection.Characters.Count > 2 Then
70 ActiveDocument.Range(lngPosStart, lngPosStart).Select
80 ActiveWindow.ActivePane.View.ShowAll = False
90 Selection.MoveLeft wdCharacter, 1, wdExtend
100 ActiveWindow.ActivePane.View.ShowAll = True
' If the start of the initial selection point was within an XE field, the selection _
now envelops the field.
110 If Selection.Characters.Count > 2 Then
120 msg = "The start of the selection IS within a field."
130 End If
140 End If
150 ActiveWindow.ActivePane.View.ShowAll = InitState
End Sub
Regards
Don
Don