Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

trying to use the find window function

Posted on 2006-11-21
8
Medium Priority
?
353 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
[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
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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

715 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