Temperature conversion macro

G

gregg098

I am trying to write a macro which searches through a document for a
temperature value such as 395 °F and will convert it to celcius and display
both values in the format 395 °F (xxx °C). I have tried all sorts of things
and cannot get the code to find the temperature values and process it. Does
anyone have any suggestions?

Public Sub macro4()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1-4} °F")
.Forward = True
.Wrap = wdFindContinue
Do While .Execute
oRg.Text = ((5 / 9) * ((Left(oRg.Text, Len(oRg.Text) - 3)) - 32))
oRg.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub

This code works if I have it find a specifc value, but not if I use the
[0-9]{1-4} values. Also, my output does not put it in the format I specified
above, I just want to get it to work first. Also, I would like the output to
be no more than one decimal place.

Thanks for the help
 
J

Jay Freedman

Hi Greg,

The problem you're having is that the syntax of the wildcard expression for
repetitions of a character requires a comma, not the dash you were using. If
you tried that expression in the Find dialog in the user interface, it would
say "The Find What text contains a Pattern Match expression which is not
valid."

Here's the rest of the macro to compute and format the result. Notice that
you need to use the # mark to make the constants in the equation
floating-point instead of integer; otherwise the computed value would always
be zero (because 5/9 in integer arithmetic is zero).

Public Sub macro4A()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText As String, sVal As Single

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1,4} °F")
.Forward = True
.Wrap = wdFindStop
Do While .Execute
sText = Left(oRg.Text, Len(oRg.Text) - 3)
On Error Resume Next
sVal = CSng(sText)
If Err.Number = 0 Then
sVal = (5# / 9#) * (sVal - 32#)
sText = Format(sVal, "0.0")
oRg.InsertAfter " (" & sText & " °C)"
oRg.Collapse Direction:=wdCollapseEnd
End If
Loop
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
G

gregg098

Works perfectly. Thanks for the help.

Jay Freedman said:
Hi Greg,

The problem you're having is that the syntax of the wildcard expression for
repetitions of a character requires a comma, not the dash you were using. If
you tried that expression in the Find dialog in the user interface, it would
say "The Find What text contains a Pattern Match expression which is not
valid."

Here's the rest of the macro to compute and format the result. Notice that
you need to use the # mark to make the constants in the equation
floating-point instead of integer; otherwise the computed value would always
be zero (because 5/9 in integer arithmetic is zero).

Public Sub macro4A()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText As String, sVal As Single

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1,4} °F")
.Forward = True
.Wrap = wdFindStop
Do While .Execute
sText = Left(oRg.Text, Len(oRg.Text) - 3)
On Error Resume Next
sVal = CSng(sText)
If Err.Number = 0 Then
sVal = (5# / 9#) * (sVal - 32#)
sText = Format(sVal, "0.0")
oRg.InsertAfter " (" & sText & " °C)"
oRg.Collapse Direction:=wdCollapseEnd
End If
Loop
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
I am trying to write a macro which searches through a document for a
temperature value such as 395 °F and will convert it to celcius and
display both values in the format 395 °F (xxx °C). I have tried all
sorts of things and cannot get the code to find the temperature
values and process it. Does anyone have any suggestions?

Public Sub macro4()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1-4} °F")
.Forward = True
.Wrap = wdFindContinue
Do While .Execute
oRg.Text = ((5 / 9) * ((Left(oRg.Text, Len(oRg.Text) - 3)) -
32)) oRg.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub

This code works if I have it find a specifc value, but not if I use
the [0-9]{1-4} values. Also, my output does not put it in the format
I specified above, I just want to get it to work first. Also, I would
like the output to be no more than one decimal place.

Thanks for the help
 
G

gregg098

Actually, I just realized something. How would I look for the possibility of
a decimal place? For example, 56.3 °F. This macro works great, but if theres
a decimal, it only converts the numbers after it. The document I wish to use
this on contain various values, some with decimals, some without.

Any further help is appreciated.

gregg098 said:
Works perfectly. Thanks for the help.

Jay Freedman said:
Hi Greg,

