Sub ExtraerPaginasPDF()
Dim AcroApp As Object, PartDoc As Object
Dim
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The warning in the picture pops up when I try to save an excel file (format: xls) into pdf using the save as function. When translated, it means "This file format is not supported by Microsoft Information Protection. You must convert the document to a supported format first."
I want to understand a few things
Sub ExtraerPaginasPDF()
Dim AcroApp As Object, PartDoc As Object
Dim
Sub ExtraerPaginasConReferencias()
Sub ExtraerPaginasConReferencias()
Dim AcroApp As Object, PartDoc As Object, NewDoc As Object, Page As Object
Dim PdfPath As String, OutputPath As String
Dim wsRefs As Worksheet, wsRutas As Worksheet
Dim referencias As Object, ref As Variant
Dim i As Integer, numPaginas As Integer
Dim paginasExtraidas As Object
' Definir hojas
Set wsRefs = ThisWorkbook.Sheets(1) ' Hoja con referencias (modificar si es otra)
Set wsRutas = ThisWorkbook.Sheets("Rutas")
' Obtener rutas desde la hoja "Rutas"
PdfPath = wsRutas.Range("A1").Value
OutputPath = wsRutas.Range("B2").Value
' Validar rutas
If PdfPath = "" Or OutputPath = "" Then
MsgBox "Las rutas en 'Rutas' (A1 y B2) no pueden estar vacías.", vbExclamation
Exit Sub
End If
' Crear objeto Acrobat
Set AcroApp = CreateObject("AcroExch.App")
Set PartDoc = CreateObject("AcroExch.PDDoc")
Set NewDoc = CreateObject("AcroExch.PDDoc")
' Abrir PDF
If PartDoc.Open(PdfPath) = False Then
MsgBox "No se pudo abrir el PDF.", vbCritical
Exit Sub
End If
numPaginas = PartDoc.GetNumPages
' Cargar referencias desde la primera hoja
Set referencias = CreateObject("Scripting.Dictionary")
i = 1
Do While wsRefs.Cells(i, 1).Value <> ""
referencias.Add wsRefs.Cells(i, 1).Value, True
i = i + 1
Loop
' Crear diccionario para evitar duplicados
Set paginasExtraidas = CreateObject("Scripting.Dictionary")
' Buscar referencias en cada página
For i = 0 To numPaginas - 1
Set Page = PartDoc.AcquirePage(i)
Dim jso As Object
Set jso = Page.GetJSObject
Dim textoPagina As String
textoPagina = jso.getPageNthWord(0, 0)
' Comparar con referencias
For Each ref In referencias.Keys
If InStr(1, textoPagina, ref, vbTextCompare) > 0 Then
If Not paginasExtraidas.Exists(i) Then
paginasExtraidas.Add i, True
NewDoc.InsertPages NewDoc.GetNumPages, PartDoc, i, 1, False
End If
Exit For
End If
Next ref
Next i
' Guardar PDF resultante si hay páginas
If NewDoc.GetNumPages > 0 Then
NewDoc.Save 1, OutputPath
MsgBox "PDF generado con éxito: " & OutputPath, vbInformation
Else
MsgBox "No se encontraron referencias en el PDF.", vbExclamation
End If
' Cerrar documentos
NewDoc.Close
PartDoc.Close
AcroApp.Exit
' Liberar memoria
Set NewDoc = Nothing
Set PartDoc = Nothing
Set AcroApp = Nothing
End Sub
```End Sub
```vba
Sub ExtraerPaginasConReferencias()