Insertion of nonbreaking space between a number and the degrees character (° ANSI value 176)

A

andreas

Dear Experts:

I would like to be able to automatically insert a nonbreaking space
between all instances of a number and the character set °C (ANSII
Value of 176) using VBA. For example:

40 °C or 5 °C

I know how to do it using the Find And Replace Dialog Field. But I
also would like to be able to do it programmatically. The "Find.Text"
Line is enough.

Help is appreciated.


Thank you very much in advance.

Regards,

Andreas
 
D

Doug Robbins - Word MVP

Recorded Code:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "°C"
.Replacement.Text = "^s°C"
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Dear Experts:

I would like to be able to automatically insert a nonbreaking space
between all instances of a number and the character set °C (ANSII
Value of 176) using VBA. For example:

40 °C or 5 °C

I know how to do it using the Find And Replace Dialog Field. But I
also would like to be able to do it programmatically. The "Find.Text"
Line is enough.

Help is appreciated.


Thank you very much in advance.

Regards,

Andreas
 
H

Helmut Weber

Hi Andreas,

in case that there is only one ordinary space chr(32)
between a digit and "°C":

Sub Test002()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "([0-9]) (°C)"
.Replacement.Text = "\1^0160\2"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub

In case that there is nothing between
the digit and "°C" or more than one ordinary space,
things get complicated.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
A

andreas

Recorded Code:

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "°C"
        .Replacement.Text = "^s°C"
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP


Dear Experts:

I would like to be able to automatically insert a nonbreaking space
between all instances of a number and the character set °C (ANSII
Value of 176) using VBA. For example:

40 °C or 5 °C

I know how to do it using the Find And Replace Dialog Field. But I
also would like to be able to do it programmatically. The "Find.Text"
Line is enough.

Help is appreciated.

Thank you very much in advance.

Regards,

Andreas

Dear Doug,
Thank you for the quick answer. With your information and Helmut's
previous help on a similiar code I came up with my own solution. See
my answer to Helmut. Thanks Doug. Regards, Andreas
 
A

andreas

Hi Andreas,

in case that there is only one ordinary space chr(32)
between a digit and "°C":

Sub Test002()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
   .Text = "([0-9]) (°C)"
   .Replacement.Text = "\1^0160\2"
   .MatchWildcards = True
   .Execute Replace:=wdReplaceAll
End With
End Sub

In case that there is nothing between
the digit and "°C" or more than one ordinary space,
things get complicated.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP


Hey Helmut,

thank you for your help. You may remember, some months ago I posted a
similiar question on the insertion of nonbreaking spaces between Page
and the Page number (e.g. S. 37). I also wanted the code to show me
the instances of the insertions made. You and Greg Maxey came up with
a superb solution (see below). I now just slightly re-wrote that
former code for my latest requirement. It is running just fine.

Thank you so much for your terrific help. Regards, Andreas



Sub NonBreakingSpacesDegree()
'Courtesy by Greg Maxey, and Helmut Weber (Google Groups)
Dim rngStory As Range
Dim i As Long

If MsgBox("Would you like to insert a nonbreaking space between a
number and the following degrees Character" _
, vbYesNo, "Insertion of nonbreaking space between the number and
Degree Celsius Sign (5 °C)") = vbYes Then


For Each rngStory In ActiveDocument.StoryRanges
Do
With rngStory.Find
.Text = "([0-9]) {1;}(°C)"
.MatchWildcards = True
.Replacement.Text = "\1^0160\2"
While .Execute(Replace:=wdReplaceOne)
i = i + 1
rngStory.Collapse wdCollapseEnd
Wend
End With
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next rngStory
If i > 0 Then
MsgBox "Replacement made " & i & " time(s)."
Else
MsgBox "Keine Grad-Zeichen vorhanden oder die geschützten
Leerzeichen wurden schon gesetzt!", vbOKOnly, "Es wurden keine
Ersetzungen vorgenommen!"
End If
End If

Call NonBreakingSpacesDegree2

End Sub

Sub NonBreakingSpacesDegree2()
'Greg Maxey, Google along with Helmut Weber
Dim rngStory As Range
Dim i As Long

If MsgBox("Would you like to insert a nonbreaking space between a
number followed by any number of spaces and the ensuing degrees
Character" _
, vbYesNo, "Insertion of nonbreaking space between the number and
Degree Celsius Sign (5°C)") = vbYes Then


For Each rngStory In ActiveDocument.StoryRanges
Do
With rngStory.Find
.Text = "([0-9])(°C)"
.MatchWildcards = True
.Replacement.Text = "\1^s\2"
While .Execute(Replace:=wdReplaceOne)
i = i + 1
rngStory.Collapse wdCollapseEnd
Wend
End With
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next rngStory
If i > 0 Then
MsgBox "Replacement made " & i & " time(s)."
Else
MsgBox "Keine Grad-Zeichen vorhanden oder Geschützte Leerzeichen
wurden schon gesetzt!", vbOKOnly, "Es wurden keine Ersetzungen
vorgenommen!"
End If
End If

End Sub
 

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