Converting soft space to hard space

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15618
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Converting soft space to hard space

Post by ChrisGreaves »

Not one of my better efforts, but it got the job done.

Code: Select all

Sub Macro1()
'''
''' I had reason quickly to change some soft spaces to hard spaces to stop "Win 8" and "from 3.1" etc from breaking across lines.
'''
'''
    Selection.HomeKey Unit:=wdStory ' go to top of document
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "^$ ^#"
'        .Replacement.Text = "^&" ' we will do our own replacement
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    While Selection.Find.Execute
        Dim strText As String
        strText = Left(Selection.Text, 1) & Chr(160) & Right(Selection.Text, 1) ' replace soft space with hard space
        Selection.Text = strText
    Wend
End Sub
There's nothing heavier than an empty water bottle

User avatar
HansV
Administrator
Posts: 78478
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Converting soft space to hard space

Post by HansV »

A version using wildcards that doesn't loop:

Code: Select all

Sub Macro1()
'''
''' I had reason quickly to change some soft spaces to hard spaces
''' to stop "Win 8" and "from 3.1" etc from breaking across lines.
'''
'''
    Selection.HomeKey Unit:=wdStory ' go to top of document
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "([A-Za-z])( )([0-9])"
        .Replacement.Text = "\1^s\3"
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With
End Sub
Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15618
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Converting soft space to hard space

Post by ChrisGreaves »

HansV wrote:A version using wild cards that doesn't loop:

Code: Select all

        .Text = "([A-Za-z])( )([0-9])"
        .Replacement.Text = "\1^s\3"
:gah!:

Thanks Hans. I was sure there was a better way.
There's nothing heavier than an empty water bottle

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15618
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Converting soft space to hard space

Post by ChrisGreaves »

HansV wrote:A version using wildcards that doesn't loop:
Thanks Hans; I finally got around to examining your code in more detail.

Code: Select all

        .Text = "([A-Za-z])( )([0-9])" ' any of 52 letters, a space, any of 10 digits
        .Replacement.Text = "\1^s\3" ' 1st character of found string, hard space, 3rd character of found string
But where would I find the help data for the “\1” stuff?
I nutted it out from my example and your code.
I could find in the MSWord User help the use of “^1” and “^2”, using a caret, but nothing there, and as far as I could see nothing in the VBA help that referred to the use of the back-slash escape sequence.

Specifically: Did you base your solution on something in a Help file somewhere?
There's nothing heavier than an empty water bottle

User avatar
HansV
Administrator
Posts: 78478
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Converting soft space to hard space

Post by HansV »

See for example Find and Replace using wildcards on Graham Mayor's website.
Best wishes,
Hans