[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to adapt my code to decrease user interaction?

Posted on 2004-10-30
5
Medium Priority
?
161 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
Comment
Question by:PeterdeB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:LRHGuy
ID: 12455075
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
ID: 12455108
Ok tnx for telling me hang on and I'll post it!

PeterdeB
0
 

Author Comment

by:PeterdeB
ID: 12455130
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:
LRHGuy earned 1000 total points
ID: 12455283
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
ID: 12455373
Brilliant truly brilliant Jiiieehaaaaaa!!!

Thanks pal thank you thank you!!!!!

regard from PeterdeB
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

656 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