Inserting a Non-breaking space if...

D

D Collins

Hello,

I have a user form that searches a document for words
such as Report 5 or Section 30, for example, that looks
for these words followed by a digit. When the search
finds it, I want it to insert a non-breaking space
character between Report and 5.

I guess the problem I'm having is to having the selection
move back to the old space, remove it, and put in the non-
breaking space instead. I'm sure it has to be some easy
code, but I'm at a loss.

Thanks, D.
 
H

Helmut Weber

Hi D,
whether your code resides in a userform or not, doesn't matter.
The problem is, that you search for words "such as"! This means,
that what you are searching for, isn't well defined. Anyway,
replacing any space preceding 1 or more digits, wouldn't do your
doc any harm, I guess. The following should achieve that.
Sub Makro21()
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = " ([0-9]{1,})" ' ; instead of , in languages like german
.MatchWildcards = True
.Replacement.Text = "^s\1"
' resetting other parameters advisable
While .Execute(Replace:=wdReplaceOne)
' or wdreplaceall after successfull debugging
oRng.Select ' can be omitted, it is for testing only
oRng.Start = oRng.End
oRng.End = ActiveDocument.Range.End
Wend
End With
End Sub

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 

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