Gestion des CreateObject instances PDF
Bonjour,
j'ai un fichier PDF de plusieurs page contenant des bulletins de paie. je l'extrait en autant de pages que de bulletin puis via macro je cherche à trouver le nom du salarié pour pouvoir renommer ce fichier avec le nom du salarié trouvé. une 2ème macro modifiera le nom du fichier.
j'ai tant bien que mal créé la macro pour chercher le nom présent sur le pdf puis le copier sur excel ou mettre "not found" si pas trouvé (erreur dans le nom par exemple ou nouveau salarié dont je n'ai pas le nom.
Ca marche un peu.. plutôt en mode pas à pas car quand je lance la macro ca plante au bout de qq fichiers sur la base d'un "excel est en attente d’une autre application pour terminer une action OLE".
Je pense que ce sont mes instances createobjects qui au fur et à mesure restent en mémoire et font planter excel.
merci pour vos retours si celà vous parle.
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 89 90 91 92
| Sub FindTextInPDF() 'pour renommer fichier
'----------------------------------------------------------------------------------------
'The code uses late binding, so no reference to external library is required.
'However, the code works ONLY with Adobe Professional, so don't try to use it with
'Adobe Reader because you will get an "ActiveX component can't create object" error.
'site: http://www.myengineeringworld.net
'----------------------------------------------------------------------------------------
Dim TextToFind As String
Dim PDFPath As String
Dim App As Object
Dim AVDoc As Object
Dim i As Integer
Dim j As Integer
Dim IDdone As Integer 'prend valeur 1 quand bullpaie identifié
Dim DerlignSAL As Integer
Dim DerlignBUL As Integer
DerlignSAL = Cells(60, 5).End(xlUp).Row
DerlignBUL = Cells(60, 1).End(xlUp).Row
'Initialize Acrobat by creating the App object.
Set App = CreateObject("AcroExch.App")
'Check if the object was created. In case of error release the object and exit.
If Err.number <> 0 Then
MsgBox "Could not create the Adobe Application object!", vbCritical, "Object Error"
Set App = Nothing
Exit Sub
End If
On Error GoTo 0
For j = 2 To DerlignBUL 'liste la totalité des fichiers
For i = 2 To DerlignSAL - IDdone 'Liste la totalité des salariés
TextToFind = Split(LTrim(Cells(i, 5).Value) & Space(1))(0) 'premier mot à trouver
PDFPath = Cells(j, 1).Value
'Create the AVDoc object.
Set AVDoc = CreateObject("AcroExch.AVDoc")
'Check if the object was created. In case of error release the objects and exit.
If Err.number <> 0 Then
MsgBox "Could not create the AVDoc object!", vbCritical, "Object Error"
Set AVDoc = Nothing
Set App = Nothing
Exit Sub
End If
On Error Resume Next
'Open the PDF file.
If AVDoc.Open(PDFPath, "") = True Then
' AVDoc.BringToFront
If AVDoc.FindText(TextToFind, False, True, False) = False Then
'Text was not found, close the PDF file without saving the changes.
AVDoc.Close True
'Release the objects.
Set AVDoc = Nothing
'Inform the user.
Cells(j, 4) = "NOT FOUND"
Else
Cells(j, 4) = TextToFind 'si trouvé coller le nom
IDdone = IDdone + 1 'incrémenter ID pour réduire la recherche pour le prochain
Cells(i, 5).Delete shift:=xlUp 'supprimer le nom trouvé de la liste de recherche pour réduire la liste de recherche
'Release the objects
AVDoc.Close True
Set AVDoc = Nothing
Exit For 'sortir de la boucle pour en commencer une autre en incrémentant J
End If
Else
'Unable to open the PDF file, close the Acrobat application.
App.Exit
'Release the objects.
Set AVDoc = Nothing
Set App = Nothing
'Inform the user.
MsgBox "Could not open the PDF file!", vbCritical, "File error"
End If
Next
Next
'Close the Acrobat application.
App.Exit
Set App = Nothing
End Sub |