G
Greg
Hello,
Jezebel and Max Moor introduced me to a new use of the Split function
today. I was experimenting with it to create a bit of code to
rearrange text.
I have the sample text:
James Miller
Margaret E. Smith
John R. Richland Sr
which I want to format as
Miller, James
Smith, Margarete E.
Richland, John R., III
I was able to do this easy enough one line at a time using selection.
However when I graduated to a For Each oPara approach my On Error
statements start acting up. If I run the following code, the first
line is properly formatted then I get a run-time error 9 "subscript out
of range" when the code attempts to format the second line. I expected
this error, that is why I used the On Error Statement.
I know that a similiar error is being generated when the first line is
processed. Apparently I am not properly instructing the routine to
clear or reset the error handler. I thought that was the purpose of
the the line Err.Clear, but appartently I am wrong.
Thanks.
Sub Test
Dim oPara As Word.Paragraph
Dim oRng As Word.Range
Dim pRef1 As String
Dim pRef2 As String
Dim pRef3 As String
Dim pRef4 As String
For Each oPara In ActiveDocument.Paragraphs
Set oRng = oPara.Range
oRng.MoveEnd wdCharacter, -1
pRef1 = Split(oRng)(0)
pRef2 = Split(oRng)(1)
On Error GoTo Construct1
pRef3 = Split(oRng)(2)
On Error GoTo Construct2
pRef4 = Split(oRng)(3)
GoTo Construct3
Construct1:
Err.Clear
oRng.Text = pRef2 & ", " & pRef1
GoTo Finished
Construct2:
Err.Clear
oRng.Text = pRef3 & ", " & pRef1 & " " & pRef2
GoTo Finished
Construct3:
If InStr("SRSrJRJrIIIV", pRef4) > 0 Then
Err.Clear
oRng.Text = pRef3 & ", " & pRef1 & " " & pRef2 & ", " & pRef4
GoTo Finished
End If
Finished:
Next
End Sub
Jezebel and Max Moor introduced me to a new use of the Split function
today. I was experimenting with it to create a bit of code to
rearrange text.
I have the sample text:
James Miller
Margaret E. Smith
John R. Richland Sr
which I want to format as
Miller, James
Smith, Margarete E.
Richland, John R., III
I was able to do this easy enough one line at a time using selection.
However when I graduated to a For Each oPara approach my On Error
statements start acting up. If I run the following code, the first
line is properly formatted then I get a run-time error 9 "subscript out
of range" when the code attempts to format the second line. I expected
this error, that is why I used the On Error Statement.
I know that a similiar error is being generated when the first line is
processed. Apparently I am not properly instructing the routine to
clear or reset the error handler. I thought that was the purpose of
the the line Err.Clear, but appartently I am wrong.
Thanks.
Sub Test
Dim oPara As Word.Paragraph
Dim oRng As Word.Range
Dim pRef1 As String
Dim pRef2 As String
Dim pRef3 As String
Dim pRef4 As String
For Each oPara In ActiveDocument.Paragraphs
Set oRng = oPara.Range
oRng.MoveEnd wdCharacter, -1
pRef1 = Split(oRng)(0)
pRef2 = Split(oRng)(1)
On Error GoTo Construct1
pRef3 = Split(oRng)(2)
On Error GoTo Construct2
pRef4 = Split(oRng)(3)
GoTo Construct3
Construct1:
Err.Clear
oRng.Text = pRef2 & ", " & pRef1
GoTo Finished
Construct2:
Err.Clear
oRng.Text = pRef3 & ", " & pRef1 & " " & pRef2
GoTo Finished
Construct3:
If InStr("SRSrJRJrIIIV", pRef4) > 0 Then
Err.Clear
oRng.Text = pRef3 & ", " & pRef1 & " " & pRef2 & ", " & pRef4
GoTo Finished
End If
Finished:
Next
End Sub