Resource problem with Range.Information

U

Udo Nesshoever

Hi yall!

I'm calling Word as COM-Server from Delphi and from C++. I have to
retrieve the current position pretty frequently and I', using the
function Information of the object Range for that.
As a test I called this function heavily and after approx. 450 calls
Word shows an out-of-resources message box. I dont't know how to free
anything as I didn't see any possibility to do so.

Here are the snippets:
=== C++ ===
void MyClass::GetPositionInRange(
CRange &oRange, int &nRow, int &nColumn)
{
VARIANT vColumn =
oRange.get_Information(wdFirstCharacterColumnNumber);
VARIANT vLine =
oRange.get_Information(wdFirstCharacterLineNumber);

nRow = (int)vLine.lVal;
nColumn = (int)vColumn.lVal;
}
==========

=== Delphi ===
Deklaration:
var
i, j: Integer;
doc: OleVariant;
itm: OleVariant;
r: Range;
[...]
doc:= ExtractFilePath(ParamStr(0)) + 'problem.doc';
wApp.Documents.Open(doc, EmptyParam, ...);
itm:= 1;
wDoc.ConnectTo(wApp.Documents.Item(itm));
for i:= 0 to 30000 do
begin
r:= wDoc.Paragraphs.Item(2).Range;
j:= r.Information[wdFirstCharacterLineNumber];
end;
===========

Any hint is highly appreciated, as I couldn't find anything nor at
Google neither at MSDN :(

Cheers,
Udo
 
H

Howard Kaikow

It would be easier if you posted a VB version, but I'd suggest that you
first clean up the code by starting with:

1. Do not use Variants. The column/line numbers should be stored in a 32 bit
integer.
2. Remove redundant calculations from loops.

For example:

r:= wDoc.Paragraphs.Item(2).Range;
j:= r.Information[wdFirstCharacterLineNumber];

Are constant for all iterations of the loop in the Delphi code.
You likely intended to use

r:= wDoc.Paragraphs.Item(i).Range;
j:= r.Information[wdFirstCharacterLineNumber];

But even that is inefficient.

For example, using VB, you would use something like

Private Sub ParaStuff()
Dim par As Word.Paragraph
Dim j As Long

For Each par In Application.ActiveDocument.Paragraphs
j = par.Range.Information(wdFirstCharacterLineNumber)
Debug.Print j
Next par
End Sub
 
U

Udo Nesshoever

It would be easier if you posted a VB version,

I just tested Delphi and C++. Posting VB code would have been an
assumption, not a fact for the leak.
1. Do not use Variants. The column/line numbers should be stored in a 32 bit
integer.

The function Information returns a variant.
2. Remove redundant calculations from loops.

I also tried moving the range selection before the loop -> no change
in results. Still the same leak. The code I appears to be correct.

Cheers,
Udo
 
U

Udo Nesshoever

Hi!

Just wanted to share the solution:

I have to clear the undo buffer!!
Apparently Word is not smart enough to free its own undo buffer if
resources are running low. Putting a UndoClear() into the loop every
now and then fixed the problem and the test application completed
successfully.

Thanks for your time and thoughts.

Cheers,
Udo
 
Top