I agree. davidh9876, could you please clarify precisely what you are trying to do? The problem is that once a macro procedure starts there can be issues with returning focus to the document (for the user to select text) and coming BACK to the processes in the procedure (macro).
Procedures (macros), as white605 points out, can easily handle whatever IS the Selection. However, you seem to be asking for a procedure to start, wait for the user to make a Selection, THEN do something, and then wait again.
I am not sure what AcadSelectionSet.SelectOnScreen does, but in Word, the Selection object
IS whatever is selected on-screen.
If the
action you wish to perform is the same - say, expand the Selection to a sentence and then make the selected text bold (but it could be anything) - it would be much simpler to make a shortcut key that executes the macro to perform that action. The user selects whatever and hits the shortcut key.
Technically, yes, what you appear to asking can be done.
Code:
Option Explicit
Private Sub cmdAction_Click()
If Len(Selection.Text) > 1 Then
With Selection
.Expand Unit:=wdSentence
.Font.Bold = True
End With
Else
MsgBox "Nothing selected."
End If
End Sub
Private Sub cmdDone_Click()
Unload Me
End Sub
This is code for a userform with two commandbuttons, nothing more.
The userform has SystemModal = False. This is critical as it is the only way for the user to be able to switch to the document (to select something) and get back to the active userform.
The Action button simply checks to see if the Selection is greater than one character, and if it is, expand the Selection the sentence, and make it bold. Otherwise it displays a messagebox stating nothing is selected. You could (and I would) have that message displayed on the userform rather than waste the effort of displaying a messagebox.
Note that the test is for Len of the Selection.Text, NOT something like:
Code:
If Selection.Text <> "" Then
While perhaps not intuitive, Selection.Text is NEVER "".
If you have the cursor (Selection) blinking - say - just before the q of:
The quick brown fox
The "q" is NOT selected, the cursor is blinking by its lonesome just before it. You may think that nothing is selected, but that is incorrect.
MsgBox Selection.Text would return......"q".
If collapsed to a point, Selection.Text returns the
next character.
In any case, the above code does indeed allow the user to do multiple selections and then perform actions on those Selections. However, if you try this you will rapidly see that it is rather awkward to use because of the need to switch back and forth.
Gerry