Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
andreas - 30 Mar 2008 00:10 GMT
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
Doug Robbins - Word MVP - 30 Mar 2008 01:45 GMT
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

Signature

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
andreas - 30 Mar 2008 21:37 GMT
On Mar 30, 2:45 am, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> Recorded Code:
>
[quoted text clipped - 41 lines]
>
> 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
Helmut Weber - 30 Mar 2008 01:55 GMT
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
andreas - 30 Mar 2008 21:47 GMT
> Hi Andreas,
>
[quoted text clipped - 23 lines]
>
> 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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.