Search and replace macro needed

  • Thread starter Thread starter GregNga
  • Start date Start date
G

GregNga

I need a macro that will replace a space with a non-breaking space <b>for the
selected text only</b>
 
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'.
 
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
 
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>
.
 
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.
 
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>
.
 
Back
Top