Solved

How to adapt my code to decrease user interaction?

Posted on 2004-10-30
159 Views
Last Modified: 2010-04-05
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.
0
Question by:PeterdeB
    5 Comments
     
    LVL 7

    Expert Comment

    by:LRHGuy
    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
     

    Author Comment

    by:PeterdeB
    Ok tnx for telling me hang on and I'll post it!

    PeterdeB
    0
     

    Author Comment

    by:PeterdeB
    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
     
    LVL 7

    Accepted Solution

    by:
    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
     

    Author Comment

    by:PeterdeB
    Brilliant truly brilliant Jiiieehaaaaaa!!!

    Thanks pal thank you thank you!!!!!

    regard from PeterdeB
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Shellfire Box VPN + Lifetime Subscription

    The Shellfire Box easily connects all of your devices, even those that don't offer the possibility to establish a safe vpn connection. Access blocked content and surf safely, no matter where in the world you are located.

    This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now