PDA

View Full Version : [SOLVED:] Check to see if there is a selection



MacroWizard
12-13-2015, 02:59 PM
All,

I know I'm spamming the forums today. I have code that wraps the current selection in a rich content control. Is there a way to verify that there is a selection before it will place the content control? FYI, A selection can be an image, text, table, etc... As long as the selection range contains something.

Thanks!

MacroWizard
12-13-2015, 03:01 PM
I think this may work, but i'll test to see.


If Selection.Type <> wdSelectionIP Then
Selection.Range.ContentControls.Add (wdContentControlRichText)
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.ParentContentControl.Title = "FacilitatorBlock"
Selection.ParentContentControl.Tag = "FacilitatorBlock"
Else
MsgBox "First select content to be placed within the facilitator block, then click the Facilitator Block button again.", vbInformation, "Error: No Content Selected"
End If

EDIT: This works. Keeping here for future reference.

gmayor
12-14-2015, 11:27 PM
You need to embrace ranges and named variables. Working with selections will get you hopelessly tied in knots

Sub InsertCC()
Dim oRng As Range
Dim oCC As ContentControl
Set oRng = Selection.Range
If Len(oRng) = 0 Then GoTo err_Handler
Set oCC = ActiveDocument.ContentControls.Add(wdContentControlRichText, oRng)
oCC.Title = "FacilitatorBlock"
oCC.Tag = "FacilitatorBlock"
lbl_Exit:
Set oCC = Nothing
Set oRng = Nothing
Exit Sub
err_Handler:
MsgBox "Nothing selected!"
GoTo lbl_Exit
End Sub

gmaxey
12-15-2015, 07:47 AM
Graham,

This may appear as a drive by but I don't mean it that way ;-)


Sub InsertCC()
Dim oCC As ContentControl
If Selection.Type <> wdSelectionIP Then
Set oCC = ActiveDocument.ContentControls.Add(wdContentControlRichText, Selection.Range)
oCC.Title = "FacilitatorBlock"
oCC.Tag = "FacilitatorBlock"
Else: MsgBox "Nothing selected"
End If
lbl_Exit:
Exit Sub
End Sub