[VBA] Tâche de fond pour killer Adobe après un cprintfile

Cocoroboss

XLDnaute Nouveau
Bonjour à tous,

J'utilise sur VBA la commande cprintfile pour générer des PDF dans le but de les fusionner avec PDFCreator par la suite. Voici le code en question pour un exemple de 3 PDF à fusionner (j'ai plus le nom de l'auteur désolé) (voir partie sous la ligne d'apostrophe pour les cprintfile) :

Code:
Sub bindPDF(fnameout$)

 Dim pdfjob As PDFCreator.clsPDFCreator
 Dim sPDFName As String
 Dim sPDFPath As String
 Dim DefaultPrinter$
 Dim bRestart As Boolean

 Dim sFilenames(3) As String

 sFilenames(1) = "T:\_USERS\ECOR\Projet Macro Priorité 1\Dossier test\Yo-test.docx"
 sFilenames(2) = "T:\_USERS\ECOR\Projet Macro Priorité 1\Dossier test\Yo-pdf - Copy.pdf"
 sFilenames(3) = "T:\_USERS\ECOR\Projet Macro Priorité 1\Dossier test\Yo-pdf - Copy - Copy.pdf"

 '/// Change the output file name here! ///
 sPDFName = fnameout

 '' stub here
 sPDFPath = "C:\Users\me\Desktop"

 'Activate error handling and turn off screen updates
 On Error GoTo EarlyExit
 ' Application.ScreenUpdating = False
 Set pdfjob = New PDFCreator.clsPDFCreator

 'Check if PDFCreator is already running and attempt to kill the process if so
 Do
 bRestart = False
 Set pdfjob = New PDFCreator.clsPDFCreator
 If pdfjob.cStart("/NoProcessingAtStartup") = False Then
 'PDF Creator is already running. Kill the existing process
 Shell "taskkill /f /im PDFCreator.exe", vbHide
 DoEvents
 Set pdfjob = Nothing
 bRestart = True
 End If
 Loop Until bRestart = False

 'Assign settings for PDF job
 With pdfjob
 .cOption("UseAutosave") = 1
 .cOption("UseAutosaveDirectory") = 1
 .cOption("AutosaveDirectory") = sPDFPath
 .cOption("AutosaveFilename") = sPDFName
 .cOption("AutosaveFormat") = 0 ' 0 = PDF
 DefaultPrinter = .cDefaultPrinter
 .cDefaultPrinter = "PDFCreator"
 .cClearCache
 End With

 'Delete the PDF if it already exists
 If Dir(sPDFPath & sPDFName) = sPDFName Then Kill (sPDFPath & sPDFName)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 'Print the document to PDF
 With pdfjob
 ' change this filename each loop
 .cPrintFile (sFilenames(1))
 Application.Wait Now + TimeValue("0:0:2")
 .cPrintFile (sFilenames(2))
 Application.Wait Now + TimeValue("0:0:2")
 .cPrintFile (sFilenames(3))
 Application.Wait Now + TimeValue("0:0:2")
 
 'Wait until all the print jobs have entered the queue
 Do Until pdfjob.cCountOfPrintjobs = 3
 DoEvents
 Loop
 .cCombineAll
 .cPrinterStop = False

 End With
 'Wait until the PDF file shows up then release the objects
 Do Until pdfjob.cCountOfPrintjobs = 0
 DoEvents
 Loop

 'Wait a bit longer for PDF Creator to finish
 Application.Wait Now + TimeValue("0:0:2")

 'reset original Windows' default printer
 pdfjob.cDefaultPrinter = DefaultPrinter
 pdfjob.cClose

Cleanup:
 'Release objects and terminate PDFCreator
 Set pdfjob = Nothing
 Shell "taskkill /f /im PDFCreator.exe", vbHide
 On Error GoTo 0
 'Application.ScreenUpdating = True
 Exit Sub

EarlyExit:
 'Inform user of error, and go to cleanup section
 MsgBox "There was an error encountered. PDFCreator has" & vbCrLf & _
 "has been terminated on file " & sPDFName & " in bind. Please try again.", _
 vbCritical + vbOKOnly, "Error"
 Resume Cleanup

 End Sub

Le problème que j'ai c'est qu'après l'exécution de la commande cprintfile pour un PDF, Adobe reste ouvert (le PDF s'est fermé mais pas l'application) et tant qu'Adobe reste ouvert le code ne se poursuit pas, j'obtiens même un message d'erreur : "Microsoft Excel is waiting for another application to complete an OLE action" (l'action en question est la fermeture manuelle d'Adobe). Le site PDFFORGE me l'a confirmé : "After printing PDF files with Adobe Acrobat Reader, the program window will not close anymore. This behavior can't be be influenced by the user. You can use an alternative application to print the PDF file, i.e. Foxit Reader".

J'ai essayé de killer Adobe une fois la commande cprintfile terminée mais en fait tant qu'Adobe reste ouvert la commande ne se termine pas.

Du coup j'ai pensé à une tâche de fond qui inspecterait l'état d'Adobe et qui, dès qu'elle repère qu'Adobe est lancée, attend quelques secondes (le temps que le cprintfile se fasse) et ferme Adobe sans autre forme de procès.