The problem you're having is that the syntax of the wildcard expression for
repetitions of a character requires a comma, not the dash you were using. If
you tried that expression in the Find dialog in the user interface, it would
say "The Find What text contains a Pattern Match expression which is not
valid."

Here's the rest of the macro to compute and format the result. Notice that
you need to use the # mark to make the constants in the equation
floating-point instead of integer; otherwise the computed value would always
be zero (because 5/9 in integer arithmetic is zero).

Public Sub macro4A()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText As String, sVal As Single

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1,4} °F")
.Forward = True
.Wrap = wdFindStop
Do While .Execute
sText = Left(oRg.Text, Len(oRg.Text) - 3)
On Error Resume Next
sVal = CSng(sText)
If Err.Number = 0 Then
sVal = (5# / 9#) * (sVal - 32#)
sText = Format(sVal, "0.0")
oRg.InsertAfter " (" & sText & " °C)"
oRg.Collapse Direction:=wdCollapseEnd
End If
Loop
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
I am trying to write a macro which searches through a document for a
temperature value such as 395 °F and will convert it to celcius and
display both values in the format 395 °F (xxx °C). I have tried all
sorts of things and cannot get the code to find the temperature
values and process it. Does anyone have any suggestions?

Public Sub macro4()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1-4} °F")
.Forward = True
.Wrap = wdFindContinue
Do While .Execute
oRg.Text = ((5 / 9) * ((Left(oRg.Text, Len(oRg.Text) - 3)) -
32)) oRg.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub

This code works if I have it find a specifc value, but not if I use
the [0-9]{1-4} values. Also, my output does not put it in the format
I specified above, I just want to get it to work first. Also, I would
like the output to be no more than one decimal place.

Thanks for the help
 
D

Doug Robbins - Word MVP

Replace

.Text = ("[0-9]{1,4} °F")


with

..Text = ("[0-9.]{1,5} °F")

increase the 5 to a 6 if your numbers might have two decimal places

--
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

gregg098 said:
Actually, I just realized something. How would I look for the possibility
of
a decimal place? For example, 56.3 °F. This macro works great, but if
theres
a decimal, it only converts the numbers after it. The document I wish to
use
this on contain various values, some with decimals, some without.

Any further help is appreciated.

gregg098 said:
Works perfectly. Thanks for the help.

Jay Freedman said:
Hi Greg,

The problem you're having is that the syntax of the wildcard expression
for
repetitions of a character requires a comma, not the dash you were
using. If
you tried that expression in the Find dialog in the user interface, it
would
say "The Find What text contains a Pattern Match expression which is
not
valid."

Here's the rest of the macro to compute and format the result. Notice
that
you need to use the # mark to make the constants in the equation
floating-point instead of integer; otherwise the computed value would
always
be zero (because 5/9 in integer arithmetic is zero).

Public Sub macro4A()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText As String, sVal As Single

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1,4} °F")
.Forward = True
.Wrap = wdFindStop
Do While .Execute
sText = Left(oRg.Text, Len(oRg.Text) - 3)
On Error Resume Next
sVal = CSng(sText)
If Err.Number = 0 Then
sVal = (5# / 9#) * (sVal - 32#)
sText = Format(sVal, "0.0")
oRg.InsertAfter " (" & sText & " °C)"
oRg.Collapse Direction:=wdCollapseEnd
End If
Loop
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so
all may benefit.

gregg098 wrote:
I am trying to write a macro which searches through a document for a
temperature value such as 395 °F and will convert it to celcius and
display both values in the format 395 °F (xxx °C). I have tried all
sorts of things and cannot get the code to find the temperature
values and process it. Does anyone have any suggestions?

Public Sub macro4()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1-4} °F")
.Forward = True
.Wrap = wdFindContinue
Do While .Execute
oRg.Text = ((5 / 9) * ((Left(oRg.Text, Len(oRg.Text) - 3)) -
32)) oRg.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub

This code works if I have it find a specifc value, but not if I use
the [0-9]{1-4} values. Also, my output does not put it in the format
I specified above, I just want to get it to work first. Also, I would
like the output to be no more than one decimal place.

Thanks for the help
 
J

Jay Freedman

I thought that might be a problem. :)

Change the .Text assignment to

.Text = ("[0-9.]{1,} °F")

This does two things: Placing the period inside the brackets lets the search
match any sequence of consecutive digits and decimal points. Changing the
repetition expression to {1,} allows it to match as many of those characters
as possible instead of limiting it to a maximum of 4.

Although this search could match a malformed text like 123.456.789 °F, the
CSng function would throw an error and refuse to convert it to a number; the
'If Err.Number' test would then skip over the rest of the processing for
that match and go looking for the next one.

--
Jay
Actually, I just realized something. How would I look for the
possibility of a decimal place? For example, 56.3 °F. This macro
works great, but if theres a decimal, it only converts the numbers
after it. The document I wish to use this on contain various values,
some with decimals, some without.

Any further help is appreciated.

gregg098 said:
Works perfectly. Thanks for the help.

Jay Freedman said:
Hi Greg,

The problem you're having is that the syntax of the wildcard
expression for repetitions of a character requires a comma, not the
dash you were using. If you tried that expression in the Find
dialog in the user interface, it would say "The Find What text
contains a Pattern Match expression which is not valid."

Here's the rest of the macro to compute and format the result.
Notice that you need to use the # mark to make the constants in the
equation floating-point instead of integer; otherwise the computed
value would always be zero (because 5/9 in integer arithmetic is
zero).

Public Sub macro4A()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText As String, sVal As Single

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1,4} °F")
.Forward = True
.Wrap = wdFindStop
Do While .Execute
sText = Left(oRg.Text, Len(oRg.Text) - 3)
On Error Resume Next
sVal = CSng(sText)
If Err.Number = 0 Then
sVal = (5# / 9#) * (sVal - 32#)
sText = Format(sVal, "0.0")
oRg.InsertAfter " (" & sText & " °C)"
oRg.Collapse Direction:=wdCollapseEnd
End If
Loop
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

gregg098 wrote:
I am trying to write a macro which searches through a document for
a temperature value such as 395 °F and will convert it to celcius
and display both values in the format 395 °F (xxx °C). I have
tried all sorts of things and cannot get the code to find the
temperature values and process it. Does anyone have any
suggestions?

Public Sub macro4()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1-4} °F")
.Forward = True
.Wrap = wdFindContinue
Do While .Execute
oRg.Text = ((5 / 9) * ((Left(oRg.Text, Len(oRg.Text) - 3)) -
32)) oRg.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub

This code works if I have it find a specifc value, but not if I use
the [0-9]{1-4} values. Also, my output does not put it in the
format I specified above, I just want to get it to work first.
Also, I would like the output to be no more than one decimal place.

Thanks for the help
 
G

gregg098

Thank you both again. Ill try it out at work tomorrow.

Jay Freedman said:
I thought that might be a problem. :)

Change the .Text assignment to

.Text = ("[0-9.]{1,} °F")

This does two things: Placing the period inside the brackets lets the search
match any sequence of consecutive digits and decimal points. Changing the
repetition expression to {1,} allows it to match as many of those characters
as possible instead of limiting it to a maximum of 4.

Although this search could match a malformed text like 123.456.789 °F, the
CSng function would throw an error and refuse to convert it to a number; the
'If Err.Number' test would then skip over the rest of the processing for
that match and go looking for the next one.

--
Jay
Actually, I just realized something. How would I look for the
possibility of a decimal place? For example, 56.3 °F. This macro
works great, but if theres a decimal, it only converts the numbers
after it. The document I wish to use this on contain various values,
some with decimals, some without.

Any further help is appreciated.

gregg098 said:
Works perfectly. Thanks for the help.

:

Hi Greg,

The problem you're having is that the syntax of the wildcard
expression for repetitions of a character requires a comma, not the
dash you were using. If you tried that expression in the Find
dialog in the user interface, it would say "The Find What text
contains a Pattern Match expression which is not valid."

