Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Last active September 4, 2025 21:17
Show Gist options
  • Save tzkmx/7e5041ea268931319b1f80dcb911a7e0 to your computer and use it in GitHub Desktop.
Save tzkmx/7e5041ea268931319b1f80dcb911a7e0 to your computer and use it in GitHub Desktop.
Lista de entradas de autocorrección (Macro de Word VBA)
¿Qué hace esta versión?
- Crea un documento nuevo.
- Inserta una tabla con tres columnas: texto original, texto sustituido, y si tiene formato.
- Detecta si el contenido tiene negritas, cursivas, tablas o imágenes.
Public Sub GenerarSoloConFormato()
Call AutoCorrectListFormatted(True)
End Sub
Public Sub GenerarListaAutocorreccion()
Call AutoCorrectListFormatted(False)
End Sub
Private Sub AutoCorrectListFormatted(Optional soloConFormato As Boolean = False)
Dim a As AutoCorrectEntry
Dim doc As Document
Dim tbl As Table
Dim r As Range
Dim i As Integer
Dim tieneFormato As String
' Crear nuevo documento
Set doc = Documents.Add
' Insertar tabla con encabezados
Set tbl = doc.Tables.Add(Range:=doc.Range(0, 0), NumRows:=1, NumColumns:=3)
tbl.Cell(1, 1).Range.Text = "Texto original"
tbl.Cell(1, 2).Range.Text = "Texto sustituido"
tbl.Cell(1, 3).Range.Text = "¿Tiene formato?"
i = 2 ' Comenzar en la segunda fila
' Recorrer entradas de autocorrección
For Each a In Application.AutoCorrect.Entries
' Insertar el valor en un rango temporal
Set r = doc.Range
r.Collapse Direction:=wdCollapseEnd
r.InsertAfter a.Value
r.Select
' Detectar si hay formato
If r.Font.Bold = True Or r.Font.Italic = True Or r.Tables.Count > 0 Or r.InlineShapes.Count > 0 Then
tieneFormato = "Sí"
Else
tieneFormato = "No"
End If
' Filtrar si solo queremos entradas con formato
If Not soloConFormato Or tieneFormato = "Sí" Then
tbl.Rows.Add
tbl.Cell(i, 1).Range.Text = a.Name
tbl.Cell(i, 2).Range.Text = a.Value
tbl.Cell(i, 3).Range.Text = tieneFormato
i = i + 1
End If
' Limpiar el rango temporal
r.Delete
Next a
MsgBox "Lista de autocorrección generada.", vbInformation
End Sub
Public Sub ExportarSoloConFormatoAExcel()
Call ExportarAutocorreccionAExcel(True)
End Sub
Private Sub ExportarAutocorreccionAExcel(Optional soloConFormato As Boolean = False)
Dim a As AutoCorrectEntry
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim r As Range
Dim fila As Integer
Dim tieneFormato As String
' Iniciar Excel
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWB = xlApp.Workbooks.Add
Set xlSheet = xlWB.Sheets(1)
' Encabezados
xlSheet.Cells(1, 1).Value = "Texto original"
xlSheet.Cells(1, 2).Value = "Texto sustituido"
xlSheet.Cells(1, 3).Value = "¿Tiene formato?"
fila = 2
' Recorrer entradas
For Each a In Application.AutoCorrect.Entries
' Insertar en rango temporal para inspección
Set r = Documents.Add.Range
r.InsertAfter a.Value
If r.Font.Bold = True Or r.Font.Italic = True Or r.Tables.Count > 0 Or r.InlineShapes.Count > 0 Then
tieneFormato = "Sí"
Else
tieneFormato = "No"
End If
' Filtrar si se requiere
If Not soloConFormato Or tieneFormato = "Sí" Then
xlSheet.Cells(fila, 1).Value = a.Name
xlSheet.Cells(fila, 2).Value = a.Value
xlSheet.Cells(fila, 3).Value = tieneFormato
fila = fila + 1
End If
' Cerrar documento temporal
r.Document.Close SaveChanges:=False
Next a
MsgBox "Exportación a Excel completada.", vbInformation
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment