Link to home
Start Free TrialLog in
Avatar of stuayre
stuayre

asked on

Auto fill out forms using Delphi Twebbrowser

Hi Guys,

Im using this code to auto fill out forms in delphi on a twebbrowser, but i cant get it to work if there is no FORM tag on the page, does anyone know if i could change it to work so it doesnt look for the FORM tag?

cheers

Stu!

To test this code put a TWebBrowser and A TButton component on the form }
 
 
function FillForm(WebBrowser: TWebBrowser; FieldName: string; Value: string): Boolean; 
var 
  i, j: Integer; 
  FormItem: Variant; 
begin 
  Result := False; 
  //no form on document 
  if WebBrowser.OleObject.Document.all.tags('FORM').Length = 0 then 
  begin 
    Exit; 
  end; 
  //count forms on document 
  for I := 0 to WebBrowser.OleObject.Document.forms.Length - 1 do 
  begin 
    FormItem := WebBrowser.OleObject.Document.forms.Item(I); 
    for j := 0 to FormItem.Length - 1 do 
    begin 
      try 
        //when the fieldname is found, try to fill out 
        if FormItem.Item(j).Name = FieldName then 
        begin 
          FormItem.Item(j).Value := Value; 
          Result := True; 
        end; 
      except 
        Exit; 
      end; 
    end; 
  end; 
end; 
 
 
//When the document is complete try to fill out the field homepage with the url 
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject; 
  const pDisp: IDispatch; var URL: OleVariant); 
begin 
  if FillForm(WebBrowser1, 'homepage', 'http://www.swissdelphicenter.ch') = False then 
    ShowMessage('Error. Field not available or no Form found.');
end;
 
// Show the Webbrowser-Progress in Label1 
procedure TForm1.WebBrowser1ProgressChange(Sender: TObject; Progress, ProgressMax: Integer); 
begin 
  if ProgressMax = 0 then 
  begin 
    label1.Caption := ''; 
    Exit; 
  end; 
  try 
    if (Progress <> -1) and (Progress <= ProgressMax) then 
      label1.Caption := IntToStr((Progress * 100) div ProgressMax) + '% loaded...' 
    else 
      label1.Caption := ''; 
  except 
    on EDivByZero do Exit; 
  end; 
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of stuayre
stuayre

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial