Search code examples
delphicomlotus-noteslotus-domino

COM Exception "Bad Variable Type" Error in Delphi Call of Domino GetDocumentByKey Method


I have a legacy Delphi 2 application that I need to convert from communicating with Notes via OLE Automation to communicating via COM early binding. I am using Delphi 7 since the code base is large and I want to avoid the work of dealing with the Unicode support in the more current versions of Delphi.

The basics are working: the program opens the database then the view and searches for a particular document using the NotesView.GetDocumentByKey method. The GetDocumentByKey call works when the first parameter is a single string cast to an OleVariant as shown below (opening of DB and view not shown).

var
  Key: OleVariant;
const ExactMatch: WordBool = True;
begin
  Key := 'AKeyValue';
  Doc := View.GetDocumentByKey(Key, ExactMatch);

The bad variable type error occurs when the first parameter is a variant array as required when it is desired to search the view based on multiple columns as shown below.

var
  TwoKeysV: OleVariant;
const ExactMatch: WordBool = True;
begin
  TwoKeysV := VarArrayCreate([0, 1], varOleStr);
  TwoKeysV[0]:= WideString('Key1');
  TwoKeysV[1]:= WideString('Key2');
  Doc := View.GetDocumentByKey(TwoKeysV, ExactMatch);

I have tried several variations on the two key assignment statements with no success. For example, just assigning the key string without a cast still produces the bad variable type, and using the StringToOleString function is rejected by the compiler as an invalid assignment (PWideChar to Variant).


Solution

  • I can't test this, so I'm not sure this works.

    HELP: If this method is used under COM, with a keyArray parameter of an array, it must be defined as an array of type Variant

    So you need to pass: an array of type Variant

    Based on How to use variant arrays in Delphi.

    Note: Code edited by Keeloid to match code that worked by casting key string to WideString.

    var
      TwoKeysV: OleVariant;
    const ExactMatch: WordBool = True;
    begin
      TwoKeysV := VarArrayCreate([0, 1], varVariant);
      TwoKeysV[0]:= WideString('Key1');  {WideString = varOleStr}
      TwoKeysV[1]:= WideString('Key2');
      Doc := View.GetDocumentByKey(TwoKeysV, ExactMatch);