rplazzotta
New Member
- Joined
- Oct 28, 2021
- Messages
- 41
- Office Version
- 2019
- 2010
- Platform
- Windows
Can anyyone tell me why the following Class (URLEvents) code raises error 1004?
Option Explicit
Public WithEvents ThisURL As MSForms.Image
Private SubName As String
Private Sub ThisURL_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 2 Then 'right-click
' Do the inputbox stuff here then run the relevant _click sub to open the URL; work in progress
Else 'left-click
'MsgBox "Mainform.IsL1 is " & MainForm.IsL1 'works because boolean variable IsL1 is declared Püblic in MainForm
SubName = "MainForm." & ThisURL.Name & "_Click()"
Application.Run SubName '**** This raises error 1004,
'even though all the _Click subs are declared Public in MainForm
'BUT: if I hardcode the name of the Sub in MainForm, it works
'e.g. MainForm.GoogleEN.Click() works !
End If
End Sub
In MainForm's header:
Dim mURLEvents As Collection
Dim cURLEvents As URLEvents
In MainForm's initialize code (excerpt) :
Dim oCtrl As MSForms.Control
Set mURLEvents = New Collection
For Each oCtrl In Me.Controls
If TypeName(oCtrl) = "Image" And ((oCtrl.Top > 418 And oCtrl.Top < 442) Or Left(oCtrl.Name, 6) = "Google") Then
Set cURLEvents = New URLEvents
Set cURLEvents.ThisURL = oCtrl
mURLEvents.Add cURLEvents
End If
...
Next
Option Explicit
Public WithEvents ThisURL As MSForms.Image
Private SubName As String
Private Sub ThisURL_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Button = 2 Then 'right-click
' Do the inputbox stuff here then run the relevant _click sub to open the URL; work in progress
Else 'left-click
'MsgBox "Mainform.IsL1 is " & MainForm.IsL1 'works because boolean variable IsL1 is declared Püblic in MainForm
SubName = "MainForm." & ThisURL.Name & "_Click()"
Application.Run SubName '**** This raises error 1004,
'even though all the _Click subs are declared Public in MainForm
'BUT: if I hardcode the name of the Sub in MainForm, it works
'e.g. MainForm.GoogleEN.Click() works !
End If
End Sub
In MainForm's header:
Dim mURLEvents As Collection
Dim cURLEvents As URLEvents
In MainForm's initialize code (excerpt) :
Dim oCtrl As MSForms.Control
Set mURLEvents = New Collection
For Each oCtrl In Me.Controls
If TypeName(oCtrl) = "Image" And ((oCtrl.Top > 418 And oCtrl.Top < 442) Or Left(oCtrl.Name, 6) = "Google") Then
Set cURLEvents = New URLEvents
Set cURLEvents.ThisURL = oCtrl
mURLEvents.Add cURLEvents
End If
...
Next