Search and replace macro needed

G

GregNga

I need a macro that will replace a space with a non-breaking space <b>for the
selected text only</b>
 
M

macropod

Hi Greg,

Why do you need a macro, when you can use a simple Find/Replace operation?
Select the desired range then:
Find: ' '
Replace: '^s'
Click 'Replace All'.
 
P

Pesach Shelnitz

Hi,

If text containing one or more spaces is selected, the following macro will
replace the spaces by nonbreaking spaces in every occurrence of the selected
expression in the document.

Sub InsertNonBreakingSpace()
Dim FindString As String

FindString = Selection.Text
With ActiveDocument.Content.Find
.Text = FindString
.Replacement.Text = Replace(FindString, " ", ChrW(&HA0))
.Forward = True
.Wrap = wdFindStop
.Execute Replace:=wdReplaceAll
End With
End Sub
 
G

GregNga

Because I want to assign the macro to a toolbar button rather than have to go
to the EDIT menu, click REPLACE, click MORE, key in a SPACE in the "Find
What" box, click SPECIAL, select "non breaking space", click FIND.

I think having one button to click that does all this would be much easier

macropod said:
Hi Greg,

Why do you need a macro, when you can use a simple Find/Replace operation?
Select the desired range then:
Find: ' '
Replace: '^s'
Click 'Replace All'.


--
Cheers
macropod
[Microsoft MVP - Word]


GregNga said:
I need a macro that will replace a space with a non-breaking space <b>for the
selected text only</b>
.
 
P

Peter T. Daniels

Ctrl-H opens the Find/Replace dialog with the cursor in the Find What?
box. Then follow macrpod's instructions. Four keystrokes and a click.
 
M

macropod

Well, if you want to go to all that trouble, start the macro recorder and execute a Find/Replace using the parameters I gave you.
The code thus produced is generic and quite efficient. Mind you, for a single space, selecting the offending space character and
pressing Ctrl-Shift-Space is quicker than selecting & executing any macro - especially once you get to Word 2007 and beyond.

--
Cheers
macropod
[Microsoft MVP - Word]


GregNga said:
Because I want to assign the macro to a toolbar button rather than have to go
to the EDIT menu, click REPLACE, click MORE, key in a SPACE in the "Find
What" box, click SPECIAL, select "non breaking space", click FIND.

I think having one button to click that does all this would be much easier

macropod said:
Hi Greg,

Why do you need a macro, when you can use a simple Find/Replace operation?
Select the desired range then:
Find: ' '
Replace: '^s'
Click 'Replace All'.


--
Cheers
macropod
[Microsoft MVP - Word]


GregNga said:
I need a macro that will replace a space with a non-breaking space <b>for the
selected text only</b>
.
 

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