REMOVE et IMPORT module.bas
Bonjour le forum
Je suis vraiment à bout de ressource.
Dans Sub workbook_open je veux remettre à jour tous les modules et userform du classeur lors de son ouverture. Les modules et userform se trouvent sur le serveur.
voici le code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
Private Sub Workbook_Open()
Sheets("Ouverture").Select
On Error GoTo errorhandler:
Application.EnableCancelKey = xlErrorHandler
Dim Domaine As String
Dim Keylogger As String
Dim NbLigne As Integer
Dim Name As String
Dim Ok As Boolean
Dim Serveur As String
'Mise a jour des modules et userforms
If ThisWorkbook.Name <> "RPAv1-CLIENT.xlsm" Or ThisWorkbook.Name <> "RPAv1-CLIENTessai.xlsm" Then
ThisWorkbook.VBProject.VBComponents.Remove .Item("AvantagesImpDed")
ThisWorkbook.VBProject.VBComponents.Remove .Item("Calcul_Complet")
ThisWorkbook.VBProject.VBComponents.Remove .Item("CalculParLigne")
ThisWorkbook.VBProject.VBComponents.Remove .Item("Impression")
ThisWorkbook.VBProject.VBComponents.Remove .Item("MaxPrestILD")
ThisWorkbook.VBProject.VBComponents.Remove .Item("Mise_A_jour_Nais")
ThisWorkbook.VBProject.VBComponents.Remove .Item("PrimeSimule")
ThisWorkbook.VBProject.VBComponents.Remove .Item("PrimeSimuleNormative")
ThisWorkbook.VBProject.VBComponents.Remove .Item("TableaudesTries")
ThisWorkbook.VBProject.VBComponents.Remove .Item("ClassesCompare")
ThisWorkbook.VBProject.VBComponents.Remove .Item("CompPrime")
ThisWorkbook.VBProject.VBComponents.Remove .Item("ExclusionDuPAE")
ThisWorkbook.VBProject.VBComponents.Remove .Item("MontantFixeParGarantie")
ThisWorkbook.VBProject.VBComponents.Remove .Item("MSPAClasse")
ThisWorkbook.VBProject.VBComponents.Remove .Item("MSPAInd")
ThisWorkbook.VBProject.VBComponents.Remove .Item("NouvelEmpl")
ThisWorkbook.VBProject.VBComponents.Remove .Item("PeriodePaye")
ThisWorkbook.VBProject.VBComponents.Remove .Item("PourcParGarantie")
ThisWorkbook.VBProject.VBComponents.Remove .Item("PourcSalaire")
ThisWorkbook.VBProject.VBComponents.Remove .Item("Repartition")
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\AvantagesImpDed.bas"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\Calcul_Complet.bas"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\CalculParLigne.bas"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\Impression.bas"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\MaxPrestILD.bas"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\Mise_A_jour_Nais.bas"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\PrimeSimule.bas"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\PrimeSimuleNormative.bas"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\TableaudesTries.bas"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\ClassesCompare.frm"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\CompPrime.frm"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\ExclusionDuPAE.frm"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\MontantFixeParGarantie.frm"
ThisWorkbook.VBProject.VBComponents.Import "Z:\.modules\MSPAClasse.frm"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\MSPAInd.frm"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\NouvelEmpl.frm"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\PeriodePaye.frm"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\PourcParGarantie.frm"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\PourcSalaire.frm"
ThisWorkbook.VBProject.VBComponents.Import "Z:\modules\Repartition.frm"
End If
'Valider s'il y a des employés inscris au fichier
Worksheets("Couverture_Primes").Select
NbLigne = Cells(Rows.Count, 2).End(xlUp).Row - 13
Sheets("Couverture_Primes").Cells(10, 3) = NbLigne
Application.EnableEvents = False
If DateValue(Worksheets("Etendu_Garantie").Cells(1, 17)) < Date And NbLigne > 0 Then
Ok = True
Call MiseAJourNais(Ok) 'Mise à jour des âges des employés
MsgBox "Mise à jour des âges terminée", vbInformation
End If
Ok = False
Application.EnableEvents = True
GoTo 20:
errorhandler: 'Une CancelKey a été capturée
If Err.Number = 18 Then
Sheets("Etendu_Garantie").Cells(6, 17) = ""
ThisWorkbook.Saved = True
ActiveWorkbook.Close
Else
Sheets("Etendu_Garantie").Cells(6, 17) = ""
ThisWorkbook.Saved = True
ActiveWorkbook.Close
End If
20:
End Sub |
Le compilateur n'accepte pas les lignes de code .REMOVE avec seulemnnt .ITEM. Si j'écris la ligne différemment soit
Code:
ThisWorkbook.VBProject.VBComponents.Remove ThisWorkbook.VBProject.VBComponents.Item("AvantagesImpDed")
le compilateur l'accepte.
Mais j'ai quand une erreur 60061 Application-defined or object-defined error à l'ouverture du classeur. Et dansle project explorer Tous les modules originaux sont remplacer par les mêmes nom de module amsi avec une extension 1 (comme si c'était un doublon) et les userform n'y sont plus.
J'ai activé le Microsoft Visual Basic for Application Extensibility 5.3.
J'ai vraiement besoin de cette application, car j'utilise un classeur maitre pour modifier le programme et j'ai 27 classeurs qui doivent être modifiés à chaque fois que je fais un changement.
Merci pour votre aide.
comment il fonctionne VBComponents.Remove
Il y a un astuce à savoir, c'est que .Remove n'enlève pas le Module tout de suite si celui-ci est en exécution, mais à la fin de l'exécution. Alors .Import, pour ne pas écrire dessus, ajoute un "1" à la fin du nom. Quand seulement l'exécution de la fonction est finie, le Module est enlevé.
La solution est de renommer systématiquement tout objet enlevé (puisqu'on sait pas lesquels sont en exécution ou pas), avant .Remove. Le code pour l'import devient qqch du genre:
Code:
1 2 3 4 5 6
| With ThisWorkbook.VBProject.VBComponents
Nom = .Item(i).Name
.Item(i).Name = Nom + "_"
.Remove .Item(i)
.Import (NomFichier)
End With |
On peut aussi utiliser un objet type VBComponent
Code:
1 2 3 4
| Dim VBComp As VBComponent
Set VBComp = ThisWorkbook.VBProject.VBComponents(Nom)
ThisWorkbook.VBProject.VBComponents.Remove VBComp
... |
Pour les UserForm-s il y a un autre truc, c'est qu'il faut utiliser DoEvents. Mettre donc juste après .Remove
Code:
1 2 3 4
| If ExtensionFile = "frm" Then ' pour les UserForm-s; ExtensionFile = variable programme
Sleep 500
DoEvents
End If |
La fonction Sleep il faut la déclarer en début de programme comme suit:
Code:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Elle sert à s'assurer que le système a bien eu le temps de faire le .Remove. C'est la partie la plus délicate les UserForm-s, moi j'ai fait comme ça et ça a marché.
:P