Variable de bloc With non définie

bernardrustrel

XLDnaute Occasionnel
Bonjour
Dans le fichier joint les 2 boutons ouvre chacun un formulaire. Chacun de ces derniers possède un label (LabelHeure) ou sera positionnée la date et l'heure.

Une macro "ExecutionTimer" permet cela, cependant je passe par un If.... pour identifier l'un ou l'autre des formulaire

J'aimerais pouvoir utiliser un autre moyen soit la macro "Execution", laquelle malheureusement fait apparaitre le message suivant à son exécution:

"Erreur 91
Variable objet ou variable de bloc With non définie"


Aussi je fais appel à vos compétences car je tourne ne rond sur ce sujet depuis un bout de temps
Merci par avance, cordialement. Bernard
 

Pièces jointes

  • Teste.xlsm
    26.5 KB · Affichages: 31

Dranreb

XLDnaute Barbatruc
Bonjour.
Essayez ça :
Dans un module standard nommer MPlanificateur :
VB:
Option Explicit
Private P As Planification
Public Sub LancerPlanif(ByVal Source As Planification, ByVal Heure As Date)
Set P = Source
Application.OnTime Heure, "OnTime_Joue"
End Sub
Public Sub AnnulerPlanif(ByVal Heure As Date)
On Error Resume Next
Application.OnTime Heure, "OnTime_Joue", Schedule:=False
Set P = Nothing
End Sub
Public Sub OnTime_Joue()
If P Is Nothing Then Exit Sub
P.OnTime_Joue_DécrèteÉchoit
Set P = Nothing
End Sub
Dans un module de classe nommé Planification :
VB:
Option Explicit
Event Échoit()
Private HeureOT As Date
Public Sub PlanifierDans(ByVal NbSec As Long)
HeureOT = Now + TimeSerial(0, 0, NbSec)
MPlanificateur.LancerPlanif Me, HeureOT
End Sub
Public Sub OnTime_Joue_DécrèteÉchoit()
HeureOT = 0: RaiseEvent Échoit
End Sub
Public Sub Annuler()
If HeureOT = 0 Then Exit Sub
MPlanificateur.AnnulerPlanif HeureOT
HeureOT = 0
End Sub
Dans les UserForm où vous voulez l'utiliser :
VB:
Private WithEvents RaffrH As Planification
Private Sub UserForm_Activate()
Set RaffrH = New Planification
RaffrH.PlanifierDans 1
End Sub
Private Sub RaffrH_Échoit()
RaffrH.PlanifierDans 1
LblHeure.Value = Format(Now, "dddd dd-mmm-yyyy hh:mm:ss")
End Sub
Private Sub UserForm_Deactivate()
RaffrH.Annuler
End Sub
 

bernardrustrel

XLDnaute Occasionnel
Bonjour.
Essayez ça :
Dans un module standard nommer MPlanificateur :
VB:
Option Explicit
Private P As Planification
Public Sub LancerPlanif(ByVal Source As Planification, ByVal Heure As Date)
Set P = Source
Application.OnTime Heure, "OnTime_Joue"
End Sub
Public Sub AnnulerPlanif(ByVal Heure As Date)
On Error Resume Next
Application.OnTime Heure, "OnTime_Joue", Schedule:=False
Set P = Nothing
End Sub
Public Sub OnTime_Joue()
If P Is Nothing Then Exit Sub
P.OnTime_Joue_DécrèteÉchoit
Set P = Nothing
End Sub
Dans un module de classe nommé Planification :
VB:
Option Explicit
Event Échoit()
Private HeureOT As Date
Public Sub PlanifierDans(ByVal NbSec As Long)
HeureOT = Now + TimeSerial(0, 0, NbSec)
MPlanificateur.LancerPlanif Me, HeureOT
End Sub
Public Sub OnTime_Joue_DécrèteÉchoit()
HeureOT = 0: RaiseEvent Échoit
End Sub
Public Sub Annuler()
If HeureOT = 0 Then Exit Sub
MPlanificateur.AnnulerPlanif HeureOT
HeureOT = 0
End Sub
Dans les UserForm où vous voulez l'utiliser :
VB:
Private WithEvents RaffrH As Planification
Private Sub UserForm_Activate()
Set RaffrH = New Planification
RaffrH.PlanifierDans 1
End Sub
Private Sub RaffrH_Échoit()
RaffrH.PlanifierDans 1
LblHeure.Value = Format(Now, "dddd dd-mmm-yyyy hh:mm:ss")
End Sub
Private Sub UserForm_Deactivate()
RaffrH.Annuler
End Sub
 

Dranreb

XLDnaute Barbatruc
Bonjour.
Pour ma part j'ai un peu changé le module MPlanificateur :
VB:
Option Explicit
Private Planifications As New Collection
Public Sub LancerPlanif(ByVal Source As Planification)
Source.IdtPlanif = Rnd * &H100000
Application.OnTime Source.HeureOT, "'OnTime_Joue """ & Source.IdtPlanif & """'"
Planifications.Add Source, Source.IdtPlanif
End Sub
Public Sub StopperPlanif(ByVal Source As Planification)
If Source.IdtPlanif = "" Then Exit Sub
Application.OnTime Source.HeureOT, "'OnTime_Joue """ & Source.IdtPlanif & """'", Schedule:=False
Planifications.Remove Source.IdtPlanif: Source.IdtPlanif = ""
End Sub
Public Sub OnTime_Joue(ByVal IdtPlanif As String)
Dim Source As Planification
On Error Resume Next: Set Source = Planifications(IdtPlanif): If Err Then Exit Sub
On Error GoTo 0: Planifications.Remove IdtPlanif: Source.IdtPlanif = ""
Source.OnTime_Joue_DécrèteÉchoit
End Sub
Et le module de classe Planification :
VB:
Option Explicit
Public HeureOT As Date, IdtPlanif As String
Event Échoit()
Public Sub PlanifierDans(ByVal NbSec As Long)
HeureOT = Now + TimeSerial(0, 0, NbSec)
MPlanificateur.LancerPlanif Me
End Sub
Public Sub Déplanifier()
MPlanificateur.StopperPlanif Me
End Sub
Public Sub OnTime_Joue_DécrèteÉchoit()
RaiseEvent Échoit
End Sub
Sinon, pour la solution rapidement aperçue de PierreJean, que je salue, j'avais juste remarqué qu'il n'était probablement pas nécessaire de prendre note par un Set de tout l'UserForm dans une variable As Object: il suffirait de noter le Label dans une variable As MSForms.Label
 

Discussions similaires

Réponses
7
Affichages
1 K

Statistiques des forums

Discussions
312 083
Messages
2 085 187
Membres
102 809
dernier inscrit
Sandrine83