PDA

View Full Version : Detect\Count Discontinuous Selections



gmaxey
07-10-2014, 09:47 AM
If you create one or more discontinuous selections in Word and then attempt to create a content control e.g., "Plain Text", Word's built-in alert reports that "Plain text controls cannot be inserted around multiple selections." Ok. Something in Word must be detecting the multiple selection condition.


Does anyone know a way with VBA to detect this condition or count the number of selections?

Thanks.

macropod
07-10-2014, 07:18 PM
I guess one way of detecting would be to try to insert a plain-text content control or doing anything else that isn't allowed when a discontiguous range is selected ...

I had though one might be able to use something like the following, to test whether the selected range and the ranges spanned have the same character count, but it only ever returns to last-selected range (which is not necessarily the last range selected).

Sub Test()
Dim Rng As Range
With Selection
Set Rng = ActiveDocument.Range(.Start, .End)
MsgBox .Characters.Count & vbTab & Rng.Characters.Count
End With
End Sub

gmaxey
07-10-2014, 07:29 PM
Paul,

Funny you mention content controls. That is the issue behind the my quest. Attempting to insert a CC at a discontinuous selection results in a built-in Word user alert dialog that can't be suppressed by Application.Display alerts. If you attempt with code you get the alert which is then followed by a RTE 4198 "Command Failed." Item trying to determine if the selection is discontinuous before attempting to insert the control so I can suppress the alert by explicitly raising the error then handling it.

macropod
07-10-2014, 09:58 PM
Hi Greg,

Are you sure it's an issue with a discontinuous selection? I just tried it in VBA and the content control gets inserted at the last-selected range. No error message. It's only when inserting through the GUI that I see an error message, but that doesn't flow through to RTE 4198 "Command Failed."

gmaxey
07-11-2014, 05:23 AM
Paul,

Yes I'm sure. Using the following code:

Sub ScratchMacro()
'Starting with a discountinuous selection
Application.DisplayAlerts = False
On Error GoTo Err_Selection
Selection.Range.ContentControls.Add wdContentControlText, Selection.Range
lbl_Exit:
Exit Sub
Err_Selection:
MsgBox Err.Number & " " & Err.Description
End Sub

I get the built-in alert "Plain text controls cannot be applied here" then the handled error message.

macropod
07-11-2014, 06:27 AM
Running your code with multiple selections on my Office 2010 system inserts the content control with no error message ...

gmaxey
07-11-2014, 06:42 AM
Paul,

You're right and I should have mentioned Office 2013. It does behave as you described in Word 2101. In Word 2013 it is as I've described. That makes it even more desirable to find a way to detect the discontinuous selection :-(

macropod
07-11-2014, 03:49 PM
What happens with:

Sub Test()
Dim Rng As Range
Set Rng = Selection.Range
ActiveDocument.ContentControls.Add wdContentControlText, Rng
End Sub

gmaxey
07-11-2014, 04:22 PM
Paul,

No change. In Word 2003 I get the built-in dialog and then the error message.

fumei
07-12-2014, 05:56 PM
Out of curiosity, can you get an accurate count of characters from your discontinuous selection? That is, does count using Start and End not include the range not included?

If you can, then

If Selection.Range.End - Selection.Range.Start > Selection.Character.Countsuggests there are gaps i.e. the Selection.RANGE has a count of space greater than the number of characters.

Of course this does not work with earlier VBA (well the actual instruction works, it simply can not give meaningful information for this scenario) because VBA completely ignores discontiguous ranges of Selection.

gmaxey
07-13-2014, 12:46 AM
Gerry,
No I can't get an accurate count. For example if I select 12345 and 9 from the sample text below and run the code I get 1, 9 and 10. As far as VBA is concerned for counting purposes, the only thing selected is the "9"

12345
6789


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Debug.Print Selection.Characters.Count
Debug.Print Selection.Start
Debug.Print Selection.End
End Sub

fumei
07-13-2014, 03:29 AM
OK, so vba is still doing its "normal" thing. You may be out of luck. VBA is doing an internal (not exposed) operation when it is detecting the discontinuos section "error". Or rather, as stated, it ignores discontinuous ranges and only "sees" the last one. If it is truly an internal unexposed operation there is no way to get a handle on it.

gmaxey
07-13-2014, 07:14 AM
It likely needs some more testing and cleanup (Paul?), but this seems to provide a step in the right direction, if not a positive result. You need a reference to the MS Forms 2.0 OL.


Sub Test()
MsgBox IsDiscontiguous
End Sub
Function IsDiscontiguous() As Boolean
Dim lngRefCount As Long
Dim strRefText As String
Dim lngCharacterCount As Long
Dim oCB As MSForms.DataObject
Dim strCBText As String
Dim i As Long
IsDiscontiguous = False
If Selection.Type = wdSelectionIP Then Exit Function
strRefText = Selection.Text
strRefText = Replace(strRefText, Chr(10), "")
strRefText = Replace(strRefText, Chr(11), "")
strRefText = Replace(strRefText, Chr(13), "")
lngRefCount = Len(strRefText)
Selection.Copy
Set oCB = New MSForms.DataObject
oCB.GetFromClipboard
strCBText = oCB.GetText
strCBText = Replace(strCBText, Chr(10), "")
strCBText = Replace(strCBText, Chr(11), "")
strCBText = Replace(strCBText, Chr(13), "")
lngCharacterCount = Len(strCBText)
If lngRefCount <> lngCharacterCount Then
IsDiscontiguous = True
End If
End Function


I don't know a lot about working with the clipboard. One thing I don't like about this approach is that the user is not aware of the copy action to the clipboard. Is there some way to clear/UNDO that action so the clipboard content is restored to the user's state?

macropod
07-13-2014, 08:20 PM
Hi Greg,

Without worrying too much about the code cleanup, instead of using Selection.Copy you might use Selection.PutInClipboard. I too, haven't worked with the clipboard much, but that might preserve whatever the user has copied to it. Actually, this discussion suggests you've played around with the clipboard rather more than I have: http://social.msdn.microsoft.com/Forums/office/en-US/ee9e0d28-0f1e-467f-8d1d-1a86b2db2878/a-clipboard-object-for-vba-including-microsoft-word?forum=worddev

gmaxey
07-13-2014, 08:41 PM
Paul,

Actually I revisited that thread earlier today. I couldn't see a way to use the class to "copy" to the VBAClipboard. Maybe I didn't try hard enough and will have another go at it tomorrow.

gmaxey
07-14-2014, 04:58 AM
No joy. I can't make much sense of that class or make it work and the truth is any solution seems to be more effort and trouble than the minor problem I'm trying to resolve justifies. Thanks.