I am working on a macro to refomat fractions. Part of the process is to
check the character before and the character after the range for a "/"
character. I know that if my range start = the active document range start
then an error will be generated. I am not very proficient or comfortable
with Error Handling. Please have a look at the method I am using to handle
this error and let me know if I have done it the way it should be done.
Thanks.

Note (this is just a part of the larger code so you can see how the error
occurs and how it is handled)

Sub FormatFraction()

Dim OrigFrac As String
Dim Numerator As String, Denominator As String
Dim NewSlashChar As String
Dim SlashPos As Integer
Dim fractionRng As Range

'Type and select 4/5 at the start of a new blank document
Set fractionRng = Selection.Range

NewSlashChar = ChrW(&H2044)
OrigFrac = fractionRng
SlashPos = InStr(OrigFrac, "/")
Numerator = Left(OrigFrac, SlashPos - 1)
Denominator = Right(OrigFrac, Len(OrigFrac) - SlashPos)
'Skip improper fractions or checksum format e.g., 123/9
If Val(Numerator) > Val(Denominator) Then
Exit Sub
End If
'skip date formats e.g., 12/31/1958
With fractionRng
On Error GoTo Handler
'Error is generated when fractionRng.Start = ActiveDocument.Range.Start
If .Characters.First.Previous = "/" Or _
.Characters.Last.Next = "/" Then
Exit Sub
End If
End With
Continue:
fractionRng.Font.Superscript = True
fractionRng = Numerator
fractionRng.Collapse Direction:=wdCollapseEnd
fractionRng = NewSlashChar
fractionRng.Font.Superscript = False
fractionRng.Collapse Direction:=wdCollapseEnd
fractionRng = Denominator
fractionRng.Font.Subscript = True
fractionRng.Collapse Direction:=wdCollapseEnd
fractionRng.Font.Subscript = False
Exit Sub
Handler:
Err.Clear
Resume Continue
End Sub



--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Re: Error Handling by Jezebel

Jezebel
Wed May 04 16:59:09 CDT 2005

Technically it's OK as far as it goes. But what happens if you get some
other error in this code? Perhaps not very likely, but ANY error will jump
into your error-handler. If you wanted to be precise you could do something
like

ExitPoint:
Exit Sub


Handler:
If err.Number = 91 then
resume Continue
end if
msgbox Err.Description
Resume ExitPoint

You don't need the Err.Clear -- The error object is cleared by the Resume
statement. You might also want to introduce some discipline in the line
labels you use. These have to be unique across the project. I prefix them
with the name of the function as a quick way to guarantee that.



In this particular case, the 'error' condition is easy to test for anyway,
so your code would be more readable with normal testing ---

If not (.Characters.First.Previous is nothing or .Characters.Last.Next is
nothing) then
....





"Greg Maxey" <gmaxey@mvps.OscarRomeoGolf> wrote in message
news:uoHccYMUFHA.1384@TK2MSFTNGP09.phx.gbl...
>I am working on a macro to refomat fractions. Part of the process is to
>check the character before and the character after the range for a "/"
>character. I know that if my range start = the active document range start
>then an error will be generated. I am not very proficient or comfortable
>with Error Handling. Please have a look at the method I am using to handle
>this error and let me know if I have done it the way it should be done.
>Thanks.
>
> Note (this is just a part of the larger code so you can see how the error
> occurs and how it is handled)
>
> Sub FormatFraction()
>
> Dim OrigFrac As String
> Dim Numerator As String, Denominator As String
> Dim NewSlashChar As String
> Dim SlashPos As Integer
> Dim fractionRng As Range
>
> 'Type and select 4/5 at the start of a new blank document
> Set fractionRng = Selection.Range
>
> NewSlashChar = ChrW(&H2044)
> OrigFrac = fractionRng
> SlashPos = InStr(OrigFrac, "/")
> Numerator = Left(OrigFrac, SlashPos - 1)
> Denominator = Right(OrigFrac, Len(OrigFrac) - SlashPos)
> 'Skip improper fractions or checksum format e.g., 123/9
> If Val(Numerator) > Val(Denominator) Then
> Exit Sub
> End If
> 'skip date formats e.g., 12/31/1958
> With fractionRng
> On Error GoTo Handler
> 'Error is generated when fractionRng.Start = ActiveDocument.Range.Start
> If .Characters.First.Previous = "/" Or _
> .Characters.Last.Next = "/" Then
> Exit Sub
> End If
> End With
> Continue:
> fractionRng.Font.Superscript = True
> fractionRng = Numerator
> fractionRng.Collapse Direction:=wdCollapseEnd
> fractionRng = NewSlashChar
> fractionRng.Font.Superscript = False
> fractionRng.Collapse Direction:=wdCollapseEnd
> fractionRng = Denominator
> fractionRng.Font.Subscript = True
> fractionRng.Collapse Direction:=wdCollapseEnd
> fractionRng.Font.Subscript = False
> Exit Sub
> Handler:
> Err.Clear
> Resume Continue
> End Sub
>
>
>
> --
> Greg Maxey/Word MVP
> See:
> http://gregmaxey.mvps.org/word_tips.htm
> For some helpful tips using Word.
>



