Solved

trying to use the find window function

Posted on 2006-11-21
8
334 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Industry Leaders: 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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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.

756 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