Need Help With Run-Time Errors

  • Thread starter Montana DOJ Help Desk
  • Start date
M

Montana DOJ Help Desk

Word 2000

I have the following code (Note the RecordNumber is alphanumeric, so it's
declared as a string).

***************
Option Base 1
Option Explicit

Dim BookmarkName As String
Dim cbMRI As String
Dim EndPos As Long
Dim MRITag As String
Dim RecordNumber As String
Dim RecordType As String
Dim StartPos As Long
Dim Subroutine As String

Sub PasteLocate()

Dim RangeEnd As Long
Dim RangeStart As Long
Dim RecordNumberRange As Range

On Error GoTo ErrorHandler

' Checks for the existance of the required bookmarks and document variables.
CheckRequiredBookmarks

' Pastes the Locate into the document.
Selection.EndKey Unit:=wdStory
Selection.Paste

' Bookmarks the record in the document using the MIN or NIC as part of the
bookmark name.
Selection.GoTo what:=wdGoToBookmark, Name:="StartOfRecord"
Selection.Extend
Selection.EndKey Unit:=wdStory
BookmarkName = "Record" & RecordNumber
ActiveDocument.Bookmarks("\Sel").Copy BookmarkName

' Finds the MIN or NIC in the selected text, creates a range covering that
location, and
' bookmarks the range using the MIN or NIC as part of the bookmark name.
StartPos = InStr(1, Selection.Text, Chr(13) & RecordType & "/",
vbTextCompare) + 4
RangeStart = Selection.Start + StartPos
RangeEnd = RangeStart + RecordNumberLengthEnd:=RangeEnd) ' <<< 4608 Value out of range.
BookmarkName = RecordType & RecordNumberRange:=RecordNumberRange ' <<< 4218 Type mismatch

' Finds the MRI in the selected text, creates a range covering that
location, and
' bookmarks the range using the MRI as part of the bookmark name.
StartPos = InStr(1, Selection.Text, MRITag, vbTextCompare) + 3
RangeStart = Selection.Start + StartPos
EndPos = InStr(StartPos + 1, Selection.Text, " ", vbTextCompare) - 1
RangeEnd = Selection.Start + EndPosEnd:=RangeEnd) ' <<< 4608 Value out of range.
BookmarkName = "MRI" & cbMRIRange:=RecordNumberRange ' <<< 4218 Type mismatch

' Collapses the selection, inserts a paragraph, and redefines the
StartOfRecord bookmark.
Selection.Collapse Direction:=wdCollapseEnd
Selection.TypeParagraph
ActiveDocument.Bookmarks("\StartOfSel").Copy "StartOfRecord"
BottomOfDocument = True

Exit Sub

ErrorHandler:

If PreviousError = False Then
Subroutine = "PasteLocate"
ErrorTrapping
End If
Resume Next

End Sub
***************

The lines marked with ">>>" are causing run-time errors (The specific error
is noted at the end of each line). I figure that all of these errors are
being caused by the same problem, so if I can figure out what's causing the
first 4608 error, all the errors should be solved. I have confirmed that
none of the variables being declared at the module level are getting
re-declared, so by the time this routine is called, the data types are still
as shown above.

Here is a sample of the data.

$L..MT0251111.

$.L.

MT0251111

LOCATE NOTIFICATION AT 1726 EDT 20040827

1N017TT71MRI1078365.LSS.MT025135Y.NIC/S770194575.0CA/TESTJV.20040827.465115

61

LOCATING ORI IS DOJ INFORMATION TECHNOLOGY SERVICES DIVISION



MKE/LOCATED SECURITIES/RANSOM MONEY

ORI/MT0251111 TYP/M0 DEN/BLANK ISS/WESTERN UNI0N

OWN/SMITH,J0HN SDT/BLANK DOT/20040827

OCA/TESTJV 20040827 MT025135Y 46511561

VLD/20040827

MIS/TEST REC0RD

NIC/S770194575 DTE/20040827 1725 EDT

SER/20040827111-20040827119

20040827 MT025135Y 46511561



MRI 1078369 IN: NC21 1329 AT 27AUG2004 15:26:55 OUT: JLET00 423 AT 27AUG200

4 15:26:55


The code should create the following bookmarks:

1) RecordS770194575 Range covering the entire record
2) NICS770194575 Range covering just the NIC number
3) MRI1078369 Range covering the MRI number

Can anyone see what is causing the run-time errors in this routine?

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 
H

Helmut Weber

Ho Montana,
difficult to help here.
Too much of code, which makes sense only to you.
Like :
CheckRequiredBookmarks
Selection.EndKey Unit:=wdStory
Selection.Paste ' what is being pasted
ErrorTrapping ' what is it
Subroutine = "PasteLocate" '?
PreviousError ?
etc.
But it seems, you are trying to assign a range
to RecordNumberRange that is not within the range
of the document.
Sub Test901()
' Length of document is 480 <<< !!!
Dim r As Range
Set r = ActiveDocument.Range(Start:=0, End:=481)
' error 4608 <<<
End Sub
 
M

Malcolm Smith

I'm sure we've had this before.

Why are these variables declared a modular level?
What are the values of the important variables, RangeStart, RangeEnd and
RecordLength when it crashes?

You've asked this before and I have answered the same questions which you
haven't replied to and then you just come back a few weeks later and post
the same thing again.

Can you determine the value of these variables please then we can look
into it a little more?