Here's the rest of the macro to compute and format the result.
Notice that you need to use the # mark to make the constants in the
equation floating-point instead of integer; otherwise the computed
value would always be zero (because 5/9 in integer arithmetic is
zero).

Public Sub macro4A()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText As String, sVal As Single

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1,4} °F")
.Forward = True
.Wrap = wdFindStop
Do While .Execute
sText = Left(oRg.Text, Len(oRg.Text) - 3)
On Error Resume Next
sVal = CSng(sText)
If Err.Number = 0 Then
sVal = (5# / 9#) * (sVal - 32#)
sText = Format(sVal, "0.0")
oRg.InsertAfter " (" & sText & " °C)"
oRg.Collapse Direction:=wdCollapseEnd
End If
Loop
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

gregg098 wrote:
I am trying to write a macro which searches through a document for
a temperature value such as 395 °F and will convert it to celcius
and display both values in the format 395 °F (xxx °C). I have
tried all sorts of things and cannot get the code to find the
temperature values and process it. Does anyone have any
suggestions?

Public Sub macro4()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1-4} °F")
.Forward = True
.Wrap = wdFindContinue
Do While .Execute
oRg.Text = ((5 / 9) * ((Left(oRg.Text, Len(oRg.Text) - 3)) -
32)) oRg.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub

This code works if I have it find a specifc value, but not if I use
the [0-9]{1-4} values. Also, my output does not put it in the
format I specified above, I just want to get it to work first.
Also, I would like the output to be no more than one decimal place.

Thanks for the help
 
G

gregg098

OK everything works great, I ran into one more potential problem though.
Some of the documents I use have had all hyphens replaced with non-breaking
hyphens (just a replace all). The code does not recognize non-breaking
hyphens as negatives. Right now Im using this:

..Text = ("[-0-9.]{1,} °F")

Is there a way to make this detect non-breaking hypens? I tried using the
the code ^~, but that didnt work in there.

Thanks
Jay Freedman said:
I thought that might be a problem. :)

Change the .Text assignment to

.Text = ("[0-9.]{1,} °F")

This does two things: Placing the period inside the brackets lets the search
match any sequence of consecutive digits and decimal points. Changing the
repetition expression to {1,} allows it to match as many of those characters
as possible instead of limiting it to a maximum of 4.

Although this search could match a malformed text like 123.456.789 °F, the
CSng function would throw an error and refuse to convert it to a number; the
'If Err.Number' test would then skip over the rest of the processing for
that match and go looking for the next one.

--
Jay
Actually, I just realized something. How would I look for the
possibility of a decimal place? For example, 56.3 °F. This macro
works great, but if theres a decimal, it only converts the numbers
after it. The document I wish to use this on contain various values,
some with decimals, some without.

Any further help is appreciated.

gregg098 said:
Works perfectly. Thanks for the help.

:

Hi Greg,

The problem you're having is that the syntax of the wildcard
expression for repetitions of a character requires a comma, not the
dash you were using. If you tried that expression in the Find
dialog in the user interface, it would say "The Find What text
contains a Pattern Match expression which is not valid."

Here's the rest of the macro to compute and format the result.
Notice that you need to use the # mark to make the constants in the
equation floating-point instead of integer; otherwise the computed
value would always be zero (because 5/9 in integer arithmetic is
zero).

Public Sub macro4A()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText As String, sVal As Single

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1,4} °F")
.Forward = True
.Wrap = wdFindStop
Do While .Execute
sText = Left(oRg.Text, Len(oRg.Text) - 3)
On Error Resume Next
sVal = CSng(sText)
If Err.Number = 0 Then
sVal = (5# / 9#) * (sVal - 32#)
sText = Format(sVal, "0.0")
oRg.InsertAfter " (" & sText & " °C)"
oRg.Collapse Direction:=wdCollapseEnd
End If
Loop
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

gregg098 wrote:
I am trying to write a macro which searches through a document for
a temperature value such as 395 °F and will convert it to celcius
and display both values in the format 395 °F (xxx °C). I have
tried all sorts of things and cannot get the code to find the
temperature values and process it. Does anyone have any
suggestions?

Public Sub macro4()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1-4} °F")
.Forward = True
.Wrap = wdFindContinue
Do While .Execute
oRg.Text = ((5 / 9) * ((Left(oRg.Text, Len(oRg.Text) - 3)) -
32)) oRg.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub

This code works if I have it find a specifc value, but not if I use
the [0-9]{1-4} values. Also, my output does not put it in the
format I specified above, I just want to get it to work first.
Also, I would like the output to be no more than one decimal place.

Thanks for the help
 
J

Jay Freedman

Hi Greg,

This ought to do it:

..Text = ("[0-9.^30]{1,} °F")
OK everything works great, I ran into one more potential problem
though. Some of the documents I use have had all hyphens replaced
with non-breaking hyphens (just a replace all). The code does not
recognize non-breaking hyphens as negatives. Right now Im using this:

.Text = ("[-0-9.]{1,} °F")

Is there a way to make this detect non-breaking hypens? I tried using
the the code ^~, but that didnt work in there.

Thanks
Jay Freedman said:
I thought that might be a problem. :)

Change the .Text assignment to

.Text = ("[0-9.]{1,} °F")

This does two things: Placing the period inside the brackets lets
the search match any sequence of consecutive digits and decimal
points. Changing the repetition expression to {1,} allows it to
match as many of those characters as possible instead of limiting it
to a maximum of 4.

Although this search could match a malformed text like 123.456.789
°F, the CSng function would throw an error and refuse to convert it
to a number; the 'If Err.Number' test would then skip over the rest
of the processing for that match and go looking for the next one.

--
Jay
Actually, I just realized something. How would I look for the
possibility of a decimal place? For example, 56.3 °F. This macro
works great, but if theres a decimal, it only converts the numbers
after it. The document I wish to use this on contain various
values, some with decimals, some without.

Any further help is appreciated.

:

Works perfectly. Thanks for the help.

:

Hi Greg,

The problem you're having is that the syntax of the wildcard
expression for repetitions of a character requires a comma, not
the dash you were using. If you tried that expression in the Find
dialog in the user interface, it would say "The Find What text
contains a Pattern Match expression which is not valid."

Here's the rest of the macro to compute and format the result.
Notice that you need to use the # mark to make the constants in
the equation floating-point instead of integer; otherwise the
computed value would always be zero (because 5/9 in integer
arithmetic is zero).

Public Sub macro4A()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText As String, sVal As Single

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1,4} °F")
.Forward = True
.Wrap = wdFindStop
Do While .Execute
sText = Left(oRg.Text, Len(oRg.Text) - 3)
On Error Resume Next
sVal = CSng(sText)
If Err.Number = 0 Then
sVal = (5# / 9#) * (sVal - 32#)
sText = Format(sVal, "0.0")
oRg.InsertAfter " (" & sText & " °C)"
oRg.Collapse Direction:=wdCollapseEnd
End If
Loop
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

gregg098 wrote:
I am trying to write a macro which searches through a document
for a temperature value such as 395 °F and will convert it to
celcius and display both values in the format 395 °F (xxx °C).
I have tried all sorts of things and cannot get the code to find
the temperature values and process it. Does anyone have any
suggestions?

Public Sub macro4()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1-4} °F")
.Forward = True
.Wrap = wdFindContinue
Do While .Execute
oRg.Text = ((5 / 9) * ((Left(oRg.Text, Len(oRg.Text) -
3)) - 32)) oRg.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub

This code works if I have it find a specifc value, but not if I
use the [0-9]{1-4} values. Also, my output does not put it in the
format I specified above, I just want to get it to work first.
Also, I would like the output to be no more than one decimal
place.

Thanks for the help
 
J

Jay Freedman

On second thought, although that expression will find a value with a
non-breaking hyphen playing the part of a minus sign, the CSng
function won't recognize the hyphen and will trigger the On Error
trap. The macro now has to be modified to internally replace the
non-breaking hyphen with a regular hyphen, do the calculation, and
then restore the non-breaking hyphen in the result if needed:

