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