problem in changing the text of sentences before tables

M

mjlaali

I am developing a word automation application. In a method of mine, I change
the text of some sentences of an opened word file, but the problem is when I
change the text of a sentence which located before a table, it will be moved
to the first cell of the table. My code is as follow:

void myMethod( long startingSentenceNumber, const char *toBeSearched, const
char *replacement, bool replace )
{
Range currentSentenceRange;
Selection sentenceSelection;
Sentences sentencesList = m_document.GetSentences();
long sentencesCount = sentencesList.GetCount();
CString replacementCStr(replacement);

for (long newIndex = startingSentenceNumber; newIndex <= sentencesCount;
newIndex++)
{
currentSentenceRange = sentencesList.Item(newIndex);
CString currentSentenceText = currentSentenceRange.GetText();
const char *temp = currentSentenceText;
std::string currentString(temp);
std::string toBeSearchedString(toBeSearched);

if (/*some condition*/)
{
std::string endCharacters = currentString.substr(strlen(toBeSearched),
currentString.size() - strlen(toBeSearched));
replacementCStr += endCharacters.c_str();
if (replace)
{
currentSentenceRange.SetText(replacementCStr);
}
foundedSentenceIndex = newIndex;
break;
}

currentSentenceRange.ReleaseDispatch();
sentenceSelection.ReleaseDispatch();
}
sentencesList.ReleaseDispatch();
}

How could I solve this problem?
 
P

Pesach Shelnitz

Hi,

I can reproduce your problem by placing the cursor in the sentence
immediately before a table and running the following macro, which replaces
the text in a Range object just like your code does.

Sub ChangeSentenceBeforeTable1()

Dim MyText As String
Dim MyRange As Range

Set MyRange = Selection.Sentences(1)
MyText = MyRange.Text + " Added text."
MyRange.Text = MyText

Set MyRange = Nothing
End Sub

The modified sentence ends up in the table because inserting additional text
into the range in this way extends the end of the Range object into the
table. I found that if I place the cursor in the last sentence of a paragraph
immediately before another paragraph, the additional text is also be added to
the beginning of the next paragraph.

I found that I can correctly insert additional text at the end of a sentence
anywhere, including at the end of a sentence before a table and at the end of
the last sentence in a paragraph by placing the cursor in the sentence and
running the following macro.

Sub ChangeSentenceBeforeTable2()

Dim MyText As String
Dim MyRange As Range

Set MyRange = Selection.Sentences(1)
MyText = MyRange.Text + " Added text."
MyRange.Collapse Direction:=wdCollapseEnd
MyRange.MoveStart wdCharacter, -1
MyRange.InsertBefore " Added text."

Set MyRange = Nothing
End Sub

The solution for you is to modify your code so that it collapses the Range
object for the sentence to its end, moves the end back one position (before
the end of the paragraph), and inserts the additional text before the
modified Range object using the InsertBefore method.
 

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