Solved

trying to use the find window function

Posted on 2006-11-21
8
298 Views
Last Modified: 2008-02-01
I have some code that opens IE from Access, navigates to a webpage, looks for an html file download link named "spreadsheet", clicks it and then uses the sendkeys function to follow the file saving process.  

Unfortunately i am getting hung up at one of the windows becuase the code loses focus of it. I was thinking maybe I should use the find window function and then focus on the windw to hit %S to save the file with send keys.  Here is my code, can someone help me utilize the find window code properly into what I am trying to do.

Thanks

Cory

Function import_atl_dspec() '(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim o2
    Set o2 = CreateObject("internetexplorer.application")
        o2.navigate "www.tester.com" 'IE navigates to a webpage
        mySleep 50 'wait 5 seconds
        SendKeys "~"
        o2.Visible = False 'hides IE
        While o2.busy: DoEvents: Wend
     
Set o = o2.Document.All.tags("A")


M = o.length: mySubmit = -1
For r = 0 To M - 1

If InStr(1, o.Item(r).innerhtml, "Spreadsheet", vbTextCompare) Then
    o.Item(r).focus
    o.Item(r).Click: Exit For
End If
Next

mySleep 200 'wait 40 seconds

'THIS IS WHERE I AM LOSING FOCUS.  AT THIS POINT,  I NEED TO FIND THE WINDOW "FILE DOWNLOAD" WHICH IS THE CAPTION ON IT, GET FOCUS ON THE WINDOW AND THEN FOLLOW THE REST OF THE CODE.

SendKeys "%S" 'Hits the save button
mySleep 50 'wait five seconds and starts typing in the path to save the file
SendKeys "g:\Subgroup\Access Data\LBX\TEST\tbl_atl_dspec.csv"

mySleep 10 'waits 1 second
SendKeys "%S" 'Hits the save button
'SendKeys "%c" 'Hits the Close this dialog box when download completes

mySleep 20 'Waits 2 seconds and Msgbox appears: 'file exists - do you want to overwrite'
SendKeys "%y"
mySleep 100 ' Waits 10 Seconds
SendKeys "~"


o2.Quit
Set o2 = Nothing
End Function
0
Comment
Question by:cstraim
8 Comments
 
LVL 39

Expert Comment

by:thenelson
ID: 18009097
Perhaps this code may help. This will open IE, load a website, enter a user name and password, navigate to an ASP page and upload a file.  Use the parts you want.


        SysCmd SYSCMD_SETSTATUS, "Opening Internet Explorer"
        On Error Resume Next
        Set Ie = CreateObject("InternetExplorer.Application")
        If Err.Number <> 0 Then
            MsgBox "Unable to open Internet Explorer" & Chr(13) _
                & "Open it and navigate to Freeclaims.com manually."
            DoCmd.Hourglass False
            SysCmd SYSCMD_CLEARSTATUS
            Exit Sub
        End If
        Ie.Visible = True
        SysCmd SYSCMD_SETSTATUS, "Loading login page"
        With Ie
            .Left = 50
            .Top = 50
            .Height = 495
            .Width = 811
            .Navigate "https://www.yourWebsite.com/docs/login.asp"
            .Visible = True
        End With
        StartTime = Now()
        Do
           If DateDiff("s", StartTime, Now) > 120 Then
                Ie.Visible = False
                MsgBox "Unable to navigate to 'https://www.yourWebsite.com/docs/login.asp'" _
                    & Chr(13) & "The site may not be active at this time" _
                    & Chr(13) & "Try to navigate there manually."
                Ie.Visible = True
                Set Ie = Nothing
                DoCmd.Hourglass False
                SysCmd SYSCMD_CLEARSTATUS
                Exit Sub
            End If
           Err.Clear
           Ie.Document.loginForm.focus
        Loop Until Err.Number = 0
       
        Ie.Document.loginForm("username").Value = "1234"
        Ie.Document.loginForm("userpassword").Value = "password"
        If Err.Number <> 0 Then
            Ie.Visible = False
            MsgBox "Problem logging in." _
                & Chr(13) & "Log in and navigate to www.yourWebsite.com manually." _
                & Chr(13) & Err.Number & vbCrLf & Err.Description
            Ie.Visible = True
            Set Ie = Nothing
            DoCmd.Hourglass False
            SysCmd SYSCMD_CLEARSTATUS
            Exit Sub
        End If
        Ie.Document.loginForm.submit.focus
        If Err.Number = 424 Then
            Err.Clear
        ElseIf Err.Number <> 0 Then
            Ie.Visible = False
            MsgBox "Problem logging in." _
                & Chr(13) & "Press 'Login' and navigate to www.yourWebsite.com manually."
            Ie.Visible = True
            Set Ie = Nothing
            DoCmd.Hourglass False
            SysCmd SYSCMD_CLEARSTATUS
            Exit Sub
        End If
       
        SysCmd SYSCMD_SETSTATUS, "Loading www.yourWebsite.com main page"
        StartTime = Now
        Do
           If DateDiff("s", StartTime, Now) > 30 Then
                Ie.Visible = False
                MsgBox "Unable to navigate to the www.yourWebsite.com main window." _
                    & Chr(13) & "The site may not be active at this time" _
                    & Chr(13) & "Try to navigate there manually."
                Ie.Visible = True
                Set Ie = Nothing
                DoCmd.Hourglass False
                SysCmd SYSCMD_CLEARSTATUS
                Exit Sub
            End If
        Loop Until Ie.Document.All(7).Src = "/docs/accstatus.asp" And Ie.Document.All(6).Src = "/docs/mynav.htm"
        StartTime = Now()
        Do
        Loop Until Now - StartTime > 0.00002
        Ie.Document.All(7).Src = "/docs/ViewBatch.asp"
       
        SysCmd SYSCMD_SETSTATUS, "Loading www.yourWebsite.com upload page"
        StartTime = Now
        Do
           If DateDiff("s", StartTime, Now) > 30 Then
                Ie.Visible = False
                MsgBox "Unable to navigate to the www.yourWebsite.com upload window." _
                    & Chr(13) & "The site may not be active at this time" _
                    & Chr(13) & "Try to navigate there manually."
                Ie.Visible = True
                Set Ie = Nothing
                DoCmd.Hourglass False
                SysCmd SYSCMD_CLEARSTATUS
                Exit Sub
            End If
        Loop Until Ie.Document.All(7).Src = "/docs/ViewBatch.asp"
