Results 1 to 8 of 8

Thread: [RESOLVED] Need help with a sendkey function.

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    15

    Resolved [RESOLVED] Need help with a sendkey function.

    So I'm creating an application that automates a job. Basically, it opens internet explorer, points to a user driven URL, selects the "check all" then selects delete.
    My issue is, when delete is hit, a messagebox pops up that wants to confirm "do you want to delete all records?"

    i've tried the sendkey with no luck.
    any suggestions?

    heres my code.

    Code:
    Public Class Form1
        Dim oInputs As Object
        Dim oWShell As Object
    
        Private Sub btndel_Click(sender As Object, e As EventArgs) Handles btndel.Click
            With CreateObject("InternetExplorer.Application")
                .visible = True
                .Navigate(txturl.Text)
                Threading.Thread.Sleep(3000)
                oInputs = .document.getElementsbytagname("a")
                For Each elm In oInputs
                    If InStr(elm.innertext, "(Un)Check All") > 0 Then
                        elm.click()
                        Exit For
                    End If
                Next
                For Each elm In oInputs
                    If InStr(elm.innertext, "Delete") > 0 Then
                        elm.click()
                        Exit For
                    End If
                Next
                Do While .busy Or .readystate <> 4
                    Threading.Thread.Sleep(2000)
                Loop
            End With
            Threading.Thread.Sleep(5000)
    
            oWShell = CreateObject("wscript.shell")
            Do Until oWShell.AppActivate("Message from webpage")
                Threading.Thread.Sleep(2000)
            Loop
            Threading.Thread.Sleep(2000)
            AppActivate("Message from webpage")
            oWShell.sendkeys.send("{ENTER"})
    
        End Sub

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Need help with a sendkey function.

    Quote Originally Posted by msnyder1112 View Post
    i've tried the sendkey with no luck.
    any suggestions?
    Try it like this,..

    Code:
    Dim objShell = CreateObject("WScript.Shell")
    Dim Success = objShell.AppActivate("Message from webpage")
    
    Do Until Success = True
        Threading.Thread.Sleep(1000)
        Success = objShell.AppActivate("Message from webpage")
    Loop
    
    Application.DoEvents()
    AppActivate("Message from webpage")
    SendKeys.Send("{ENTER}")
    If that doesn't solve it then I'd revert to using API code like post #2 here.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    15

    Re: Need help with a sendkey function.

    This worked! Thanks!
    Another question that you may be able to help me with... how would you go about automatically closing the browser. I can have 15k records and it only displays 50 on a page so it opens multiple browser.
    I would like the browser to close after it hits OK? heres my current code that works

    Code:
    Public Class deleteutil
        Dim oInputs As Object
        Dim oWShell As Object
    
    
        Private Sub btndel_Click(sender As Object, e As EventArgs) Handles btndel.Click
    
    
            With CreateObject("InternetExplorer.Application")
                .visible = True
                .Navigate(txturl.Text)
                Threading.Thread.Sleep(3000)
                oInputs = .document.getElementsbytagname("a")
                For Each elm In oInputs
                    If InStr(elm.innertext, "(Un)Check All") > 0 Then
                        elm.click()
                        Exit For
                    End If
                Next
    
                For Each elm In oInputs
                    If InStr(elm.innertext, "Delete") > 0 Then
                        elm.click()
                        Exit For
                    End If
                Next
    
                Do While .busy Or .readystate <> 4
                    Threading.Thread.Sleep(2000)
                Loop
            End With
    
            Dim objShell = CreateObject("WScript.Shell")
            Dim Success = objShell.AppActivate("Message from webpage")
    
            Do Until Success = True
                Threading.Thread.Sleep(1000)
                Success = objShell.AppActivate("Message from webpage")
    
            Loop
    
            Application.DoEvents()
            AppActivate("Message from webpage")
            SendKeys.Send("{ENTER}")
    
    
    
        End Sub

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Need help with a sendkey function.

    Please don't use Threading.Thread.Sleep() and application.doevents. Use each instance exposed properties PROPERLY. Please read msdn webbrowser classes and post back.
    My Github - 1d3nt

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    15

    Re: Need help with a sendkey function.

    Explain...

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Need help with a sendkey function.

    One condition. You explain first why you use them. Do you understand what the lines even do? After that i will go line by line with you to help you make a more efficient program.
    My Github - 1d3nt

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Need help with a sendkey function.

    Quote Originally Posted by msnyder1112 View Post
    ... how would you go about automatically closing the browser. I can have 15k records and it only displays 50 on a page so it opens multiple browser.
    Just a wild guess going by some info on MSDN here,...
    Code:
    ' Add reference "Microsoft Internet Controls" (from COM tab).
    Imports SHDocVw
    Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            ' create instance of IE
            Dim ie As SHDocVw.InternetExplorer
            ie = CType(CreateObject("InternetExplorer.Application"), InternetExplorer)
            ' do whatever with this instance of IE
            With ie
                .Visible = True
                .Navigate(txturl.Text)
                ' bla bla bla...
            End With
    
            ' <click OK or whatever here >
    
            ' close this instance of IE
            ie.Quit()
        End Sub
    
    End Class
    Overall using sleep and loops is not very good, you should probably be acting on the IE events like DocumentComplete. I don't do this sort of stuff so will hand it off to others, best of luck!

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2013
    Posts
    15

    Re: Need help with a sendkey function.

    Thank you Edgemeal. I appreciate your help. Everything works flawlessly!

    ident- Instead of being a dickhead, why not offer assistance? You tell me not to do something but refuse to explain why, and bark at me to explain why I am doing it the way I chose. Were you abused as a child? Did you not get enough attention? I came here for assistance, not some lousy piece of ***** jerk to try and criticize my work in a manner that is uncalled for. So far, your little smartass comments have not benefited in this post at all. So if you cannot explain to me why not to use thread sleep with do events, your comments are pointless.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width