1

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.

Community
  • 1
  • 1
beerwin
  • 9,813
  • 6
  • 42
  • 57

1 Answers1

6

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • It seems, that my Delphi 5 doesn't have a `VarIsClear` function. – beerwin Nov 29 '13 at 12:07
  • Then you might roll your own. The code reads something like this: `Result := (VType = varEmpty) or (((VType = varDispatch) or (VType = varUnknown)) and (VDispatch = nil))` Now, what about my final paragraph? Do you really need to perform this check? – David Heffernan Nov 29 '13 at 12:11
  • Thank you. I will accept this answer, as it has everything (even more than that) i need. `PptFile` is not assigned when there are no charts to export. I know, it's sloppy coding (not written by me, i just need to add something to it and this issue stays in my way). I might end up with counting the exported charts, and if the count is zero, skip this `PptFile.SaveAs(...)`. Thank you for your effort. – beerwin Nov 29 '13 at 12:18
  • If you just want to detect that you have never assigned a value, use VarIsEmpty. Surely that's in D5. – David Heffernan Nov 29 '13 at 12:27
  • I took VarIsClear from the FPC runtime library, and it works out of the box. Thank you. – beerwin Nov 29 '13 at 12:53