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



garyfulcherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

garyfulcherAuthor Commented:
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



EE_AutoDeleterCommented:
garyfulcher,
Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.

EE_AutoDeleter

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.