Re: Error Handling by Greg

Greg
Wed May 04 17:36:25 CDT 2005

Jezebel,

Thanks for your help. I sure beats pounding my nose against the keyboard
until it bleeds ;-)

I decided to pull the error handler in favor of normal testing. Final code
posted here for anyone interested:

http://gregmaxey.mvps.org/Formatted_Fractions.htm

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Jezebel wrote:
> Technically it's OK as far as it goes. But what happens if you get
> some other error in this code? Perhaps not very likely, but ANY error
> will jump into your error-handler. If you wanted to be precise you
> could do something like
>
> ExitPoint:
> Exit Sub
>
>
> Handler:
> If err.Number = 91 then
> resume Continue
> end if
> msgbox Err.Description
> Resume ExitPoint
>
> You don't need the Err.Clear -- The error object is cleared by the
> Resume statement. You might also want to introduce some discipline in
> the line labels you use. These have to be unique across the project.
> I prefix them with the name of the function as a quick way to
> guarantee that.
>
>
> In this particular case, the 'error' condition is easy to test for
> anyway, so your code would be more readable with normal testing ---
>
> If not (.Characters.First.Previous is nothing or
> .Characters.Last.Next is nothing) then
> ....
>
>
>
>
>
> "Greg Maxey" <gmaxey@mvps.OscarRomeoGolf> wrote in message
> news:uoHccYMUFHA.1384@TK2MSFTNGP09.phx.gbl...
>> I am working on a macro to refomat fractions. Part of the process
>> is to check the character before and the character after the range
>> for a "/" character. I know that if my range start = the active
>> document range start then an error will be generated. I am not very
>> proficient or comfortable with Error Handling. Please have a look
>> at the method I am using to handle this error and let me know if I
>> have done it the way it should be done. Thanks.
>>
>> Note (this is just a part of the larger code so you can see how the
>> error occurs and how it is handled)
>>
>> Sub FormatFraction()
>>
>> Dim OrigFrac As String
>> Dim Numerator As String, Denominator As String
>> Dim NewSlashChar As String
>> Dim SlashPos As Integer
>> Dim fractionRng As Range
>>
>> 'Type and select 4/5 at the start of a new blank document
>> Set fractionRng = Selection.Range
>>
>> NewSlashChar = ChrW(&H2044)
>> OrigFrac = fractionRng
>> SlashPos = InStr(OrigFrac, "/")
>> Numerator = Left(OrigFrac, SlashPos - 1)
>> Denominator = Right(OrigFrac, Len(OrigFrac) - SlashPos)
>> 'Skip improper fractions or checksum format e.g., 123/9
>> If Val(Numerator) > Val(Denominator) Then
>> Exit Sub
>> End If
>> 'skip date formats e.g., 12/31/1958
>> With fractionRng
>> On Error GoTo Handler
>> 'Error is generated when fractionRng.Start =
>> ActiveDocument.Range.Start If .Characters.First.Previous = "/" Or _
>> .Characters.Last.Next = "/" Then
>> Exit Sub
>> End If
>> End With
>> Continue:
>> fractionRng.Font.Superscript = True
>> fractionRng = Numerator
>> fractionRng.Collapse Direction:=wdCollapseEnd
>> fractionRng = NewSlashChar
>> fractionRng.Font.Superscript = False
>> fractionRng.Collapse Direction:=wdCollapseEnd
>> fractionRng = Denominator
>> fractionRng.Font.Subscript = True
>> fractionRng.Collapse Direction:=wdCollapseEnd
>> fractionRng.Font.Subscript = False
>> Exit Sub
>> Handler:
>> Err.Clear
>> Resume Continue
>> End Sub
>>
>>
>>
>> --
>> Greg Maxey/Word MVP
>> See:
>> http://gregmaxey.mvps.org/word_tips.htm
>> For some helpful tips using Word.



