"Value out of range" COM exception

J

Jacky

I got this COM exception when running this code(in C#)

object startPos = (object)start;
object endPos = (object)end;
Word._Document doc = .... // all these have been initialized
doc.Range( ref start, ref end );

the last line gives me "Value out of range" exception if the endPos is
greater than doc.Content.End

How should i expand the document content to accommodate that?
Thanks in advance!
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?SmFja3k=?=,
I got this COM exception when running this code(in C#)

object startPos = (object)start;
object endPos = (object)end;
Word._Document doc = .... // all these have been initialized
doc.Range( ref start, ref end );

the last line gives me "Value out of range" exception if the endPos is
greater than doc.Content.End

How should i expand the document content to accommodate that?
Well, the error certainly makes sense. You can't extend the range beyond
the characters available in a document. Can you explain what you're
trying to accomplish by doing so?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
H

Helmut Weber

Hi Cindy
Well, the error certainly makes sense. You can't extend the range beyond
the characters available in a document. Can you explain what you're
trying to accomplish by doing so?

yes, but I don't get an error:

Dim rDcm As Word.Range
Set rDcm = ActiveDocument.Range
MsgBox rDcm.End
rDcm.End = rDcm.End + 500
MsgBox rDcm.End ' unchanged

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jacky

Thanks Cindy, Helmut!

The way Helmut mentioned by extending the End after initializing a Range
object works! Thanks Helmut!

so the code looks like

Word.Range range = doc.Range( start, smallEnd );
range.End = bigEnd;

this will work

but doing it directly
Word.Range range = doc.Range( start, bigEnd );
will give exception.

Cindy:
The reason for me to do this is that
I have a section of text in a WORD template that I need to copy and paste
multiple times, and the number of times to paste is unknown until runtime.
To do that, i first define the range to paste then copy from the original
range then paste, and the new range might exceed the edge of the document.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?SmFja3k=?=,
The reason for me to do this is that
I have a section of text in a WORD template that I need to copy and paste
multiple times, and the number of times to paste is unknown until runtime.
To do that, i first define the range to paste then copy from the original
range then paste, and the new range might exceed the edge of the document.
Hmmm. Do you have a particular reason for copy/paste rather than:

rngTarget.FormattedText = rngSource.FormattedText

Generally, I try to avoid the Clipboard if at all possible.

Also, you shouldn't need to select a range of characters in order to paste?
Why not just paste at the end of the document?

range.Collapse wdCollapseEnd 'VBA-speak, can't remember the exact syntax
for C#
range.Paste

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
J

Jacky

Yeah, I also found a way not having to specify the large range
i just have to define the range at the starting point and insert the text at
that point
something like
Word.Range rng = doc.Range( ref strat, ref start );

Thanks cindy :)
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?SmFja3k=?=,
Yeah, I also found a way not having to specify the large range
i just have to define the range at the starting point and insert the text at
that point
something like
Word.Range rng = doc.Range( ref strat, ref start );
One small tip (since none of us likes to type more than necessary):

Generally, I prefer to set the range for a document like this
Word.Range rng = doc.Content

If I need the end of the document, then I collapse the range to it's end-point
(or to the start point, if I want to add something at the beginning of the
file)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
Top