-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodAPPUDFsRegistration.bas
More file actions
317 lines (245 loc) · 11.7 KB
/
Copy pathmodAPPUDFsRegistration.bas
File metadata and controls
317 lines (245 loc) · 11.7 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
Attribute VB_Name = "modAPPUDFsRegistration"
' ==========================================
' SISTEMA DE AUTO-REGISTRO Y DESREGISTRO DE UDFs EN EXCEL
' ==========================================
' Este módulo implementa un sistema completo para:
' - Detectar funciones UDF en el proyecto VBA
' - Registrar y desregistrar UDFs en Excel
' - Persistir el estado en el Registro de Windows
' - Soportar fallback dinámico ante fallos
'
' Se apoya en ParsearProcsDelProyecto() y clsVBAProcedure
' ==========================================
'FIXME: revisarlo. Las clases clsVBAProcedure.cls, modAPPUDFsRegistration.bas y modUTILSProcedureParsing.bas Pretenden
' - la identificación de todos los procedimientos en el proyecto VBA (clsVBAProcedure.cls y modUTILSProcedureParsing.bas)
' - la identificación de todas las funciones Que pudieran ser udfs para registrarlas en la carga del XLM (modAPPUDFsRegistration.bas).
'
' [DONE 2026-01-01] Extendido el reconocimiento de atributos de documentacion:
' - Soporta formatos: '@Tag: Valor', '@Tag("Valor")', '@Tag "Valor"'
' - Nuevos atributos: @Example, @Raises, @Throws, @Dependencies
' - Ver clsVBAProcedure.ParsearMetadataCompleta() para detalles
'@Folder "1-Aplicacion.2-Gestion de modulos y procs"
Option Explicit
Private Const MODULE_NAME As String = "modAPPUDFsRegistration"
Private bVerbose As Boolean
' ---------------------------------------------------------------------
' PUNTOS DE ENTRADA PÚBLICOS
' ---------------------------------------------------------------------
'@Description: Punto de entrada sin parámetros para auto-registrar todas las UDFs del proyecto.
'@Scope: Manipula el registro de funciones UDF en Excel.
'@ArgumentDescriptions: (sin argumentos)
'@Returns: (ninguno)
'@Category: Registro UDF
Sub AutoRegistrarTodasLasUDFsNOPARAMS()
Attribute AutoRegistrarTodasLasUDFsNOPARAMS.VB_ProcData.VB_Invoke_Func = " \n0"
Call AutoRegistrarTodasLasUDFs
End Sub
'@Description: Auto-registra todas las funciones UDF del proyecto VBA, opcionalmente filtrando por metadatos.
'@Scope: Analiza procedimientos del proyecto y registra funciones UDF en Excel.
'@ArgumentDescriptions: bOnlyWithMetadata: Solo registrar UDFs con metadatos explícitos | bVerbose_: Mostrar salida detallada en ventana Inmediato
'@Returns: (ninguno)
'@Category: Registro UDF
Public Sub AutoRegistrarTodasLasUDFs(Optional bOnlyWithMetadata As Boolean = False, Optional bVerbose_ As Boolean = False)
Attribute AutoRegistrarTodasLasUDFs.VB_ProcData.VB_Invoke_Func = " \n0"
On Error GoTo ErrorHandler
Dim funciones As Object
Dim metadata As clsVBAProcedure
Dim key As Variant
bVerbose = bVerbose_
' Parsear todas las UDFs del proyecto
Set funciones = ParsearProcsDelProyecto()
If funciones.Count > 0 Then
Application.ScreenUpdating = False
ThisWorkbook.Activate
' Registrar cada función
For Each key In funciones
Set metadata = funciones.Item(key)
If metadata.ProcedureType <> udf Then
funciones.Remove key
ElseIf (Not bOnlyWithMetadata Or metadata.HasMetadata) Then
If Not RegistrarUDF(metadata) Then funciones.Remove key
End If
Next key
Application.ScreenUpdating = True
' Persistir lista para desinstalación posterior
Call GuardarListaFuncionesRegistradas(funciones)
LogInfo "modAPPUDFsRegistration", "[AutoRegistrarTodasLasUDFs] " & funciones.Count & " funciones registradas correctamente."
Else
LogWarning "modAPPUDFsRegistration", "[AutoRegistrarTodasLasUDFs] No se encontraron funciones UDF válidas."
End If
Exit Sub
ErrorHandler:
LogCurrentError "modAPPUDFsRegistration", "[AutoRegistrarTodasLasUDFs]"
End Sub
'@Description: Desregistra todas las UDFs previamente registradas utilizando la información persistida.
'@Scope: Elimina registros de funciones UDF en Excel.
'@ArgumentDescriptions: bVerbose_: Mostrar salida detallada en ventana Inmediato
'@Returns: (ninguno)
'@Category: Registro UDF
Public Sub DesregistrarTodasLasUDFs(Optional bVerbose_ As Boolean = False)
Attribute DesregistrarTodasLasUDFs.VB_ProcData.VB_Invoke_Func = " \n0"
On Error GoTo ErrorHandler
Dim listaFunciones As String
Dim funciones() As String
Dim i As Long
bVerbose = bVerbose_
' Obtener lista persistida
listaFunciones = ObtenerListaFuncionesRegistradas()
If listaFunciones <> "" Then
funciones = Split(listaFunciones, "|")
For i = LBound(funciones) To UBound(funciones)
If Trim(funciones(i)) <> "" Then
DesregistrarUDF funciones(i)
End If
Next i
' Limpiar lista guardada
BorrarListaFuncionesRegistradas
LogInfo "modAPPUDFsRegistration", "[DesregistrarTodasLasUDFs] : " & (UBound(funciones) + 1) & " funciones desregistradas."
Else
LogWarning "modAPPUDFsRegistration", "[DesregistrarTodasLasUDFs] No hay lista guardada, intentando desregistro manual."
' Fallback: desregistrar todas las funciones encontradas ahora
DesregistrarTodasLasFuncionesActuales
End If
Exit Sub
ErrorHandler:
LogCurrentError "modAPPUDFsRegistration", "[DesregistrarTodasLasUDFs]"
' Intentar desregistro manual como respaldo
DesregistrarTodasLasFuncionesActuales
End Sub
' ---------------------------------------------------------------------
' REGISTRO Y DESREGISTRO INDIVIDUAL DE UDFs
' ---------------------------------------------------------------------
'@Description: Registra una función UDF individual en Excel usando sus metadatos.
'@Scope: Llama a Application.MacroOptions para registrar funciones.
'@ArgumentDescriptions: metadata: Objeto clsVBAProcedure con información de la función
'@Returns: Boolean | True si el registro fue exitoso
'@Category: Registro UDF
Private Function RegistrarUDF(metadata As clsVBAProcedure) As Boolean
On Error Resume Next
Dim strDescription As String
Dim argArray() As String
Dim i As Long
strDescription = metadata.Description
If metadata.Scope <> "" Then strDescription = strDescription & ". Aplica a: " & metadata.Scope
If metadata.Returns <> "" Then strDescription = strDescription & ". Devuelve: " & metadata.Returns
strDescription = Replace(strDescription, "..", ".")
strDescription = Left("[" & metadata.Module & "] " & strDescription, 255)
If metadata.ArgumentDescriptions <> "" Then
' Procesar argumentos separados por "|"
argArray = Split(metadata.ArgumentDescriptions, "|")
For i = LBound(argArray) To UBound(argArray)
argArray(i) = Trim(argArray(i))
Next i
' Registrar con argumentos ("'" & APP_NAME & ".xlam'!" & ?)
Application.MacroOptions _
Macro:=metadata.Name, _
Description:=strDescription, _
Category:=metadata.Category, _
ArgumentDescriptions:=argArray
Else
' Registrar sin descripción de argumentos
Application.MacroOptions _
Macro:=metadata.Name, _
Description:=strDescription, _
Category:=metadata.Category
End If
If Err.Number <> 0 Then
LogError "modAPPUDFsRegistration", "[RegistrarUDF] Error registrando '" & metadata.Name & "'", , Err.Description
Else
If bVerbose Then LogInfo "modAPPUDFsRegistration", "[RegistrarUDF] Registrada: " & metadata.Name
RegistrarUDF = True
End If
On Error GoTo 0
End Function
'@Description: Elimina el registro de una función UDF individual en Excel.
'@Scope: Limpia metadatos de una función registrada.
'@ArgumentDescriptions: nombreFuncion: Nombre de la función UDF
'@Returns: Boolean | True si se desregistró correctamente
'@Category: Registro UDF
Private Function DesregistrarUDF(nombreFuncion As String) As Boolean
On Error Resume Next
Application.MacroOptions _
Macro:=Trim(nombreFuncion), _
Description:=Empty, _
Category:=Empty
If Err.Number = 0 Then
If bVerbose Then LogInfo "modAPPUDFsRegistration", "[DesregistrarUDF] Desregistrada: " & nombreFuncion
DesregistrarUDF = True
End If
On Error GoTo 0
End Function
' ---------------------------------------------------------------------
' PERSISTENCIA EN REGISTRO DE WINDOWS
' ---------------------------------------------------------------------
'@Description: Guarda en el registro de Windows la lista de UDFs registradas.
'@Scope: Escribe valores REG_SZ en el registro del sistema.
'@ArgumentDescriptions: funciones: Dictionary con objetos clsVBAProcedure
'@Returns: (ninguno)
'@Category: Persistencia
Private Sub GuardarListaFuncionesRegistradas(funciones As Object)
Dim lista As Variant
Dim key As Variant
Dim metadata As clsVBAProcedure
For Each key In funciones.Keys()
Set metadata = funciones(key)
If lista <> "" Then lista = lista & "|"
lista = lista & metadata.Name
Next key
On Error Resume Next
CreateObject("WScript.Shell").RegWrite CFG_RUTA_UDFS, lista, "REG_SZ"
If Err.Number <> 0 Then
LogError "modAPPUDFsRegistration", "[DesregistrarUDF] No se pudo guardar lista en registro", , Err.Description
Else
If bVerbose Then LogInfo "modAPPUDFsRegistration", "[DesregistrarUDF] Lista de UDFs guardada en registro."
End If
On Error GoTo 0
End Sub
'@Description: Obtiene del registro de Windows la lista de UDFs registradas.
'@Scope: Lee valores REG_SZ del registro del sistema.
'@ArgumentDescriptions: (sin argumentos)
'@Returns: String | Lista de nombres de funciones separadas por "|"
'@Category: Persistencia
Private Function ObtenerListaFuncionesRegistradas() As String
On Error Resume Next
ObtenerListaFuncionesRegistradas = CreateObject("WScript.Shell").RegRead(CFG_RUTA_UDFS)
If Err.Number <> 0 Then
ObtenerListaFuncionesRegistradas = ""
LogError "modAPPUDFsRegistration", "[ObtenerListaFuncionesRegistradas] No se encontró lista guardada en registro."
End If
On Error GoTo 0
End Function
'@Description: Elimina del registro de Windows la lista de UDFs almacenada.
'@Scope: Borra valores del registro del sistema.
'@ArgumentDescriptions: (sin argumentos)
'@Returns: (ninguno)
'@Category: Persistencia
Private Sub BorrarListaFuncionesRegistradas()
On Error Resume Next
CreateObject("WScript.Shell").RegDelete CFG_RUTA_UDFS
If Err.Number = 0 Then
LogInfo "modAPPUDFsRegistration", "[BorrarListaFuncionesRegistradas] Lista de UDFs eliminada del registro."
End If
On Error GoTo 0
End Sub
' ---------------------------------------------------------------------
' FALLBACK: DESREGISTRO DINÁMICO
' ---------------------------------------------------------------------
'@Description: Desregistra dinámicamente todas las funciones encontradas en el proyecto.
'@Scope: Analiza procedimientos actuales y elimina su registro.
'@ArgumentDescriptions: (sin argumentos)
'@Returns: (ninguno)
'@Category: Registro UDF
Private Sub DesregistrarTodasLasFuncionesActuales()
Dim funciones As Object
Dim key As Variant
Dim Count As Long
Set funciones = ParsearProcsDelProyecto()
For Each key In funciones
If DesregistrarUDF(funciones(key).Name) Then Count = Count + 1
Next key
If Count > 0 Then
If bVerbose Then LogInfo "modAPPUDFsRegistration", "[DesregistrarTodasLasFuncionesActuales] Desregistro dinámico: " & Count & " funciones procesadas."
Else
LogInfo "modAPPUDFsRegistration", "[DesregistrarTodasLasFuncionesActuales] Desregistro dinámico: No se encontraron funciones para desregistrar."
End If
End Sub