Public Sub macro4B()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText As String, sVal As Single

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9.^30]{1,4} °F")
.Forward = True
.Wrap = wdFindStop
Do While .Execute
sText = Left(oRg.Text, Len(oRg.Text) - 3)
If Left(sText, 1) = Chr(30) Then
sText = "-" & Right(sText, Len(sText) - 1)
End If
On Error Resume Next
sVal = CSng(sText)
If Err.Number = 0 Then
sVal = (5# / 9#) * (sVal - 32#)
sText = Format(sVal, "0.0")
If Left(sText, 1) = "-" Then
sText = Chr(30) & Right(sText, Len(sText) - 1)
End If
oRg.InsertAfter " (" & sText & " °C)"
oRg.Collapse Direction:=wdCollapseEnd
End If
Loop
End With
End Sub


Hi Greg,

This ought to do it:

.Text = ("[0-9.^30]{1,} °F")
OK everything works great, I ran into one more potential problem
though. Some of the documents I use have had all hyphens replaced
with non-breaking hyphens (just a replace all). The code does not
recognize non-breaking hyphens as negatives. Right now Im using this:

.Text = ("[-0-9.]{1,} °F")

Is there a way to make this detect non-breaking hypens? I tried using
the the code ^~, but that didnt work in there.

Thanks
Jay Freedman said:
I thought that might be a problem. :)

Change the .Text assignment to

.Text = ("[0-9.]{1,} °F")

This does two things: Placing the period inside the brackets lets
the search match any sequence of consecutive digits and decimal
points. Changing the repetition expression to {1,} allows it to
match as many of those characters as possible instead of limiting it
to a maximum of 4.

Although this search could match a malformed text like 123.456.789
°F, the CSng function would throw an error and refuse to convert it
to a number; the 'If Err.Number' test would then skip over the rest
of the processing for that match and go looking for the next one.

--
Jay

gregg098 wrote:
Actually, I just realized something. How would I look for the
possibility of a decimal place? For example, 56.3 °F. This macro
works great, but if theres a decimal, it only converts the numbers
after it. The document I wish to use this on contain various
values, some with decimals, some without.

Any further help is appreciated.

:

Works perfectly. Thanks for the help.

:

Hi Greg,

The problem you're having is that the syntax of the wildcard
expression for repetitions of a character requires a comma, not
the dash you were using. If you tried that expression in the Find
dialog in the user interface, it would say "The Find What text
contains a Pattern Match expression which is not valid."

Here's the rest of the macro to compute and format the result.
Notice that you need to use the # mark to make the constants in
the equation floating-point instead of integer; otherwise the
computed value would always be zero (because 5/9 in integer
arithmetic is zero).

Public Sub macro4A()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText As String, sVal As Single

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1,4} °F")
.Forward = True
.Wrap = wdFindStop
Do While .Execute
sText = Left(oRg.Text, Len(oRg.Text) - 3)
On Error Resume Next
sVal = CSng(sText)
If Err.Number = 0 Then
sVal = (5# / 9#) * (sVal - 32#)
sText = Format(sVal, "0.0")
oRg.InsertAfter " (" & sText & " °C)"
oRg.Collapse Direction:=wdCollapseEnd
End If
Loop
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

gregg098 wrote:
I am trying to write a macro which searches through a document
for a temperature value such as 395 °F and will convert it to
celcius and display both values in the format 395 °F (xxx °C).
I have tried all sorts of things and cannot get the code to find
the temperature values and process it. Does anyone have any
suggestions?

Public Sub macro4()
Dim oRg As Range
Set oRg = ActiveDocument.Range
Dim sText

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.MatchWildcards = True
.Text = ("[0-9]{1-4} °F")
.Forward = True
.Wrap = wdFindContinue
Do While .Execute
oRg.Text = ((5 / 9) * ((Left(oRg.Text, Len(oRg.Text) -
3)) - 32)) oRg.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub

This code works if I have it find a specifc value, but not if I
use the [0-9]{1-4} values. Also, my output does not put it in the
format I specified above, I just want to get it to work first.
Also, I would like the output to be no more than one decimal
place.

Thanks for the help
 

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