problem in changing part of a sentence

M

mjlaali

I am developing a word automation program. In one of my methods I iterate on
sentences of a document, get the range of each sentence and in order to
select a part of the sentence, I call “MoveStart†and “MoveEnd†on the Range
object, and then I call “Select†on it to select it. But the problem is,
calling Application.GetSelection() after all of these, returns a selection
which has selected the sentence completely, and not the Range resulted after
calling “MoveStart†and “MoveEndâ€.

Another problem is if I try to change the text of the Range object, after
calling “MoveStart†and “MoveEnd†on it, using “SetText†method of the Range
results in an exception which tells “the range cannot be deletedâ€.

currentSentenceRange.MoveStart(COleVariant((short)wdCharacter),COleVariant((long)foundStartPoint)
currentSentenceRange.MoveEnd(COleVariant((short)wdCharacter),COleVariant((long)lengthDifference);

//currentSentenceRange.SetText(replacementCStr); this line causes exception

currentSentenceRange.Select();
Selection sentenceSelection = m_wordApplication.GetSelection();//the
resulted //selection has selected the sentence completely

CString selectionText = sentenceSelection.GetText();
sentenceSelection.SetText(replacementCStr);

Independent of the questions above, the whole thing I want is a method to
select and change part of a sentence. I have no problem with changing the
whole text of a sentence
 
P

Pesach Shelnitz

Hi,

The problem may be in the values of the second argument that you are
supplying in your calls to MoveStart and MoveEnd. If you are trying to
replace a part of the sentence, the number that you supply in the second
argument for MoveStart should be a positive number that is smaller than total
length of the sentence, and the number that you supply in the second argument
for MoveEnd should be a *negative* number which is such that the sum of its
absolute value and the number supplied in the second argument for MoveStart
does not exceed the total length of the sentence. If you supply a positive
number in the call to MoveEnd, the Range will extend beyond the original
sentence and may include a character that cannot be deleted.
 
M

mjlaali

Hi,
Thank you for your reply, but …
The problem is not such as what you guessed. I have checked the arguments
for validity, before calling “MoveStart†and “MoveEndâ€. The Range object
resulted from “MoveStart†and “MoveEnd†calls is a valid Range which is a
part of the sentence and is not extended beyond the range of the original
sentence. But when I call "Select" on it the resulted selection will be the
complete sentence.
 
P

Pesach Shelnitz

Hi,

You originally wrote that you are seeing an exception in your call to
SetText. What is the error number or exact description of the exception?
 
M

mjlaali

Hi,

As I said the exception occurs only if I use “SetText†method of the Range
object itself, and its message is “the range cannot be deletedâ€. If you look
to the code included in my first message you can see the line of code as:
//currentSentenceRange.SetText(replacementCStr); this line causes //exception

If I uncomment it, it will cause the exception to occur.
As I said before, the “currentSentenceRange†variable includes part of a
sentence Range after calling “MoveStart†and “MoveEndâ€, but if I call method
“Select†on it the resulting Selection object will include the complete
sentence Range.
 

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