message to appear when macro is run while no text is selected

J

James

Hi

Few months ago I asked for help in applying a simple recorded macro to
selected text. Graham Mayor replied, advising me to do a small change in the
macro (link to post shown below). And it worked. My problem now is that when
I accidentally click the shortcut button for the corrected macro while no
selection is made, the macro runs for all the document. I know I can always
undo, but I'm just wondering if it's possible for the macro to display a
warning message that no selection is made when it's clicked. Of course, it
should run normally when text is selected.

Thank you.

Link to my previous post
https://www.microsoft.com/office/co...-office-word&lang=en&cr=US&sloc=en-us&m=1&p=1
 
G

Greg Maxey

James,

Evaluate the selection type:

Sub ScratchMacro()
With Selection
If .Type = wdSelectionNormal Then
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[aeiouAEIOU]"
.Replacement.Text = ""
.Forward = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Else
MsgBox "Nothing is selected"
End If
End With
End Sub
 
J

James

That was fast, and great! Thanks Greg.

Greg Maxey said:
James,

Evaluate the selection type:

Sub ScratchMacro()
With Selection
If .Type = wdSelectionNormal Then
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[aeiouAEIOU]"
.Replacement.Text = ""
.Forward = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Else
MsgBox "Nothing is selected"
End If
End With
End Sub


--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org



James said:
Hi

Few months ago I asked for help in applying a simple recorded macro to
selected text. Graham Mayor replied, advising me to do a small change in
the
macro (link to post shown below). And it worked. My problem now is that
when
I accidentally click the shortcut button for the corrected macro while no
selection is made, the macro runs for all the document. I know I can
always
undo, but I'm just wondering if it's possible for the macro to display a
warning message that no selection is made when it's clicked. Of course, it
should run normally when text is selected.

Thank you.

Link to my previous post:
https://www.microsoft.com/office/co...-office-word&lang=en&cr=US&sloc=en-us&m=1&p=1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top