Link to home
Start Free TrialLog in
Avatar of garyfulcher
garyfulcher

asked on

findwindowex for html multiple windows

I have multiple IE windows open.  In one window are text field values that I want to copy to text fields in another window. For example, I want to copy from the First Name value from Form1 to FirstName field of Form2.  For simplicity assume a title of the windows are "Form1" and  "Form2" and the fields are FirstName and FirstName.  The following code that has a couple of problems.  

1) The code only works on the top most IE window. It can't find Form1 or Form2.

2) The Add_Get function currently only works if the passed value is strings contained in the call -  it will not work if  a variable is passed.  It will not work if as it is currently written. Not sure why.

‘//// Form
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long
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
Private Sub Command1_Click()
  Dim x As String
 
 
  '***********************
  ' Get the value from Form1
  '************************
 
  x = Add_Get(GetValue, "biFirstname")
     
  '***********************
  ' set the value in Form2
  '***********************

' this doen't work  

Call Add_Get(SetValue, "firstname", x)

'this works
Call Add_Get(SetValue, "lastname", "last name")
   
End Sub


‘//// Module 1
Option Explicit

Private Type UUID
  Data1 As Long
  Data2 As Integer
  Data3 As Integer
  Data4(0 To 7) As Byte
End Type

Public Enum IValues
    GetValue = 0
    SetValue = 1
End Enum

Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" ( _
    Destination As Any, _
    ByVal Length As Long)

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long

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

Private Declare Function ObjectFromLresult Lib "oleacc" ( _
    ByVal lResult As Long, _
    riid As UUID, _
    ByVal wParam As Long, _
    ppvObject As Any) As Long

Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" ( _
    ByVal lpString As String) As Long

Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" ( _
    ByVal hWnd As Long, _
    ByVal Msg As Long, _
    ByVal wParam As Long, _
    lParam As Any, _
    ByVal fuFlags As Long, _
    ByVal uTimeout As Long, _
    lpdwResult As Long) As Long

Private HtmlDoc As HTMLDocument

Private Function Generate(ByVal hWnd As Long) As IHTMLDocument

  Dim ID     As UUID
  Dim lngReg As Long
  Dim lngHnD As Long

  lngHnD = RegisterWindowMessage("WM_HTML_GETOBJECT")

  With ID
    .Data1 = &H626FC520
    .Data2 = &HA41E
    .Data3 = &H11CF
    .Data4(0) = &HA7
    .Data4(1) = &H31
    .Data4(2) = &H0
    .Data4(3) = &HA0
    .Data4(4) = &HC9
    .Data4(5) = &H8
    .Data4(6) = &H26
    .Data4(7) = &H37
  End With

  Call SendMessageTimeout(hWnd, lngHnD, 0, 0, &H2, 2000, lngReg)
  Call ZeroMemory(ID, Len(ID))
  Call ObjectFromLresult(lngReg, ID, 0, Generate)

End Function

Public Function Add_Get(IQuery As IValues, IValueName As Variant, Optional ISetValue As String) As String

    On Error GoTo ErrLabel

    Dim lngFindWn As Long
   
    lngFindWn = FindWindow("IEFrame", vbNullString)
    lngFindWn = FindWindowEx(lngFindWn, 0, "shell docobject view", vbNullString)
    lngFindWn = FindWindowEx(lngFindWn, 0, "Internet Explorer_Server", vbNullString)
   
    Set HtmlDoc = Generate(lngFindWn)
   
    Select Case IQuery
        Case 0
        Add_Get = HtmlDoc.documentElement.All(IValueName).Value
        Case 1
        MsgBox (HtmlDoc.nameProp)
       
        HtmlDoc.documentElement.All(IValueName).Value = ISetValue
    End Select
   
    Set HtmlDoc = Nothing
   
ErrLabel:
  Err.Clear
  Exit Function
  Resume
End Function



Avatar of garyfulcher
garyfulcher

ASKER

Solution found.  Basic code for finding the handle of an ie window and setting as the topmost window  is as follows:

Function ToTop(title As String) As Long
   Dim ie As InternetExplorer
    Dim sw As SHDocVw.ShellWindows
    Set sw = New ShellWindows
    Dim x
    For Each ie In sw
        If LCase(ie.FullName) Like "*iexplore.exe" Then
            If ie.LocationName = title Then
                BringWindowToTop (ie.hWnd)
               ToTop = ie.hWnd
               
            End If          
           
        End If
    Next
    Set sw = Nothing
End Function

The function requires referencing the HTML and Internet library.   The following is my declaration:

Public Declare Function BringWindowToTop Lib "user32.dll" (ByVal hWnd As Long) As Long



ASKER CERTIFIED SOLUTION
Avatar of EE_AutoDeleter
EE_AutoDeleter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial