# Visual Basic.NET and IE



## muellerj (Jan 20, 2004)

I am creating an application that automatically logs me into my webmail. I need to find a way to use VB fill in the username and password and then "click" the OK button in internet explorer. I have attached the screenshot to demonstrate what I mean. The popup i believe is part of windows not IE, but I need to know how to interact with it from VB. Does anyone know or have a link to a site that could help me out. Thanks.


----------



## asbo (Sep 21, 2001)

Why not just put in your username and password, and tick the "Remember password" box?


----------



## lunapero (May 31, 2000)

you can use vb, vba or what ever to you want to move the cursor to the apporpriate text box and enter you criteria.. but as soon as they change there format your script it toast. 

asbo has a good idea, just add a click event and your set..


----------



## asbo (Sep 21, 2001)

What I mean is why does it have to be automated at all? You just fill in the username and password once, tick the box, and tada!

If that doesn't suit you, you could try http://username:[email protected] and see if that works.


----------



## pac_had (May 19, 2003)

Try the sendkeys() function.

This will only work if the login box is the active window.


----------



## xProphet (Jun 16, 2005)

There are a few ways to go about this using VB. If you are familliar with VB API look into the FindWindowByHandle and then the SendKeys() to send in the id and password. The skeleton would look something like this :

(General Declairations)
Public Const GW_HWNDNEXT = 2
Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, lpdwprocessid As Long) As Long

Function ProcIDFromWnd(ByVal hwnd As Long) As Long
Dim idProc As Long

' Get PID for this HWnd
GetWindowThreadProcessId hwnd, idProc

' Return PID
ProcIDFromWnd = idProc
End Function

Function GetWinHandle(hInstance As Long) As Long
Dim tempHwnd As Long

' Grab the first window handle that Windows finds:
tempHwnd = FindWindow(vbNullString, vbNullString)

' Loop until you find a match or there are no more window handles:
Do Until tempHwnd = 0
' Check if no parent for this window
If GetParent(tempHwnd) = 0 Then
' Check for PID match
If hInstance = ProcIDFromWnd(tempHwnd) Then
' Return found handle
GetWinHandle = tempHwnd
' Exit search loop
Exit Do
End If
End If

' Get the next window handle
tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
Loop
End Function

- Sub Command1_Click()
Dim hInst As Long ' Instance handle from Shell function.
Dim hWndApp As Long ' Window handle from GetWinHandle.
Dim buffer As String ' Holds caption of Window.
Dim numChars As Integer ' Count of bytes returned.

' Shell to an application
hInst = Shell("Connect to webmail.uwec.edu")

' Begin search for handle
hWndApp = GetWinHandle(hInst)

If hWndApp <> 0 Then
' Init buffer
buffer = Space$(128)

sendkeys "[username]"
sendkeys "{TAB}"
sendkeys "[password]"
sendkeys "{ENTER}"

End If
End Sub

*note if the window isnt open, you may have to shell IExplorer and input the address in the title bar first.*


----------



## muellerj (Jan 20, 2004)

Thanks alot, the reason I want to do it with VB is that my roomates use the computer to check the same webmail system so I cannot just use the "remember" check box. Also, I am learning to program vb so I thought it may be a fun project. I have set it up with VBScript using sendkeys.


----------

