Run-time Error '4608' Value Out of Range

T

Thomas M

Word 2000

This has been driving my nuts for the last couple of days. I have the
code below in a set of fairly extensive macros. This code has been
working flawlessly for months. Recently, I've been working on adding
some error trapping to the project, but I haven't touched this particular
subroutine. Now when I run this code it sometimes gives me the following
error:

Run-time Error: '4608'
Value Out of Range

Here's the code (I've marked the line that causes the error with ">>>").

*****************
Sub PasteLocate()

' 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
If Override Then BookmarkName = "Override" & BookmarkName
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) <<<
BookmarkName = RecordType & RecordNumber
If Override Then BookmarkName = "Override" & BookmarkName
ActiveDocument.Bookmarks.Add Name:=BookmarkName,
Range:=RecordNumberRange

' 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 + EndPos
Set RecordNumberRange = ActiveDocument.Range(Start:=RangeStart,
End:=RangeEnd)
BookmarkName = "MRI" & cbMRI
If Override Then BookmarkName = "Override" & BookmarkName
ActiveDocument.Bookmarks.Add Name:=BookmarkName,
Range:=RecordNumberRange

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

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

I've checked the values of RangeStart and RangeEnd, and they are well
within the block of text being processed (i.e. Neither value is beyond
the end of the document or anything like that). I also tried putting
RecordNumberRange in the declarations, but that didn't make any
difference one way or the other. Curiously, the second Set
RecordNumberRange command has never caused an error.

The bizzare thing is that the error doesn't happen all the time. For the
last two days, I've been getting the error about 90% of the time. But
just now I completed a test that ran this subroutine more than 50 times,
and I didn't get a single error. The thing is, I didn't change anything!
The code that just ran 50+ times without an error is the SAME code that
was previously causing an error 90% of the time. There were literally NO
changes.

Now, I could just trap this error with On Error Resume Next, but I would
prefer to know what's causing of the error. One thing that I am
wondering about is the document itself. I've been using the document for
months--adding and deleting large section of text, creating the deleting
bookmarks, running my code and then undoing everything, etc. I'm
wondering if the document is becoming corrupt, and maybe the code needs
to be copied to a fresh document. Is that possbile?

Another thing that I'm wondering about is the fact that I've added more
modules. Initially, all the code was contained in the ThisDocument
module. With the addition of the error trapping features I've been
developing, I've added 2 more modules--one that declares a couple of
public variables, and one that contains and Option Explicit statement and
declares a function for passing information from the Err object to a
button on a custom form. Could it be that something about one of those
modules is causing the problem?

Any ideas?

--Tom
 
M

Malcolm Smith

This has been driving my nuts for the last couple of days.

Tom,

In this case we had better help you before you start speaking falsetto.

One question, how have you defined these variables; have you got 'Option
Explicit' set at the top of your module?

I would recommend that you declare your variables first and then come back
to us so that we can work out what's going wrong. Also it's good
programming practice to do this anyway.

- Malc
 
T

Thomas M

Tom,

In this case we had better help you before you start speaking falsetto.

One question, how have you defined these variables; have you got 'Option
Explicit' set at the top of your module?

I would recommend that you declare your variables first and then come back
to us so that we can work out what's going wrong. Also it's good
programming practice to do this anyway.

- Malc

Here are the delcarations from all my modules. I'm not currently using
Option Explicit in all modules, but in reading about it in Help, I can
see where it would be a good practice to use it.

ThisDocument
------------
Option Base 1

' Remove these variables when testing is done.
Dim TestDataLength As Long
Dim ErrDescription As String
Dim ErrHelpContext As Long
Dim ErrHelpFile As String
Dim ErrNumber As Long
Dim ErrSource As String

Dim BestAvailablePrinter As String
Dim BookmarkCollection() As String
Dim BookmarkName As String
Dim BottomOfDocument As Boolean
Dim Caller As String
Dim cbData As String
Dim cbMRI As String
Dim cbValue As DataObject
Dim Counter As Long
Dim ErrDateTime As Date
Dim FirstMRI As String
Dim HaltReason As String
Dim IndexJump As Long
Dim LastBookmarkStart As Long
Dim LocatesCollection As New Collection
Dim MessageMarker As String
Dim MessageReason As String
Dim MINLength As Long
Dim MINTag As String
Dim ModuleName As String
Dim MRITag As String
Dim MTLocateTag As String
Dim NCICClearedTag As String
Dim NCICLocateTag As String
Dim NICLength As Long
Dim NICTag As String
Dim oBookmark
Dim RecordBookmarks As Long
Dim RecordExclusionsCollection As New Collection
Dim RecordNumber As String
Dim RecordNumberLength As Long
Dim RecordNumberRange As Range
Dim RecordStart As Long
Dim RecordType As String
Dim RemovedFromDocument As Boolean
Dim SaveFileFlag As Boolean
Dim StartPos As Long
Dim Subroutine As String


HTMLHelp
--------
Option Explicit

'HTML Help declarations
Declare Function HTMLHelp Lib "HHCtrl.ocx" Alias "HtmlHelpA" _
(ByVal hwndCaller As Long, _
ByVal pszFile As String, _
ByVal uCommand As Long, _
ByVal dwData As Long) As Long

Const HH_DISPLAY_TOPIC = &H0
Const HH_HELP_CONTEXT = &HF


PublicDeclarations
------------------
Public AddToLocatesCollection As Boolean
Public cbDataClip As String
Public ErrMsg As String
Public Override As Boolean


HTMLHelp and PublicDeclarations are separate modules. I don't know if it
makes any difference that they are in separate modules.

The project also has two user forms, but nothing is declared in those.
The variables that are declared publicly are passed to--or from--those
user forms. One of the forms has a button that will pull up the Help
file on the specific error, hence the function declared in the HTMLHelp
module.

The only variables in the routine that were not being declared were
RangeStart, RangeEnd, and EndPos. I declared those variables at the top
of the routine, and I still get the error when I run the code.

Here's the routine again, so you can see it with the above declarations.

*****************
Sub PasteLocate()

Dim EndPos As Long
Dim RangeEnd As Long
Dim RangeStart As Long

' 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
If Override Then BookmarkName = "Override" & BookmarkName
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 + RecordNumberLength
Set RecordNumberRange = ActiveDocument.Range(Start:=RangeStart,
End:=RangeEnd) ' Error on this line.
BookmarkName = RecordType & RecordNumber
If Override Then BookmarkName = "Override" & BookmarkName
ActiveDocument.Bookmarks.Add Name:=BookmarkName,
Range:=RecordNumberRange

' 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 + EndPos
Set RecordNumberRange = ActiveDocument.Range(Start:=RangeStart,
End:=RangeEnd)
BookmarkName = "MRI" & cbMRI
If Override Then BookmarkName = "Override" & BookmarkName
ActiveDocument.Bookmarks.Add Name:=BookmarkName,
Range:=RecordNumberRange

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

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

--Tom
 
M

Malcolm Smith

Tom

You are going to have to put some of those variables into the routine
rather than define them at a modular level. It's a pain trying to work
out what type, for example, RecordNumberLength is; though we can guess
it's a long, but it's not a good idea to guess that.

Where is this value set? Why is it not set in the routine; it could be a
massively negative number by the time that it gets to this part of the
code.

Suggestions:

1. Make everything Option Explicit. It is more than a good idea; it
should be compulsory and there is not one good reason to work without it.

2. Put all of the variables which are used locally into this routine.
This could be half of the problem. Without this it's going to be
impossible to debug.

- 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