Recent

Author Topic: OLEVariants problem  (Read 893 times)

daveinhull

  • Full Member
  • ***
  • Posts: 249
  • 1 divided by nothing must still be 1!
OLEVariants problem
« on: February 18, 2019, 08:54:16 am »
Hi,

I have a problem with an OLEVariant (connects to Excel).
When I cloase a particualr form I want to make sure that the Excel object is also closed, so I have used
Code: Pascal  [Select]
  1. If ExcelServer <> Unassigned then
  2. begin
  3.   ExcelServer.ActiveWorkbook.Close (False);
  4.   if ExcelServer.Workbooks.Count = 0 then
  5.     ExcelServer.Quit;
  6.     ExcelServer := Unassigned;
  7.   end
  8. ....

Which is ok if the ExcelServer was never assigned during the program execution, i.e. the user didn't want to do anything with Excel.

However, it fails with an "Invalid Variant Operation Displatch = Empty" message if I actually do setup a link to Excel.
If I change the code
Code: Pascal  [Select]
  1. if ExcelServer <> Null then
it also fails.

Anybody got any thoughts in this?

Dave
« Last Edit: February 19, 2019, 03:38:40 am by daveinhull »
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

rvk

  • Hero Member
  • *****
  • Posts: 3745
Re: OLEVariants problem
« Reply #1 on: February 18, 2019, 12:32:12 pm »
Maybe
Code: Pascal  [Select]
  1. if not VarIsEmpty(ExcelServer) then

Thaddy

  • Hero Member
  • *****
  • Posts: 8179
Re: OLEVariants problem
« Reply #2 on: February 18, 2019, 02:16:08 pm »
I expected VarNull or VarEmpty
Read the manuals and if you are a professional get a proper education in computer science. Makes the forum a lot cleaner.

rvk

  • Hero Member
  • *****
  • Posts: 3745
Re: OLEVariants problem
« Reply #3 on: February 18, 2019, 02:48:07 pm »
VarIsEmpty checks for VarEmpty. It's just a wrapper for the check. You shouldn't check for VarEmpty directory.

Quote
VarIsEmpty returns true if the given variant contains the value Unassigned.

So to make sure you could also do
Code: Pascal  [Select]
  1. if not VarIsNullthen(ExcelServer) and not VarIsEmpty(ExcelServer) then //...

O yeah, to make it more confusing there is also VarIsClear() :)

Also note the function:
Code: Pascal  [Select]
  1. function VarIsEmpty(const V: Variant): Boolean;
  2. begin
  3.   Result:=TVarData(V).vType=varEmpty;
  4. end;

So that's something different from
Code: Pascal  [Select]
  1. if Variant <> varEmpty then ... // <-- incorrect

So
  • Check for unassigned with VarIsEmpty.
  • Check for an undefined value with VarIsClear.
  • Check for a null value with VarIsNull.
« Last Edit: February 18, 2019, 02:59:10 pm by rvk »

daveinhull

  • Full Member
  • ***
  • Posts: 249
  • 1 divided by nothing must still be 1!
Re: OLEVariants problem
« Reply #4 on: February 19, 2019, 02:56:53 am »
Thanks guys,

This worked
Maybe
Code: Pascal  [Select]
  1. if not VarIsEmpty(ExcelServer) then

And thanks for the other info as well, all useful learning  :D

Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64