End If

        Set Ie = Nothing
       
        DoCmd.Hourglass False
        SysCmd SYSCMD_CLEARSTATUS-
0
 

Author Comment

by:cstraim
ID: 18047137
Im not sure that this will help, as I am trying to find a specific window, focus on it, and then send keys to it.  This is finding a login form.  i don't know the function for finding a window
0
 

Author Comment

by:cstraim
ID: 18098100
I was able to resolve this myself if anyone needs it here ya go. I commented out the notes so you can see what i did step by step.

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
     Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
     Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
     
Function import_atl_dspec()
Dim o2
Dim lhwnd As Long
Set o2 = CreateObject("internetexplorer.application") 'Opens IE
         o2.navigate "www.mywebpage.com" 'IE navigates to a webpage

getWindow "#32770", "connect to mywebpage.com", lhwnd 'Finds the login window
        If lhwnd <> 0 Then
        SetForegroundWindow lhwnd 'Focuses on the login window
        mySleep 5
        SendKeys "~" ' 'Sends the [ENTER] Key to login
End If
o2.Visible = True 'Allows you to see the Webpage Load
While o2.busy 'Waits for the webpage to Load
mySleep 20 ' Waits for 2 Seconds
DoEvents: Wend 'Allows other processes on the computer to run

Set o = o2.Document.All.tags("A") 'Set's O to A'tags
M = o.length: mySubmit = -1
For r = 0 To M - 1

If InStr(1, o.Item(r).innerhtml, "Spreadsheet", vbTextCompare) Then 'Loops until it finds the "Spreadsheet" Link
   o.Item(r).focus
   o.Item(r).Click: Exit For 'Clicks the Spreadsheet Link
End If

Next

getWindow "#32770", "File Download", lhwnd 'Looks for the File Download Window
       If lhwnd <> 0 Then
       SetForegroundWindow lhwnd 'Brings the File Download Window to the foreground
       mySleep 20
       SendKeys "%S" 'Hits the save button
       End If

getWindow "#32770", "Save As", lhwnd ' Looks for the Save As Window
       If lhwnd <> 0 Then
       SetForegroundWindow lhwnd 'Brings the Save As window to the foreground
       End If
       SendKeys "G:\subgroup\access Data\lbx\raw Data\Current Week\dspec\tbl_atl_dspec.csv" 'Types the path for the file to be saved
       SendKeys "%S" 'Hits the save button
       mySleep 20 'Waits 2 seconds and Msgbox appears: 'file exists - do you want to overwrite'
       SendKeys "%y" ' Hits the yes button

getWindow "#32770", "Download Complete", lhwnd ' Looks for the Download Complete Window
       If lhwnd <> 0 Then
       SetForegroundWindow lhwnd 'Brings the Download Complete window to the foreground
       End If
       SendKeys "~" 'Hits the [Enter Key] to close the window

o2.Quit ' Closes out of IE
mySleep 20
Set o2 = Nothing
End Function

Sub getWindow(ByVal ID As String, ByVal title As String, ByRef win As Long)
    win = 0
    While win = 0
        win = FindWindow(ID, title)
        mySleep 5
    Wend
End Sub

Sub mySleep(ByVal deciSeconds As Long)
    Sleep (deciSeconds * 100)
    DoEvents
End Sub

Sub WaitWin()
    lhwnd = 0
End Sub
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:cstraim
ID: 18098109
Moderator:

I need to accept my own answer.  can you do that?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 18356869
cstraim,
That link has instructions for closing questions in all situations, including those where you get no acceptable answers, or solve the problem yourself.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 18414476
PAQed with points refunded (250)

Computer101
EE Admin
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

762 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

24 Experts available now in Live!

Get 1:1 Help Now