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