Excel

Handling dialog boxes in IE automation

Ease of Use

Hard

Version tested with

2003 

Submitted by:

mohanvijay

Description:

The main aim of this program is handle dialog boxes in IE automation. This program login into the gmail and compose mail and attach the file 

Discussion:

When i come into the IE automation through VBA i can't set file path to html file input element. So i decided to click the file input element and set the file path to file dialog window but the problem arises, when the file pop-up window (File Dialog) shows the VBA stops running the program and after the closing of pop-up VBA continues (I know this problem when answering this thread http://www.vbaexpress.com/forum/showthread.php?t=39490). So i make three supporting files iehelp.vbs(VB Script file),iehelp.txt (text file) and iehelp.xls(Excel file) .The main program runs the VB Script file before click the file input element. The VB Script file open the excel file (iehelp.xls) in new instance of Excel.Application after the 10 seconds from when the VB Script run.The excel file set the path to file dialog box in the Workbbok_Open Event I tested the following code with OS = Windows XP,IE Version = 8, Excel 2003 

Code:

instructions for use

			

'****************************************************** '****************************************************** 'Code in main.xls '****************************************************** '****************************************************** Sub Main_Module() 'This Project Contains main module that is where the code execute and the Three 'Supporting files they are listed below '1. The VB Script file named iehelp.vbs '2. The Text file named iehelp.txt '3. The excel file named iehelp.xls '************************************ '** Main Module: ** '************************************ 'The main module program does the following '1. Login into gmail '2. Change gmail view into standard HTML view for easy use '3. Click Compose mail '4. Save the dialog Caption (Ex:Choose file,Save As) and button text (Ex.Open,Save) and file path into text file (iehelp.txt) '4. Run the VB Script iehelp.vbs '5. Click the "Attch file" in compse mail '************************************ '** VB Script (iehelp.vbs): ** '************************************ 'The VB Script program does the following '1. Wait for 10 Seconds from when it ran from main module '2. Create the new instance of Excel.Application '3. Open the Excel file (iehelp.xls) '************************************ '** Excel file (iehelp.xls): ** '************************************ 'The iehelp.xls Excel file run the following program in open event '1. Open the iehelp.txt file and get the dialog caption,button txt and file path into array (variable name = Tl_Str) '2. Find the dilaog box '3. Set the file path to the file dialog box '4. Click the open button in the file dialog box 'URL of releated problem http://www.vbaexpress.com/forum/showthread.php?t=39490 Const Txt_Path As String = "C:\iehelp.txt" 'text file path for hold File chooser Const Dlg_Head As String = "Choose File to Upload" 'File dialog window name Const Dlg_Butt_Name As String = "&Open" 'open button name open file dialog Const M_URL As String = "http://mail.google.com" Const M_User As String = "" ' Your email user name Const M_Pwd As String = "" ' Your email password Dim OB_IE As SHDocVw.InternetExplorer Dim OB_Doc As MSHTML.HTMLDocument Dim EE_Anch As MSHTML.HTMLAnchorElement Set OB_IE = New SHDocVw.InternetExplorer OB_IE.Visible = True OB_IE.Navigate M_URL Do Until OB_IE.ReadyState = READYSTATE_COMPLETE Loop Set OB_Doc = OB_IE.Document OB_Doc.all.Item("Email").Value = M_User ' Set user name OB_Doc.all.Item("Passwd").Value = M_Pwd ' Set password OB_Doc.all.Item("signIn").Click Do Until OB_IE.ReadyState = READYSTATE_COMPLETE Loop 'wait for 45 seconds for load all elements. you can change the seconds depends upon your internet speed Application.Wait (Now + TimeValue("00.00.45")) 'loop through the each HTMLANCHORELEMENT and find element to change current gmail view ind basic HTML view For Each EE_Anch In OB_Doc.all If EE_Anch.href = "https://mail.google.com/mail/?ui=html&zy=s" Then EE_Anch.Click Exit For End If Next Do Until OB_IE.ReadyState = READYSTATE_COMPLETE Loop 'wait for 20 seconds for load all elements. you can change the seconds depends upon your internet speed Application.Wait (Now + TimeValue("00.00.20")) 'loop through the each HTMLANCHORELEMENT and find the element for compose mail and activate For Each EE_Anch In OB_Doc.all If Len(EE_Anch.href) > 16 Then If Right(EE_Anch.href, 16) = "?&v=b&pv=tl&cs=b" Then EE_Anch.Click Exit For End If End If Next Do Until OB_IE.ReadyState = READYSTATE_COMPLETE Loop 'wait for 20 seconds for load the all elements. you can change the seconds depends upon your internet speed Application.Wait (Now + TimeValue("00.00.20")) 'create or open the text file which path set to the constant Txt_Path Dim S_FSO As Object Dim S_TxtFile As Object Set S_FSO = CreateObject("Scripting.filesystemobject") If S_FSO.fileexists(Txt_Path) = True Then Set S_TxtFile = S_FSO.opentextfile(Txt_Path, 2) Else Set S_TxtFile = S_FSO.createtextfile(Txt_Path) End If Dim Upload_File As String Upload_File = "C:\test.txt" 'set the file path of which is set to the file in gmail attach file dialog S_TxtFile.write Dlg_Head & ";;" & Dlg_Butt_Name & ";;" & Upload_File S_TxtFile.Close Set S_TxtFile = Nothing Set S_FSO = Nothing Dim R_Shl As Double 'run the script file before click the browse button in gmail R_Shl = Shell("wscript.exe C:\iehelp.vbs") OB_Doc.all("file0").Click Set EE_Anch = Nothing Set OB_Doc = Nothing Set OB_IE = Nothing End Sub '****************************************************** '****************************************************** 'Code in VB Script File (iehelp.vbs) '****************************************************** '****************************************************** Dim t_wait t_wait = now + timevalue("00.00.10") Do Until now > t_wait Loop Dim xl_app Dim wk_ie Set xl_app = createobject("Excel.Application") Set wk_ie = xl_app.workbooks.open("C:\iehelp.xls") wk_ie.close Set wk_ie = Nothing xl_app.quit Set xl_app = Nothing '****************************************************** '****************************************************** 'Code in Excel File (iehelp.xls) (in Thisworkbook module) '****************************************************** '****************************************************** 'declere API function to send messages to window Private Declare Function SendMessage Lib "user32" Alias _ "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, lParam As Any) As Long 'declere API function to get next window for search Private Declare Function GetNextWindow Lib "user32" Alias _ "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long 'declere API function to get lenth of a windows text Private Declare Function GetWindowTextLength Lib _ "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long 'declere API function to get windows text Private Declare Function GetWindowText Lib "user32" Alias _ "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long 'declere API function to find in child windows Private Declare Function FindWindowEx Lib "user32" Alias _ "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, _ ByVal lpsz1 As String, ByVal lpsz2 As String) As Long 'declere API function to find window Private Declare Function FindWindow Lib "user32" Alias _ "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Const WIN_ClassName_FilePath As String = "COMBOBOXEX32" 'class name of filepathbox Private Const WIN_ClassName_Button As String = "BUTTON" 'class name of buuton Private Const WM_SETTEXT = &HC 'send messaget value for set text to file path box Private Const BM_CLICK = &HF5 'send message value to click button Private Const WIN_NEXT As Long = 2 'value to search window through next Private Const WIN_PREVIOUS As Long = 3 'value to search window through previous Private Sub Workbook_Open() Main_SetPath End Sub Private Sub Main_SetPath() 'this procedure find window named that you assigned into variable Dialog_Caption 'and set the file_path ' if you want to open the the file, change constant ButtonTxt into what text shoed on the ' open dialogbox Const T_Path As String = "C:\iehelp.txt" Dim WIN_Dialog As Long 'Hold dialogbox hwnd Dim Dlg_ChildWIN As Long 'hold hwnd of childwindows of dialogbox Dim Dialog_Caption As String 'var to hold caption of dialog box for find dialogbox hwnd Dim Dlg_Retun As Long 'var to make sure set path successfully Dim File_Path As String 'hold file path change for your suit Dim ButtonTxt As String 'hold text of button (save,open) Dim Same_Window As Long Dim FSO As Object Dim T_M As Object Dim Tl_Str() As String Set FSO = CreateObject("Scripting.FileSystemObject") Set T_M = FSO.opentextfile(T_Path) Tl_Str = Split(T_M.readall, ";;") T_M.Close Set T_M = Nothing Set FSO = Nothing Dialog_Caption = Tl_Str(0) 'set the diloagbox caption you can change this as dialog box caption ButtonTxt = Tl_Str(1) File_Path = Tl_Str(2) WIN_Dialog = 0 'set the intional value WIN_Dialog = FindWindow(vbNullString, Dialog_Caption) 'get the dialogbox hwnd If WIN_Dialog = 0 Then 'if dialogbox not open then exit MsgBox "Cannot find dialog box named '" & Dialog_Caption & _ "' make sure dialogbox open or change to your suit" Exit Sub End If 'Search for any opend window has same name as dialog_caption if found then exit Same_Window = Find_WindowDuplicte(WIN_Dialog, Dialog_Caption) If Same_Window <> 0 Then MsgBox "More than one windows opened in the name of '" & _ Dialog_Caption & "' Please check and close one" Exit Sub End If 'get hwnd of filepathbox and set the path if not found then exit Dlg_ChildWIN = FindWindowEx(WIN_Dialog, 0, WIN_ClassName_FilePath, vbNullString) If Dlg_ChildWIN <> 0 Then Dlg_Retun = SendMessage(Dlg_ChildWIN, WM_SETTEXT, 0, ByVal File_Path) 'set file path If Dlg_Retun <> 1 Then 'Ensure that path set successfully if not exit MsgBox "Path Not set please try again" Exit Sub End If Else MsgBox "File path window not found" Exit Sub End If 'get hwnd of savebutton and set the path if not found then exit Dlg_ChildWIN = FindWindowEx(WIN_Dialog, 0, WIN_ClassName_Button, ButtonTxt) If Dlg_ChildWIN <> 0 Then SendMessage Dlg_ChildWIN, BM_CLICK, 0, 0 'click button Else MsgBox "Button window not found" Exit Sub End If End Sub Private Function Find_WindowDuplicte(Main_Hwnd As Long, WIN_Caption As String) As Long Dim Ser_Win As Long 'var to hold windows hwnd Dim TxtLen_Win As Long 'var to get the text lenth of window Dim Txt_Win As String 'var to get windows text Dim Ret_Txt As Long 'var to get windows text lenth Find_WindowDuplicte = 0 'set intial value 'start search from main window through next Ser_Win = GetNextWindow(Main_Hwnd, WIN_NEXT) If Ser_Win = 0 Then Exit Function Do Until Ser_Win = 0 TxtLen_Win = GetWindowTextLength(Ser_Win) 'get next windows text lenth TxtLen_Win = TxtLen_Win + 1 'add one extra to null value Txt_Win = Space$(TxtLen_Win) 'make var lenth as window text lenth 'get the window text into txt_win variable and text lenth into Ret_Txt variable Ret_Txt = GetWindowText(Ser_Win, Txt_Win, TxtLen_Win) If Ret_Txt > 0 Then Txt_Win = UCase(Left(Txt_Win, Ret_Txt)) End If If Txt_Win = UCase(WIN_Caption) Then 'match window text into main_window Find_WindowDuplicte = Ser_Win Exit Function End If Ser_Win = GetNextWindow(Ser_Win, WIN_NEXT) Loop 'start search from main window through previous Ser_Win = GetNextWindow(Main_Hwnd, WIN_PREVIOUS) If Ser_Win = 0 Then Exit Function Do Until Ser_Win = 0 TxtLen_Win = GetWindowTextLength(Ser_Win) Txt_Win = Space$(TxtLen_Win) Ret_Txt = GetWindowText(Ser_Win, Txt_Win, TxtLen_Win) If Ret_Txt > 0 Then Txt_Win = UCase(Left(Txt_Win, Ret_Txt)) End If If Txt_Win = UCase(WIN_Caption) Then Find_WindowDuplicte = Ser_Win Exit Function End If Ser_Win = GetNextWindow(Ser_Win, WIN_PREVIOUS) Loop End Function

How to use:

  1. Extract the attached zip file
  2. save the iehelp.xls,iehelp.vbs and iehelp.txt to the location as you wish
  3. Open the iehelp.vbs in notepad
  4. Check "C:\iehelp.xls" and change the path if you save the excel file in different location
  5. Save and close the iehelp.vbs file
  6. Open the iehelp.xls file
  7. Check "C:\iehelp.txt" and change the path if you save the text file in different location
  8. Save and close the iehelp.xls file
  9. Open the main.xls file
  10. Check "C:\iehelp.txt" and change the path if you save the text file in different location
  11. Check "C:\iehelp.vbs" and change the path if you save the VB Script file in different location
  12. Create the text file in "C:\test.txt" for attach file in gmail or change the path as you like
  13. Save and run the Main_Module Procedure or click the button in Sheet1
 

Test the code:

  1. Check the following configuration
  2. OS = Windows XP
  3. IE Version 7 or Later
  4. Open the main.xls
  5. Open the VBE (Press Alt+F11)
  6. Open the Module named "Main"
  7. Set gmail Username to constant M_User
  8. Set gmail Password to constant M_Pwd
  9. Click the Dialog button
 

Sample File:

IE Dialog.zip 23.14KB 

Approved by Jacob Hilderbrand


This entry has been viewed 340 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express