- Malc
 
M

Montana DOJ Help Desk

See comments in-line below.

"Making the world a safer place."
Helmut Weber said:
Ho Montana,
difficult to help here.
Too much of code, which makes sense only to you.
Like :

Calls a routine to check for the StartOfRecord bookmark, and creates it if
it does not already exist.

The data being pasted to the document is the sample data that I included in
the original message. I didn't think to mention that before.

ErrorTrapping is a routine that writes the error information to a log file
and gives the user the option of keeping or discarding the data that caused
the error. This is accomplished by displaying a dialog box that contains
the data being processed, and provided a "Keep" and a "Discard" button. All
my subroutines have the same basic error handler. PreviousError is just a
variable that identifies if the ErrorTrapping routine has already been run
(if the user has already specified to keep or discard the date, then there
is no need to display the dialog box again). PreviousError gets reset to
"False" at the start of each run (before the posted subroutine is called).
The Subroutine variable is just so that the name of the subroutine where the
error was encountered can be written to the error log.
etc.
But it seems, you are trying to assign a range
to RecordNumberRange that is not within the range
of the document.
Sub Test901()
' Length of document is 480 <<< !!!
Dim r As Range
Set r = ActiveDocument.Range(Start:=0, End:=481)
' error 4608 <<<
End Sub

That would seem to be the case, but I had looked at that possibility before
and the range start and range end values were always within the range of the
document. However, I was just stepping through the code, and I noticed that
the section just after the text is pasted into the document (code below) is
not selecting the block of text, and that is causing RangeStart to calculate
to a value that is beyond the end of the document (which would cause the
4608 error).

Selection.GoTo what:=wdGoToBookmark, Name:="StartOfRecord"
Selection.Extend
Selection.EndKey Unit:=wdStory
BookmarkName = "Record" & RecordNumber
ActiveDocument.Bookmarks("\Sel").Copy BookmarkName

The StartOfRecord bookmark is always one line above the point where the text
is pasted into the document, so by going to that bookmark and extending the
selection down to the end of the document, the block of text that was just
pasted into the document should be selected. When that happens, the code
works.

The weird thing is, I *know* that I checked this before. I remember setting
all the watch values and stepping through the code a line at a time,
checking what was going on in the document with each step. Maybe I just
missed it. Then again, I did recently have a problem with a corrupt module,
which I tried to fix by cleaning the module and moving the code into a new
template, but maybe that's still an issue.

Anyway, it would seem now that I've got something to really look at in an
effort to solve this problem.

--Tom
 
M

Montana DOJ Help Desk

Malcolm,

You're right. I have posted this question before. The reason I did not
reply to your earlier message is that I was TRYING you're recommended
solutions. If you recall, one of your suggestions was to use Option
Explicit and declare every variable. I've now done that. I didn't return
to the newsgroup before now because I just hadn't had the time to try all of
your recommendations until yesterday. I don't work on this project 24x7. I
work a technical support line, and some nights it's quite, and other nights
I'm on the phone for the entire shift, so it's one of those things where I
get to it when I get to it.

I reposted the question because I thought that some people who might be new
to the thread may not be able to see the original message anymore. For
example, *I* can no longer see the original message on my server.

As for why certain values are being declared at the module level, that's
because they are being used in multiple subroutines. Unless there is
something extremely basic about VBA that I have missed, those variables MUST
be declared at the module level in order to pass values from one subroutine
to another. Please let me know if this is not correct.

I did try to post the values of the important variables (RangeStart,
RangeEnd, RecordNumberLength, and total document length) with my message
last night, but as I was writing up that message, I could not get the code
to crash. What can I say? Sometimes it crashes, and sometimes it doesn't.
That's the frustrating thing with this--there doesn't seem to be a common
thread between when it crashes, and when it works. It seems like the error
is generated at random.

I've even wondered if the problems are being causes by a corrupt documnet or
module. I've had another problem with the project code being unviewable,
which tends to be an indication of a corrupt module (but which can be
addressed by closing and reopening the document), so I thought that might be
part of the problem, and I wanted to rule it out before coming back to the
newgroup because if the problem was with a corrupt module, then no amount of
troubleshooting the code would help. In fact, that's another reason that I
haven't gotten back to this issue until now--I've been attacking the problem
from that angle. I researched corrupt modules, cleaned my module, put the
code into a brand new template, etc. But none of that helped, so it looks
like it's a code problem after all.

I posted a reply to Helmut that I think has finally gotten me on the right
track to solving this problem.

-- Tom

State of Montana
Department of Justice Help Desk

"Making the world a safer place."
 
M

Malcolm Smith

As for why certain values are being declared at the module level, that's
because they are being used in multiple subroutines. Unless there is
something extremely basic about VBA that I have missed, those variables
MUST
be declared at the module level in order to pass values from one
subroutine
to another. Please let me know if this is not correct.

Morning!

Unless you HAVE to have module level variables it is best to have your
variables at a more local level and then pass in what you need by various
parameters.

That way you are then more certain of what's going on. If you have the
Scope of a variable set too loosely then you won't know what is changing
the values elsewhere.

The trick is to keep everything tight so that you know what's going on.

In your code you have having Start and End values. What if one is zero,
or one is smaller than the other? This could cause the problem. You
ought to check for this sort of incidence because, I bet, that is what is
happening.

Cheers
Malc
 

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