Pensez-vous qu'une telle tâche existe ?
 
C

Compte Supprimé 979

Guest
Re : [VBA] Tâche de fond pour killer Adobe après un cprintfile

Bonjour Cocoroboss

Ta méthode n'est pas bonne :(;)

Essaye ce code ;)
Code:
Option Explicit

'Déclaration des API
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
  (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage& Lib "user32" Alias "PostMessageA" ( _
  ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long)
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
  ByVal hWnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long


Private Const WM_CLOSE = &H10


Sub Test()
  Call bindPDF("Essai.pdf")
End Sub


Sub bindPDF(fnameout$)
  Dim pdfjob As PDFCreator.clsPDFCreator
  Dim sPDFName As String
  Dim sPDFPath As String
  Dim DefaultPrinter$
  Dim bRestart As Boolean
  Dim sFilenames(3) As String
  Dim hWnd As Long


  sFilenames(1) = "T:\_USERS\ECOR\Projet Macro Priorité 1\Dossier test\Yo-test.docx"
  sFilenames(2) = "T:\_USERS\ECOR\Projet Macro Priorité 1\Dossier test\Yo-pdf - Copy.pdf"
  sFilenames(3) = "T:\_USERS\ECOR\Projet Macro Priorité 1\Dossier test\Yo-pdf - Copy - Copy.pdf"


  '/// Change the output file name here! ///
  sPDFName = fnameout
  '' stub here
  sPDFPath = "C:\Users\me\Desktop"


  'Activate error handling and turn off screen updates
  On Error GoTo EarlyExit
  ' Application.ScreenUpdating = False
  Set pdfjob = New PDFCreator.clsPDFCreator


  'Check if PDFCreator is already running and attempt to kill the process if so
  Do
    bRestart = False
    Set pdfjob = New PDFCreator.clsPDFCreator
    If pdfjob.cStart("/NoProcessingAtStartup") = False Then
      'PDF Creator is already running. Kill the existing process
      Shell "taskkill /f /im PDFCreator.exe", vbHide
      DoEvents
      Set pdfjob = Nothing
      bRestart = True
    End If
  Loop Until bRestart = False


  'Assign settings for PDF job
  With pdfjob
    .cOption("UseAutosave") = 1
    .cOption("UseAutosaveDirectory") = 1
    .cOption("AutosaveDirectory") = sPDFPath
    .cOption("AutosaveFilename") = sPDFName
    .cOption("AutosaveFormat") = 0  ' 0 = PDF
    DefaultPrinter = .cDefaultPrinter
    .cDefaultPrinter = "PDFCreator"
    .cClearCache
  End With


  'Delete the PDF if it already exists
  If Dir(sPDFPath & "\" & sPDFName) = sPDFName Then Kill (sPDFPath & "\" & sPDFName)


  ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  'Print the document to PDF
  '.cPrintFile (sFilenames(1))  ' Non incorrecte
  ShellExecute 1, "Print", sFilenames(1), vbNullString, vbNullString, 1
  Application.Wait Now + TimeValue("0:0:2")
  '.cPrintFile (sFilenames(2))
  ShellExecute 1, "Print", sFilenames(2), vbNullString, vbNullString, 1
  Application.Wait Now + TimeValue("0:0:2")
  '.cPrintFile (sFilenames(3))
  ShellExecute 1, "Print", sFilenames(3), vbNullString, vbNullString, 1
  Application.Wait Now + TimeValue("0:0:2")
  
  'Wait until all the print jobs have entered the queue
  Do Until pdfjob.cCountOfPrintjobs = 3
    DoEvents
  Loop


  ' Close Adobe Reader / Adobe Acrobat
  hWnd = FindWindow(vbNullString, "Adobe Reader")
  If hWnd = 0 Then hWnd = FindWindow(vbNullString, "Adobe Acrobat Standard")
  If hWnd = 0 Then hWnd = FindWindow(vbNullString, "Adobe Acrobat Pro")
  PostMessage hWnd, WM_CLOSE, vbNull, vbNull


  ' Merge print
  With pdfjob
    .cCombineAll
    .cPrinterStop = False
  End With


  'Wait until the PDF file shows up then release the objects
  Do Until pdfjob.cCountOfPrintjobs = 0
    DoEvents
  Loop


  'Wait a bit longer for PDF Creator to finish
  Application.Wait Now + TimeValue("0:0:2")


  'reset original Windows' default printer
  pdfjob.cDefaultPrinter = DefaultPrinter
  pdfjob.cClose


Cleanup:
  'Release objects and terminate PDFCreator
  Set pdfjob = Nothing
  Shell "taskkill /f /im PDFCreator.exe", vbHide
  On Error GoTo 0
  'Application.ScreenUpdating = True
  MsgBox "c'est finit"
  Exit Sub


EarlyExit:
  'Inform user of error, and go to cleanup section
  MsgBox "There was an error encountered. PDFCreator has" & vbCrLf & _
         "has been terminated on file " & sPDFName & " in bind. Please try again.", _
         vbCritical + vbOKOnly, "Error"
  Resume Cleanup


End Sub

A+
 

Statistiques des forums

Discussions
315 089
Messages
2 116 098
Membres
112 661
dernier inscrit
ceucri