How to adapt my code to decrease user interaction?

Hi guys! I wrote this small app which launches Internet Explorer in the FormCreate Event after which a button press fills in several fields and clicks a button to login to some site. So far so good.
Now I'd like to have this app launch IE >  do the button click > and close itself all by just running the application. (doubleclicking)
Now I've tried several scenario's but all of which were unsuccesful, I implemented some naps (sleep command) but I can't get this done.

Currently: Running the app > launches the desired IE Window
               Clicking the button > logs in automatically
               Closing the app > kills the app

Desired: Running the app > launches the desired IE Window > logs in > kills the app

How do I accomplish this 'behaviour'?

If you need to see my code please let me know!

My regards and max respect to all of you guys!!!

PeterdeB from Holland

Ps I can't figure out how to get this baby going and after a few hours of experimenting I gave up. Please provide working sample or assist in adapting my code.
PeterdeBAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LRHGuyCommented:
I would suggest doing something with a timer. Drop a timer on the form, set the interval to 10 or 15 seconds, or however long it may take for IE to launch. In the timer event, the first time in call Button.Click, then wait another 10-15 seconds, on the second call, execute form.close.

Without seeing your code, we're working blind here!
0
PeterdeBAuthor Commented:
Ok tnx for telling me hang on and I'll post it!

PeterdeB
0
PeterdeBAuthor Commented:
Here you are. These are the procedures. Messing around with these resulted in either no fields that got filled, no button that got clicked or some java window popping up nagging because of the fact the fields were left blank while the button got clicked. After some hours I gave up.....tnx in advance!

PeterdeB

//Beginning code

procedure TForm1.FormCreate(Sender: TObject);
begin
ShellExecute(HANDLE,
    'open',
    'http://www.someurl.com/index.php',
    nil,
    nil,
    SW_SHOW);
end;


procedure TForm1.Button2Click(Sender: TObject);
var
  ShellWindow: IShellWindows;
  WB: IWebbrowser2;
  spDisp: IDispatch;
  IDoc1: IHTMLDocument2;
  Document: Variant;
  k, m: Integer;
  ovElements: OleVariant;
  i: Integer;
begin
 ShellWindow := CoShellWindows.Create;
  // get the running instance of Internet Explorer
  for k := 0 to ShellWindow.Count do
  begin
    spDisp := ShellWindow.Item(k);
    if spDisp = nil then Continue;
    // QueryInterface determines if an interface can be used with an object
    spDisp.QueryInterface(iWebBrowser2, WB);
    if WB <> nil then
    begin
      WB.Document.QueryInterface(IHTMLDocument2, iDoc1);
      if iDoc1 <> nil then
      begin
        WB := ShellWindow.Item(k) as IWebbrowser2;
        begin
          Document := WB.Document;
          // count forms on document and iterate through its forms
          for m := 0 to Document.forms.Length - 1 do
          begin
            ovElements := Document.forms.Item(m).elements;
            // iterate through elements
            for i := 0 to ovElements.Length - 1 do
            begin
              // when input fieldname is found, try to fill out
              try
                if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and
                  (CompareText(ovElements.item(i).type, 'text') = 0) then
                begin
                  ovElements.item(i).Value := 'myname';
                end;
              except
              end;
              try
                if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and
                  (CompareText(ovElements.item(i).type, 'password') = 0) then
                begin
                  ovElements.item(i).Value := 'mypassword';
                end;
              except
              end;
              // when Submit button is found, try to click
              try
                if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and
                  (CompareText(ovElements.item(i).type, 'SUBMIT') = 0) and
                  (ovElements.item(i).Value = 'Log mij in') then  // Suchen für German
                begin
                  ovElements.item(i).Click;
                end;
              except
              end;
            end;
          end;
        end;
      end;
    end;
  end;
end;
0
LRHGuyCommented:
This works great for me.

I moved the code from button to Go1. Go1 returns TRUE if the 'click' button was found in the form. At startup, a two second timer starts. When it expires, it tries to fill in the form. If the CLICK was found it closes the program, otherwise it waits another two seconds and tries again. I leave the fine tuning to you!


procedure TForm2.FormCreate(Sender: TObject);
begin
ShellExecute(HANDLE,
    'open',
    'http://www.lrh.net/public/code/html-m/public/newuser.htm',
    nil,
    nil,
    SW_SHOW);
  Timer1.enabled:=true;
end;


function TForm2.Go1:boolean;
var
  ShellWindow: IShellWindows;
  WB: IWebbrowser2;
  spDisp: IDispatch;
  IDoc1: IHTMLDocument2;
  Document: Variant;
  k, m: Integer;
  ovElements: OleVariant;
  i: Integer;
begin
  result:=false; //assume no go
 ShellWindow := CoShellWindows.Create;
  // get the running instance of Internet Explorer
  for k := 0 to ShellWindow.Count do
  begin
    spDisp := ShellWindow.Item(k);
    if spDisp = nil then Continue;
    // QueryInterface determines if an interface can be used with an object
    spDisp.QueryInterface(iWebBrowser2, WB);
    if WB <> nil then
    begin
      WB.Document.QueryInterface(IHTMLDocument2, iDoc1);
      if iDoc1 <> nil then
      begin
        WB := ShellWindow.Item(k) as IWebbrowser2;
        begin
          Document := WB.Document;
          // count forms on document and iterate through its forms
          for m := 0 to Document.forms.Length - 1 do
          begin
            ovElements := Document.forms.Item(m).elements;
            // iterate through elements
            for i := 0 to ovElements.Length - 1 do
            begin
              // when input fieldname is found, try to fill out
              try
                if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and
                  (CompareText(ovElements.item(i).type, 'text') = 0) then
                begin
                  ovElements.item(i).Value := 'custodian2';
                end;
              except
              end;
              try
                if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and
                  (CompareText(ovElements.item(i).type, 'password') = 0) then
                begin
                  ovElements.item(i).Value := 'mypassword';
                end;
              except
              end;
              // when Submit button is found, try to click
              try
                if (CompareText(ovElements.item(i).tagName, 'INPUT') = 0) and
                  (CompareText(ovElements.item(i).type, 'SUBMIT') = 0) and
                  (ovElements.item(i).Value = 'Sign Up') then
                begin
                  ovElements.item(i).Click;
                  result:=true; // we're a go!
                end;
              except
              end;
            end;
          end;
        end;
      end;
    end;
  end;
end;



procedure TForm2.Timer1Timer(Sender: TObject);
begin
  Timer1.enabled:=false;
  if Go1 then begin
    close;
  end
  else begin
    Timer1.Enabled:=true; //start timer again
  end;
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PeterdeBAuthor Commented:
Brilliant truly brilliant Jiiieehaaaaaa!!!

Thanks pal thank you thank you!!!!!

regard from PeterdeB
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.