Re: Error Handling by Jezebel

Jezebel
Wed May 04 17:52:04 CDT 2005

And you speak from experience? .... now try it until your NOSE bleeds ....


"Greg Maxey" <gmaxey@mvps.OscarRomeoGolf> wrote in message
news:%23vDnomPUFHA.1384@TK2MSFTNGP09.phx.gbl...
> Jezebel,
>
> Thanks for your help. I sure beats pounding my nose against the keyboard
> until it bleeds ;-)
>
> I decided to pull the error handler in favor of normal testing. Final
> code posted here for anyone interested:
>
> http://gregmaxey.mvps.org/Formatted_Fractions.htm
>
> --
> Greg Maxey/Word MVP
> See:
> http://gregmaxey.mvps.org/word_tips.htm
> For some helpful tips using Word.
>
> Jezebel wrote:
>> Technically it's OK as far as it goes. But what happens if you get
>> some other error in this code? Perhaps not very likely, but ANY error
>> will jump into your error-handler. If you wanted to be precise you
>> could do something like
>>
>> ExitPoint:
>> Exit Sub
>>
>>
>> Handler:
>> If err.Number = 91 then
>> resume Continue
>> end if
>> msgbox Err.Description
>> Resume ExitPoint
>>
>> You don't need the Err.Clear -- The error object is cleared by the
>> Resume statement. You might also want to introduce some discipline in
>> the line labels you use. These have to be unique across the project.
>> I prefix them with the name of the function as a quick way to
>> guarantee that.
>>
>>
>> In this particular case, the 'error' condition is easy to test for
>> anyway, so your code would be more readable with normal testing ---
>>
>> If not (.Characters.First.Previous is nothing or
>> .Characters.Last.Next is nothing) then
>> ....
>>
>>
>>
>>
>>
>> "Greg Maxey" <gmaxey@mvps.OscarRomeoGolf> wrote in message
>> news:uoHccYMUFHA.1384@TK2MSFTNGP09.phx.gbl...
>>> I am working on a macro to refomat fractions. Part of the process
>>> is to check the character before and the character after the range
>>> for a "/" character. I know that if my range start = the active
>>> document range start then an error will be generated. I am not very
>>> proficient or comfortable with Error Handling. Please have a look
>>> at the method I am using to handle this error and let me know if I
>>> have done it the way it should be done. Thanks.
>>>
>>> Note (this is just a part of the larger code so you can see how the
>>> error occurs and how it is handled)
>>>
>>> Sub FormatFraction()
>>>
>>> Dim OrigFrac As String
>>> Dim Numerator As String, Denominator As String
>>> Dim NewSlashChar As String
>>> Dim SlashPos As Integer
>>> Dim fractionRng As Range
>>>
>>> 'Type and select 4/5 at the start of a new blank document
>>> Set fractionRng = Selection.Range
>>>
>>> NewSlashChar = ChrW(&H2044)
>>> OrigFrac = fractionRng
>>> SlashPos = InStr(OrigFrac, "/")
>>> Numerator = Left(OrigFrac, SlashPos - 1)
>>> Denominator = Right(OrigFrac, Len(OrigFrac) - SlashPos)
>>> 'Skip improper fractions or checksum format e.g., 123/9
>>> If Val(Numerator) > Val(Denominator) Then
>>> Exit Sub
>>> End If
>>> 'skip date formats e.g., 12/31/1958
>>> With fractionRng
>>> On Error GoTo Handler
>>> 'Error is generated when fractionRng.Start =
>>> ActiveDocument.Range.Start If .Characters.First.Previous = "/" Or _
>>> .Characters.Last.Next = "/" Then
>>> Exit Sub
>>> End If
>>> End With
>>> Continue:
>>> fractionRng.Font.Superscript = True
>>> fractionRng = Numerator
>>> fractionRng.Collapse Direction:=wdCollapseEnd
>>> fractionRng = NewSlashChar
>>> fractionRng.Font.Superscript = False
>>> fractionRng.Collapse Direction:=wdCollapseEnd
>>> fractionRng = Denominator
>>> fractionRng.Font.Subscript = True
>>> fractionRng.Collapse Direction:=wdCollapseEnd
>>> fractionRng.Font.Subscript = False
>>> Exit Sub
>>> Handler:
>>> Err.Clear
>>> Resume Continue
>>> End Sub
>>>
>>>
>>>
>>> --
>>> Greg Maxey/Word MVP
>>> See:
>>> http://gregmaxey.mvps.org/word_tips.htm
>>> For some helpful tips using Word.
>
>



