-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodMACROProceduresToWorksheet.bas
More file actions
656 lines (549 loc) · 26.9 KB
/
Copy pathmodMACROProceduresToWorksheet.bas
File metadata and controls
656 lines (549 loc) · 26.9 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
Attribute VB_Name = "modMACROProceduresToWorksheet"
' ==========================================
' EXTENSIÓN PARA modUTILSProcedureParsing.bas
' SINCRONIZACIÓN BIDIRECCIONAL CON HOJA EXCEL "PROCEDIMIENTOS"
' ==========================================
' Reutiliza completamente la función ParsearProcsDelProyecto() existente
' y todas las propiedades de clsVBAProcedure
' ==========================================
'@Folder "1-Inicio e Instalacion.Gestion de modulos y procs"
Option Explicit
Private Const SHEET_NAME As String = "PROCEDIMIENTOS"
'@Description: Sincroniza procedimientos del proyecto con hoja Excel "PROCEDIMIENTOS". Crea la hoja si no existe, o sincroniza cambios bidireccionales si existe.
'@Scope: Público
'@Category: Sincronización
Public Sub WriteProcedimientosSheet()
Attribute WriteProcedimientosSheet.VB_ProcData.VB_Invoke_Func = " \n0"
Dim ws As Worksheet
Dim funciones As Object
Dim bSheetExisted As Boolean
On Error GoTo ErrorHandler
' Parsear todos los procedimientos del proyecto
Set funciones = ParsearProcsDelProyecto()
If funciones Is Nothing Or funciones.Count = 0 Then
MsgBox "No se encontraron procedimientos para procesar.", vbInformation, "Sin procedimientos"
Exit Sub
End If
' Verificar si existe la hoja
bSheetExisted = SheetExists(ThisWorkbook, SHEET_NAME)
If bSheetExisted Then
' Leer hoja existente y comparar
Set ws = ThisWorkbook.Worksheets(SHEET_NAME)
Call SincronizarConHoja(ws, funciones)
Else
' Crear hoja nueva y volcar datos
Set ws = CrearHojaProcedimientos(ThisWorkbook, SHEET_NAME)
Call VolcarProcedimientosAHoja(ws, funciones)
MsgBox "Hoja '" & SHEET_NAME & "' creada con " & funciones.Count & " procedimientos.", vbInformation, "Hoja creada"
End If
Exit Sub
ErrorHandler:
Debug.Print "[WriteProcedimientosSheet] - Error: " & Err.Description
MsgBox "Error al procesar hoja de procedimientos: " & Err.Description, vbCritical, "Error"
End Sub
' ==========================================
' FUNCIÓN 4: MODIFICACIÓN DE WriteProcedimientosSheet
' ==========================================
'@Description: VERSIÓN MODIFICADA que usa SincronizarConHoja_ConBackup en lugar de SincronizarConHoja
'@Scope: Público
'@Category: Sincronización
Public Sub WriteProcedimientosSheet_ConBackup()
Attribute WriteProcedimientosSheet_ConBackup.VB_ProcData.VB_Invoke_Func = " \n0"
Dim ws As Worksheet
Dim funciones As Object
Dim bSheetExisted As Boolean
On Error GoTo ErrorHandler
' Parsear todos los procedimientos del proyecto
Set funciones = ParsearProcsDelProyecto()
If funciones Is Nothing Or funciones.Count = 0 Then
MsgBox "No se encontraron procedimientos para procesar.", vbInformation, "Sin procedimientos"
Exit Sub
End If
' Verificar si existe la hoja
bSheetExisted = SheetExists(ThisWorkbook, SHEET_NAME)
If bSheetExisted Then
' Leer hoja existente y comparar (CON BACKUP)
Set ws = ThisWorkbook.Worksheets(SHEET_NAME)
Call SincronizarConHoja_ConBackup(ws, funciones) ' ? CAMBIO AQUÍ
Else
' Crear hoja nueva y volcar datos (sin backup necesario)
Set ws = CrearHojaProcedimientos(ThisWorkbook, SHEET_NAME)
Call VolcarProcedimientosAHoja(ws, funciones)
MsgBox "Hoja '" & SHEET_NAME & "' creada con " & funciones.Count & " procedimientos.", vbInformation, "Hoja creada"
End If
Exit Sub
ErrorHandler:
Debug.Print "[WriteProcedimientosSheet_ConBackup] - Error: " & Err.Description
MsgBox "Error al procesar hoja de procedimientos: " & Err.Description, vbCritical, "Error"
End Sub
'@Description: Crea una nueva hoja con encabezados para procedimientos
'@Scope: Privado
'@ArgumentDescriptions: wb: Workbook donde crear | sheetName: Nombre de la hoja
'@Returns: Worksheet | Referencia a la hoja creada
Private Function CrearHojaProcedimientos(wb As Workbook, ByVal sheetName As String) As Worksheet
Dim ws As Worksheet
Set ws = wb.Worksheets.Add
ws.Name = sheetName
Call CrearEncabezadosHoja(ws)
Set CrearHojaProcedimientos = ws
End Function
'@Description: Crea los encabezados de columnas en la hoja de procedimientos
'@Scope: Privado
'@ArgumentDescriptions: ws: Worksheet donde crear encabezados
Private Sub CrearEncabezadosHoja(ws As Worksheet)
With ws
.Cells(1, 1).Value = "Módulo"
.Cells(1, 2).Value = "Firma del procedimiento"
.Cells(1, 3).Value = "Description"
.Cells(1, 4).Value = "Category"
.Cells(1, 5).Value = "Scope"
.Cells(1, 6).Value = "ArgumentDescriptions"
.Cells(1, 7).Value = "Returns"
' Formato de encabezados
With .Range("A1:G1")
.Font.Bold = True
.Interior.Color = RGB(68, 114, 196)
.Font.Color = RGB(255, 255, 255)
.HorizontalAlignment = xlCenter
End With
' Ajustar anchos
.Columns("A:A").ColumnWidth = 20 ' Módulo
.Columns("B:B").ColumnWidth = 50 ' Firma
.Columns("C:C").ColumnWidth = 70 ' Description
.Columns("D:D").ColumnWidth = 25 ' Category
.Columns("E:E").ColumnWidth = 25 ' Scope
.Columns("F:F").ColumnWidth = 40 ' ArgumentDescriptions
.Columns("G:G").ColumnWidth = 30 ' Returns
.Columns("A:G").WrapText = True
End With
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
End Sub
'@Description: Vuelca todos los procedimientos parseados a la hoja Excel
'@Scope: Privado
'@ArgumentDescriptions: ws: Worksheet destino | funciones: Dictionary con objetos clsVBAProcedure
Private Sub VolcarProcedimientosAHoja(ws As Worksheet, funciones As Object)
Dim i As Long, col As Long, fila As Long
Dim proc As clsVBAProcedure
fila = 2 ' Fila inicial (después de encabezados)
For i = 0 To funciones.Count - 1
Set proc = funciones(i)
With ws
.Cells(fila, 1).Value = proc.Module
.Cells(fila, 2).Value = proc.NormalizedSignature
.Cells(fila, 3).Value = proc.Description
.Cells(fila, 4).Value = IIf(proc.Category = DEFAULT_CATEGORY, "", proc.Category)
.Cells(fila, 5).Value = proc.Scope
.Cells(fila, 6).Value = IIf(proc.ArgumentDescriptions = DEFAULT_NOPARAMS, "", proc.ArgumentDescriptions)
.Cells(fila, 7).Value = proc.Returns
' Formato condicional para mejor lectura
If i Mod 2 = 0 Then
.Range(.Cells(fila, 1), .Cells(fila, 7)).Interior.Color = RGB(242, 242, 242)
.Range(.Cells(fila, 1), .Cells(fila, 7)).HorizontalAlignment = xlLeft
.Range(.Cells(fila, 1), .Cells(fila, 7)).VerticalAlignment = xlTop
End If
' enfatiza celdas que tengan diferencias en valores deducidos vs metadatos procesados del proyecto
For col = 3 To 7
If InStr(.Cells(fila, col).Value, "M.D.:") > 0 Then
If i Mod 2 = 0 Then
.Cells(fila, col).Interior.Color = RGB(240, 210, 120)
Else
.Cells(fila, col).Interior.Color = RGB(210, 240, 120)
End If
End If
Next
End With
fila = fila + 1
Next i
' Aplicar bordes
With ws.Range(ws.Cells(1, 1), ws.Cells(fila - 1, 7))
.Borders.LineStyle = xlContinuous
.Borders.Weight = xlThin
End With
End Sub
'@Description: Gestiona la sincronización bidireccional entre hoja Excel y código VBA
'@Scope: Privado
'@ArgumentDescriptions: ws: Worksheet con datos existentes | funciones: Dictionary con procedimientos actuales del código
Private Sub SincronizarConHoja(ws As Worksheet, funciones As Object)
Dim dictHoja As Object ' Dictionary con clave compuesta: Módulo + "|" + Firma -> array de metadatos
Dim dictCodigo As Object ' Dictionary con clave compuesta: Módulo + "|" + Firma -> objeto clsVBAProcedure
Dim hayDiferencias As Boolean
Dim respuesta As VbMsgBoxResult
Dim mensaje As String
' Crear diccionarios para comparación
Set dictHoja = LeerMetadatosDeHoja(ws)
Set dictCodigo = CrearDiccionarioProcedimientos(funciones)
' Comparar y detectar diferencias
hayDiferencias = HayDiferenciasEnMetadatos(dictHoja, dictCodigo, mensaje)
If Not hayDiferencias Then
MsgBox "No hay diferencias entre la hoja y el código." & vbCrLf & vbCrLf & _
"Total procedimientos: " & funciones.Count, vbInformation, "Sincronización"
Exit Sub
End If
' Preguntar al usuario qué hacer
respuesta = MsgBox("Se encontraron diferencias entre la hoja PROCEDIMIENTOS y el código VBA:" & vbCrLf & vbCrLf & _
mensaje & vbCrLf & vbCrLf & _
"¿Desea actualizar el CÓDIGO con los datos de la hoja?" & vbCrLf & vbCrLf & _
"Sí = Actualizar código VBA desde Excel" & vbCrLf & _
"No = Actualizar hoja Excel desde código" & vbCrLf & _
"Cancelar = No hacer nada", _
vbYesNoCancel + vbQuestion, "Sincronizar Metadatos")
Select Case respuesta
Case vbYes
' Actualizar código VBA
If MsgBox("ADVERTENCIA: Se modificarán los archivos de código VBA." & vbCrLf & _
"¿Está seguro de continuar?", vbExclamation + vbYesNo, "Confirmación") = vbYes Then
Stop
Call ActualizarCodigoVBA(dictHoja, dictCodigo)
MsgBox "Código VBA actualizado correctamente." & vbCrLf & _
"Se recomienda revisar los cambios.", vbInformation, "Actualización completada"
End If
Case vbNo
' Actualizar hoja Excel
ws.Cells.Clear
Call CrearEncabezadosHoja(ws)
Call VolcarProcedimientosAHoja(ws, funciones)
MsgBox "Hoja Excel actualizada correctamente con " & funciones.Count & " procedimientos.", _
vbInformation, "Actualización completada"
Case vbCancel
' No hacer nada
MsgBox "Operación cancelada. No se realizaron cambios.", vbInformation, "Operación cancelada"
End Select
End Sub
' ==========================================
' FUNCIÓN 3: MODIFICACIÓN DE SincronizarConHoja
' ==========================================
'@Description: Versión MODIFICADA de SincronizarConHoja que crea backups antes de modificar
'@Scope: Privado
'@ArgumentDescriptions: ws: Worksheet con datos existentes | funciones: Dictionary con procedimientos actuales del código
Private Sub SincronizarConHoja_ConBackup(ws As Worksheet, funciones As Object)
Dim dictHoja As Object
Dim dictCodigo As Object
Dim hayDiferencias As Boolean
Dim respuesta As VbMsgBoxResult
Dim mensaje As String
Dim rutaBackupVBA As String
Dim backupHojaOK As Boolean
' Crear diccionarios para comparación
Set dictHoja = LeerMetadatosDeHoja(ws)
Set dictCodigo = CrearDiccionarioProcedimientos(funciones)
' Comparar y detectar diferencias
hayDiferencias = HayDiferenciasEnMetadatos(dictHoja, dictCodigo, mensaje)
If Not hayDiferencias Then
MsgBox "No hay diferencias entre la hoja y el código." & vbCrLf & vbCrLf & _
"Total procedimientos: " & funciones.Count, vbInformation, "Sincronización"
Exit Sub
End If
' Preguntar al usuario qué hacer
respuesta = MsgBox("Se encontraron diferencias entre la hoja PROCEDIMIENTOS y el código VBA:" & vbCrLf & vbCrLf & _
mensaje & vbCrLf & vbCrLf & _
"¿Desea actualizar el CÓDIGO con los datos de la hoja?" & vbCrLf & vbCrLf & _
"Sí = Actualizar código VBA desde Excel" & vbCrLf & _
"No = Actualizar hoja Excel desde código" & vbCrLf & _
"Cancelar = No hacer nada", _
vbYesNoCancel + vbQuestion, "Sincronizar Metadatos")
Select Case respuesta
Case vbYes
' ============================================
' ACTUALIZAR CÓDIGO VBA (con backup)
' ============================================
' 1. CREAR BACKUP DE CÓDIGO VBA
MsgBox "Creando copia de seguridad del código VBA...", vbInformation, "Backup en proceso"
rutaBackupVBA = CrearBackupCodigoVBA()
If rutaBackupVBA = "" Then
If MsgBox("ADVERTENCIA: No se pudo crear la copia de seguridad del código VBA." & vbCrLf & vbCrLf & _
"¿Desea continuar SIN backup?", vbExclamation + vbYesNo, "Error de Backup") = vbNo Then
Exit Sub
End If
Else
MsgBox "Backup de código VBA creado:" & vbCrLf & rutaBackupVBA, vbInformation, "Backup creado"
End If
' 2. CONFIRMAR Y ACTUALIZAR
If MsgBox("ADVERTENCIA: Se modificarán los archivos de código VBA." & vbCrLf & _
"Backup guardado en: " & IIf(rutaBackupVBA <> "", rutaBackupVBA, "NO DISPONIBLE") & vbCrLf & vbCrLf & _
"¿Está seguro de continuar?", vbExclamation + vbYesNo, "Confirmación") = vbYes Then
Stop
Call ActualizarCodigoVBA(dictHoja, dictCodigo)
MsgBox "Código VBA actualizado correctamente." & vbCrLf & _
"Backup guardado en: " & rutaBackupVBA, vbInformation, "Actualización completada"
End If
Case vbNo
' ============================================
' ACTUALIZAR HOJA EXCEL (con backup)
' ============================================
' 1. CREAR BACKUP DE HOJA EXCEL
backupHojaOK = CrearBackupHojaExcel(ws)
If Not backupHojaOK Then
MsgBox "Operación cancelada. No se pudo crear backup de la hoja.", vbExclamation, "Cancelado"
Exit Sub
End If
' 2. ACTUALIZAR HOJA
ws.Cells.Clear
Call CrearEncabezadosHoja(ws)
Call VolcarProcedimientosAHoja(ws, funciones)
MsgBox "Hoja Excel actualizada correctamente con " & funciones.Count & " procedimientos." & vbCrLf & _
"Backup guardado como: '" & ws.Name & "_bkp'", vbInformation, "Actualización completada"
Case vbCancel
MsgBox "Operación cancelada. No se realizaron cambios.", vbInformation, "Operación cancelada"
End Select
End Sub
'@Description: Lee los metadatos de procedimientos desde la hoja Excel
'@Scope: Privado
'@ArgumentDescriptions: ws: Worksheet de origen
'@Returns: Object | Dictionary con clave compuesta Módulo|Firma -> array(1 To 5) de metadatos
Private Function LeerMetadatosDeHoja(ws As Worksheet) As Object
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim fila As Long
Dim claveCompuesta As String
Dim metadatos(1 To 5) As String
Dim modulo As String, firma As String
fila = 2 ' Primera fila de datos
Do While ws.Cells(fila, 1).Value <> ""
modulo = ws.Cells(fila, 1).Value
firma = ws.Cells(fila, 2).Value
claveCompuesta = modulo & "|" & firma
metadatos(1) = ws.Cells(fila, 3).Value ' Description
metadatos(2) = ws.Cells(fila, 4).Value ' Category
metadatos(3) = ws.Cells(fila, 5).Value ' Scope
metadatos(4) = ws.Cells(fila, 6).Value ' ArgumentDescriptions
metadatos(5) = ws.Cells(fila, 7).Value ' Returns
If Not dict.Exists(claveCompuesta) Then
dict.Add claveCompuesta, metadatos
Else
Debug.Print "[LeerMetadatosDeHoja] - ADVERTENCIA: Procedimiento duplicado en hoja: " & claveCompuesta
End If
fila = fila + 1
Loop
Set LeerMetadatosDeHoja = dict
End Function
'@Description: Crea un diccionario de procedimientos indexado por clave compuesta Módulo|Firma
'@Scope: Privado
'@ArgumentDescriptions: funciones: Dictionary de ParsearProcsDelProyecto
'@Returns: Object | Dictionary con clave compuesta -> clsVBAProcedure
Private Function CrearDiccionarioProcedimientos(funciones As Object) As Object
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim i As Long
Dim proc As clsVBAProcedure
Dim claveCompuesta As String
For i = 0 To funciones.Count - 1
Set proc = funciones(i)
claveCompuesta = proc.Module & "|" & proc.NormalizedSignature
If Not dict.Exists(claveCompuesta) Then
dict.Add claveCompuesta, proc
Else
Debug.Print "[CrearDiccionarioProcedimientos] - ADVERTENCIA: Procedimiento duplicado: " & claveCompuesta
End If
Next i
Set CrearDiccionarioProcedimientos = dict
End Function
'@Description: Compara metadatos de hoja vs código y detecta diferencias
'@Scope: Privado
'@ArgumentDescriptions: dictHoja: Dictionary de Excel | dictCodigo: Dictionary del código | mensaje: String de salida con resumen
'@Returns: Boolean | True si hay diferencias
Private Function HayDiferenciasEnMetadatos(dictHoja As Object, dictCodigo As Object, ByRef mensaje As String) As Boolean
Dim claveCompuesta As Variant
Dim proc As clsVBAProcedure
Dim metadatos As Variant
Dim contadorDiferencias As Long
Dim contadorNuevos As Long
Dim contadorEliminados As Long
HayDiferenciasEnMetadatos = False
contadorDiferencias = 0
contadorNuevos = 0
contadorEliminados = 0
' Verificar cada procedimiento en el código
For Each claveCompuesta In dictCodigo.Keys
Set proc = dictCodigo(claveCompuesta)
' Si el procedimiento no está en la hoja, es nuevo
If Not dictHoja.Exists(claveCompuesta) Then
HayDiferenciasEnMetadatos = True
contadorNuevos = contadorNuevos + 1
Else
' Comparar metadatos
metadatos = dictHoja(claveCompuesta)
' Normalizar valores para comparación
Dim descCode As String, descHoja As String
Dim catCode As String, catHoja As String
Dim scopeCode As String, scopeHoja As String
Dim argsCode As String, argsHoja As String
Dim retCode As String, retHoja As String
descCode = proc.Description
descHoja = metadatos(1)
catCode = IIf(proc.Category = DEFAULT_CATEGORY, "", proc.Category)
catHoja = metadatos(2)
scopeCode = proc.Scope
scopeHoja = metadatos(3)
argsCode = IIf(proc.ArgumentDescriptions = DEFAULT_NOPARAMS, "", proc.ArgumentDescriptions)
argsHoja = metadatos(4)
retCode = proc.Returns
retHoja = metadatos(5)
' Comparar
If descCode <> descHoja Or _
catCode <> catHoja Or _
scopeCode <> scopeHoja Or _
argsCode <> argsHoja Or _
retCode <> retHoja Then
HayDiferenciasEnMetadatos = True
contadorDiferencias = contadorDiferencias + 1
End If
End If
Next claveCompuesta
' Verificar si hay procedimientos en la hoja que ya no existen en el código
For Each claveCompuesta In dictHoja.Keys
If Not dictCodigo.Exists(claveCompuesta) Then
HayDiferenciasEnMetadatos = True
contadorEliminados = contadorEliminados + 1
End If
Next claveCompuesta
' Generar mensaje resumen
If HayDiferenciasEnMetadatos Then
mensaje = "Diferencias detectadas:" & vbCrLf
If contadorNuevos > 0 Then
mensaje = mensaje & " " & contadorNuevos & " procedimiento(s) nuevo(s) en código" & vbCrLf
End If
If contadorEliminados > 0 Then
mensaje = mensaje & " " & contadorEliminados & " procedimiento(s) eliminado(s) del código" & vbCrLf
End If
If contadorDiferencias > 0 Then
mensaje = mensaje & " " & contadorDiferencias & " procedimiento(s) con metadatos diferentes" & vbCrLf
End If
Else
mensaje = ""
End If
End Function
'@Description: Actualiza los archivos de código VBA con los metadatos de la hoja Excel
'@Scope: Privado
'@ArgumentDescriptions: dictHoja: Dictionary de Excel | dictCodigo: Dictionary del código
Private Sub ActualizarCodigoVBA(dictHoja As Object, dictCodigo As Object)
Dim claveCompuesta As Variant
Dim proc As clsVBAProcedure
Dim metadatos As Variant
Dim vbComp As VBIDE.VBComponent
Dim CodeModule As VBIDE.CodeModule
Dim actualizados As Long
On Error GoTo ErrorHandler
actualizados = 0
For Each claveCompuesta In dictHoja.Keys
If dictCodigo.Exists(claveCompuesta) Then
Set proc = dictCodigo(claveCompuesta)
metadatos = dictHoja(claveCompuesta)
' Actualizar objeto en memoria
proc.Description = metadatos(1)
proc.Category = metadatos(2)
proc.Scope = metadatos(3)
proc.ArgumentDescriptions = metadatos(4)
proc.Returns = metadatos(5)
' Actualizar archivo VBA
Set vbComp = ThisWorkbook.VBProject.VBComponents(proc.Module)
Set CodeModule = vbComp.CodeModule
Call ActualizarMetadatosEnCodigo(CodeModule, proc)
actualizados = actualizados + 1
End If
Next claveCompuesta
Debug.Print "[ActualizarCodigoVBA] - " & actualizados & " procedimiento(s) actualizado(s) en código"
Exit Sub
ErrorHandler:
Debug.Print "[ActualizarCodigoVBA] - Error: " & Err.Description
MsgBox "Error al actualizar código VBA: " & Err.Description & vbCrLf & _
"Procedimiento: " & proc.Module & "." & proc.Name, vbCritical, "Error"
End Sub
'@Description: Actualiza los metadatos de un procedimiento específico en su módulo de código
'@Scope: Privado
'@ArgumentDescriptions: CodeModule: Módulo VBA donde está el procedimiento | proc: Objeto clsVBAProcedure con nuevos metadatos
Private Sub ActualizarMetadatosEnCodigo(CodeModule As VBIDE.CodeModule, proc As clsVBAProcedure)
Dim i As Long
Dim lineText As String
Dim nuevosMetadatos As String
Dim inicioMetadatos As Long
Dim finMetadatos As Long
Dim lineasEliminadas As Long
On Error GoTo ErrorHandler
' Buscar y eliminar metadatos existentes (comentarios con @ antes de la firma)
inicioMetadatos = proc.procStartLine
finMetadatos = proc.procSignatureLine - 1
lineasEliminadas = 0
' Eliminar metadatos antiguos de abajo hacia arriba para no desplazar índices
For i = finMetadatos To inicioMetadatos Step -1
lineText = Trim$(CodeModule.Lines(i, 1))
If Left$(lineText, 1) = "'" And InStr(lineText, "@") > 0 Then
CodeModule.DeleteLines i, 1
lineasEliminadas = lineasEliminadas + 1
End If
Next i
' Generar nuevos metadatos
nuevosMetadatos = GenerarMetadatosFormateados(proc)
' Insertar nuevos metadatos antes de la firma del procedimiento
' (ajustando la línea por las eliminaciones)
If nuevosMetadatos <> "" Then
Dim lineaInsercion As Long
lineaInsercion = proc.procSignatureLine - lineasEliminadas
CodeModule.InsertLines lineaInsercion, nuevosMetadatos
End If
Exit Sub
ErrorHandler:
Debug.Print "[ActualizarMetadatosEnCodigo] - Error en " & proc.Module & "." & proc.Name & ": " & Err.Description
End Sub
'@Description: Genera el texto formateado de los metadatos para insertar en el código
'@Scope: Privado
'@ArgumentDescriptions: proc: Objeto clsVBAProcedure con metadatos
'@Returns: String | Texto con metadatos formateados (incluye vbCrLf al final)
Private Function GenerarMetadatosFormateados(proc As clsVBAProcedure) As String
Dim resultado As String
resultado = ""
' Description (siempre incluir si no está vacío)
If proc.Description <> "" Then
resultado = resultado & "'@Description: " & proc.Description & vbCrLf
End If
' Category (omitir si es la categoría por defecto)
If proc.Category <> "" And proc.Category <> DEFAULT_CATEGORY Then
resultado = resultado & "'@Category: " & proc.Category & vbCrLf
End If
' Scope
If proc.Scope <> "" Then
resultado = resultado & "'@Scope: " & proc.Scope & vbCrLf
End If
' ArgumentDescriptions (omitir si es "(sin parámetros)")
If proc.ArgumentDescriptions <> "" And proc.ArgumentDescriptions <> DEFAULT_NOPARAMS Then
resultado = resultado & "'@ArgumentDescriptions: " & proc.ArgumentDescriptions & vbCrLf
End If
' Returns
If proc.Returns <> "" Then
resultado = resultado & "'@Returns: " & proc.Returns & vbCrLf
End If
GenerarMetadatosFormateados = resultado
End Function
' ==========================================
' FIN DE EXTENSIÓN
' ==========================================
'
' RESUMEN DE FUNCIONES CREADAS:
'
' 1. WriteProcedimientosSheet() - Procedimiento principal público
' 2. SheetExists() - Verifica existencia de hoja
' 3. CrearHojaProcedimientos() - Crea hoja nueva
' 4. CrearEncabezadosHoja() - Formatea encabezados
' 5. VolcarProcedimientosAHoja() - Vuelca datos a Excel
' 6. SincronizarConHoja() - Gestiona sincronización bidireccional
' 7. LeerMetadatosDeHoja() - Lee desde Excel
' 8. CrearDiccionarioProcedimientos() - Crea diccionario del código
' 9. HayDiferenciasEnMetadatos() - Detecta y reporta diferencias
' 10. ActualizarCodigoVBA() - Actualiza archivos VBA
' 11. ActualizarMetadatosEnCodigo() - Modifica metadatos en código
' 12. GenerarMetadatosFormateados() - Genera formato de metadatos
'
' MEJORAS RESPECTO A LA VERSIÓN ORIGINAL:
' Clave compuesta Módulo|Firma para evitar conflictos
' Columna adicional "Módulo" en Excel
' Mensajes más informativos con contadores
' Mejor formato visual en hoja Excel (colores alternados, bordes)
' Confirmación adicional antes de modificar código
' Normalización de valores para comparación correcta
' Manejo de duplicados con advertencias en Debug
' Anchos de columna optimizados
'
' ==========================================