tags:

views:

354

answers:

7

I have an error in my button click, and I can't figure out how to resolve it.

This is my code:

if (ovElements.item(i).name = 'add') and    
(ovElements.item(i).type = 'button') and    
(ovElements.item(i).Value = ' + ') then    
ovElements.item(i).Click;

This is the markup:

<td width="20" align="left"><input class="button" style="width: 30px;" 
 name="add" value=" + " onclick="addLvl();" type="button"></td>

And it gives this error:

Invalid Variant Operation Error

What did I do wrong?

A: 

It means an operator on a variant which is executed is invalid. This happends, for example, when a variant containing some text is devided by an integer. Clearly this can not work, but since the compiler can't check this it is a runtime error.

Use temporary variable for the 3 parts in your if statement to see better on which line the error is raised. Then inspect what the values are and what the invalid operation is.

Lars Truijens
+1  A: 

Just a guess:

ovElements.item(i).Value is probably a Variant. If a variant contains a null value you will get that error when you compare it to a string.

Make sure ovElements.item(i) doesn't contain a null value before comparing it.

Kluge
A: 

i have

var

ovElements: OleVariant; i: Integer;

my englisch is not very good and i not understand all yours answer i must chagne i: Integer; to i: String

this fix my problem?

soryy for my englisch and sorry for my understand and thx very much for any answer i try transalte yours answer witch dictionary when i go to home

A: 

ok i meaby know whots you means but i am noob on delphi so i think var integer is not good for 3 variants and i must chagne this for exaple string?

can you give me fix code ? fix code looks like:

var

ovElements: OleVariant; i: String;

if (ovElements.item(i).name = 'add') and
(ovElements.item(i).type = 'button') and
(ovElements.item(i).Value = ' + ') then
ovElements.item(i).Click;

and they must work??

A: 

sorry for few post but i cant edit

i think this code on the web is java script and meaby this is problem? how i can fix it

You **can** edit. Click the "edit" link below your question. You can also leave comments. (The minimum-reputation restriction is waived for answers to your *own* question, so you can comment on others' answers.)
Rob Kennedy
A: 

i think this can fix my problem: uses MSHTML;

procedure TForm1.CalladdLvl(); (S: string; I: Integer); { Calls JavaScript addLvl(); function } var Doc: IHTMLDocument2; // current HTML document HTMLWindow: IHTMLWindow2; // parent window of current HTML document JSFn: string; // stores JavaScipt function call begin // Get reference to current document Doc := WebBrowser1.Document as IHTMLDocument2; if not Assigned(Doc) then Exit; // Get parent window of current document HTMLWindow := Doc.parentWindow; if not Assigned(HTMLWindow) then Exit; // Run JavaScript try JSFn := Format('addLvl();("%s",%d)', [S, I]); // build function call HTMLWindow.execScript(JSFn, 'JavaScript'); // execute function except // handle exception in case JavaScript fails to run end; end;

A: 

You can save "ovElements.item(i)" to a local variable and then split your code into multiple line.

obj = ovElements.item(i);
if obj <> nil then
try
  if obj.name = 'add' then
    if obj.type = 'button' then
      if obj.value = ' + ' then    
        obj.click;
except
end;

In this way you can see which line causes this problem.

stanleyxu2005