Re: Error Handling by Greg

Greg
Thu May 05 05:50:46 CDT 2005

Jezebel,

I am not sure that I understand your question or suggestion.

I meant to say, "Thanks for your help. It sure beats pounding my nose
against the keyboard until it bleeds." The "Thanks" is genuine, the
second part is a reference, made lightly, to a tip that you offered
another user a while ago.


Re: Error Handling by Jezebel

Jezebel
Thu May 05 08:14:29 CDT 2005

The response was equally light-hearted. I was just playing on the ambiguity
in the original -- most people take 'it' to refer to the nose, but
syntactically it could equally be the keyboard that bleeds. Happy to help,
anyway.



"Greg" <gmaxey@mvps.org> wrote in message
news:1115290246.063801.32350@f14g2000cwb.googlegroups.com...
> Jezebel,
>
> I am not sure that I understand your question or suggestion.
>
> I meant to say, "Thanks for your help. It sure beats pounding my nose
> against the keyboard until it bleeds." The "Thanks" is genuine, the
> second part is a reference, made lightly, to a tip that you offered
> another user a while ago.
>



Re: Error Handling by Greg

Greg
Thu May 05 08:26:31 CDT 2005

Jezebel,

Ok. Just wanted to be sure that you weren't hoping I would give myself a
bloody nose over my politics ;-).




--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Jezebel wrote:
> The response was equally light-hearted. I was just playing on the
> ambiguity in the original -- most people take 'it' to refer to the
> nose, but syntactically it could equally be the keyboard that bleeds.
> Happy to help, anyway.
>
>
>
> "Greg" <gmaxey@mvps.org> wrote in message
> news:1115290246.063801.32350@f14g2000cwb.googlegroups.com...
>> Jezebel,
>>
>> I am not sure that I understand your question or suggestion.
>>
>> I meant to say, "Thanks for your help. It sure beats pounding my nose
>> against the keyboard until it bleeds." The "Thanks" is genuine, the
>> second part is a reference, made lightly, to a tip that you offered
>> another user a while ago.



Re: Error Handling by Jezebel

Jezebel
Thu May 05 23:09:05 CDT 2005

Ah no. History has done that already. :)



"Greg Maxey" <gmaxey@mvps.OscarRomeoGolf> wrote in message
news:%23SkV%23XXUFHA.2664@TK2MSFTNGP15.phx.gbl...
> Jezebel,
>
> Ok. Just wanted to be sure that you weren't hoping I would give myself a
> bloody nose over my politics ;-).
>
>
>
>
> --
> Greg Maxey/Word MVP
> See:
> http://gregmaxey.mvps.org/word_tips.htm
> For some helpful tips using Word.
>
> Jezebel wrote:
>> The response was equally light-hearted. I was just playing on the
>> ambiguity in the original -- most people take 'it' to refer to the
>> nose, but syntactically it could equally be the keyboard that bleeds.
>> Happy to help, anyway.
>>
>>
>>
>> "Greg" <gmaxey@mvps.org> wrote in message
>> news:1115290246.063801.32350@f14g2000cwb.googlegroups.com...
>>> Jezebel,
>>>
>>> I am not sure that I understand your question or suggestion.
>>>
>>> I meant to say, "Thanks for your help. It sure beats pounding my nose
>>> against the keyboard until it bleeds." The "Thanks" is genuine, the
>>> second part is a reference, made lightly, to a tip that you offered
>>> another user a while ago.
>
>