简体   繁体   中英

OLE automation: How to check if a variant references an automation object

I would like to know how can i determine, whether a variant is referencing an OLE automation object, or not.

I'm exporting some Excel graphs to Powerpoint.

I have this code:

var PptFile: Variant;

....

// PptFile _might_ be initialized:

PptFile:=pptApp.Presentations.Open(pptFilename);

// It depends on whether the export has items which need to be exported to 
// Powerpoint or not

....

// I would like to determine if PptFile does reference an OLE automated object or not
PptFile.SaveAs(excelFileName+'.pptx');

I know, it could be done by placing the last line of the code (with saveAs) between try...except...end , but i don't feel that approach is good enough.

I was reading about VarIsEmpty , VarIsEmptyParam , Nothing , this question , but i'm not sure about this.

You should use VarIsClear for this test.

Indicates whether the specified variant has an undefined value. VarIsClear returns true if the given variant's value is undefined. The value can be undefined for any of several reasons:

  • The Variant may have had its value set to Unassigned.
  • The Variant's value may be an interface type that has been set to nil (Delphi) or NULL (C++).
  • The Variant may be a custom variant that returns true from its IsClear method.

In all other cases, the function result is false.

Note : Do not confuse an unassigned variant with a Null variant. A Null variant is still assigned, but has the value Null. Unlike unassigned variants, Null variants can be used in expressions and can be converted to other types of variants.


However, I question whether or not it is needed. How could it be that PptFile was not assigned? That can only happen if the call to pptApp.Presentations.Open() fails, and that would raise an exception. Or am I mis-understanding this? I cannot at the present see any scenario in which you could reach the call to PptFile.SaveAs() for which PptFile had not been assigned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM