Jaafar Tribak
Well-known Member
- Joined
- Dec 5, 2002
- Messages
- 9,779
- Office Version
- 2016
- Platform
- Windows
Hi all and Happy new year.
I have seen this question asked many times before but surprisingly enough there is no direct method to access the Properties/Methods of a remote userform and its controls at runtime..... I needed this in a small project recently and I thought I would post here the solution I came up with as this may prove useful to others.
1- Code in the UserForm Module in Workbook A (Server workbook)
2- Code in Worknbook B (Client workbook)
This also works for more than one loaded userform.
I have seen this question asked many times before but surprisingly enough there is no direct method to access the Properties/Methods of a remote userform and its controls at runtime..... I needed this in a small project recently and I thought I would post here the solution I came up with as this may prove useful to others.
1- Code in the UserForm Module in Workbook A (Server workbook)
Code:
Option Explicit
Private Declare Function SetProp Lib "user32" Alias "SetPropA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal hData As Long) As Long
Private Declare Function RemoveProp Lib "user32" Alias "RemovePropA" _
(ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private hwnd As Long
Private Sub UserForm_Initialize()
hwnd = FindWindow(vbNullString, Me.Caption)
RemoveProp hwnd, "FrmPointer"
SetProp hwnd, "FrmPointer", ObjPtr(Me)
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
RemoveProp hwnd, "FrmPointer"
End Sub
2- Code in Worknbook B (Client workbook)
Code:
Option Explicit
Private Declare Function GetProp Lib "user32" Alias "GetPropA" _
(ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" (Destination As Any, Source As Any, _
ByVal Length As Long)
Sub Test()
[B][COLOR=#008000]'\\ Get a pointer to the remote UForm and manipulate[/COLOR][/B]
[B][COLOR=#008000] '\\ its Properties/Methods/Controls.[/COLOR][/B]
Dim oRemoteUFrm As Object
Set oRemoteUFrm = GetForm(Frm_Name:="UserForm1")
If Not oRemoteUFrm Is Nothing Then
With oRemoteUFrm
.BackColor = vbBlue
.Width = oRemoteUFrm.Width * 1.5
.Left = 10
End With
Else
MsgBox " 'UserForm1' not loaded."
End If
Set oRemoteUFrm = Nothing
End Sub
[B][COLOR=#008000]'\\ Function that gets a pointer to the remote UForm.[/COLOR][/B]
Function GetForm(ByVal Frm_Name As String) As Object
Dim hwnd As Long, lPtr As Long
Dim oTempFrm As Object
hwnd = FindWindow(vbNullString, Frm_Name)
If hwnd Then
lPtr = GetProp(hwnd, "FrmPointer")
CopyMemory oTempFrm, lPtr, 4
Set GetForm = oTempFrm
CopyMemory oTempFrm, 0&, 4
Set oTempFrm = Nothing
End If
End Function
This also works for more than one loaded userform.