Delphi OLE - How to avoid errors like "The requested member of the collection does not exist"?

Posted by Edwin on Stack Overflow See other posts from Stack Overflow or by Edwin
Published on 2010-05-26T14:53:56Z Indexed on 2010/05/26 20:51 UTC
Read the original article Hit count: 380

Filed under:
|

Hi,

I'm automating Word with Delphi, but some times I got an error message:

The requested member of the collection does not exist

It seems that the Item member of the Styles collection class does not always exist and some times causes the above mentioned error. My workaround is to catch the exception and skip it, but is there anyway to detect it instead of using the try...except block? The problem with the try...except block is that when debugging the raised exception is annoying...

My code example:

var
  aWordDoc: _WordDocument
  i: Integer;
  ovI: OleVariant;
  wordStyle: Style;
begin
  for i := 1 to aWordDoc.Styles.Count do
  begin
    ovI := i;
    try
      wordStyle := aWordDoc.Styles.Item(ovI);
    except
      Continue;//skip if any error occurred.
    end;

    //do something with wordStyle
  end;
end

© Stack Overflow or respective owner

Related posts about delphi

Related posts about ole