Page 1 sur 1

PDFCreator : conversion de document notes en PDF

MessagePublié: 15 Oct 2009 à 11:47
par abertisch
Voilà un petit bout de code qui permet, via PDFCreator, de générer un pdf depuis un document notes. :wink:

Bon je sais le code n'est pas très propre mais dès que j'ai la version final, je la fait suivre ! :D

Code : Tout sélectionner
   On Error Goto ErrorHandler
   
   Dim sDefaultPrinter As String, spdfname As String
   Dim ws As New notesuiworkspace()
   Dim vrPDFCreator As Variant, PrtNamesColl As Variant
   Dim doc As notesdocument
   Dim cStartedFlag As Boolean, PrinterReset As Boolean
   Dim UIDocToPrint As NotesUIDocument, uidoc As NotesUIDocument
   Dim PDFCreatorOptions As Variant ' PDFCreator options to restore.

       Set uidoc = ws.currentdocument
       set doc = uidoc.Document()
   
   Call uidoc.Save()
   uidoc.EditMode=False
   
   Dim RTIForAttachment As NotesRichTextItem
   Set RTIForAttachment = doc.GetFirstItem("FilePDF")
   
   If Not Isempty(RTIForAttachment.EmbeddedObjects) Then
      Msgbox "Il y a déjà un PDF rattaché au document. Fin du programme."
      Exit Sub
   End If
   
   REM Récupère l'imprimante par défaut
   sDefaultPrinter = PrinterDefautGet()
   
   REM Objet qui gère l'impression
   Set vrPDFCreator = CreateObject("PDFCreator.clsPDFCreator")   
   Set PrtNamesColl = vrPDFCreator.cGetPDFCreatorPrinters()
   
   If vrPDFCreator.cProgramIsRunning Then
      cStartedFlag = True
   Else
      cStartedFlag = vrPDFCreator.cStart(  , True)
   End If
   
   If cStartedFlag = False Then
      Msgbox("CreatePDF - PDFCreator failed to start or not installed. Process halted.")
      Goto skip
   End If
   
   ' Grab the current options for restoration later.
   Set PDFCreatorOptions = vrPDFCreator.cOptions
   
   With vrPDFCreator
         REM Passe L'imprimante PDFCreator en imprimante par défaut
      .cDefaultprinter = Cstr(PrtNamesColl.Item(1))
      
      REM Set and save the PDFCreator options.
      .cClearCache
      .cOption("UseAutosave") = 1
      .cOption("UseAutosaveDirectory") = 1
      .cOption("AutosaveDirectory") = Environ("Temp")
      .cOption("AutosaveFilename") = Cstr(doc.UniversalID())
      .cOption("AutosaveFormat") = 0
      .cOption("AutosaveStartStandardProgram" ) = 0
      .cOption("NoConfirmMessageSwitchingDefaultprinter") = 0
   End With
   
   Call vrPDFCreator.cSaveOptions()
   
   Set UIDocToPrint = app.ws.EditDocument(False, Doc, True , "" , True , True)
   
   Call UIDocToPrint.Print(1 ,  ,  ,  , PrtNamesColl.Item(1))
   Call UIDocToPrint.Close(True)
   
   spdfname = Environ("Temp") +"\"+ Cstr(doc.UniversalID()) + ".pdf"
   
   REM Wait for print job to complete.
   Dim counter As Integer
   Counter = 0
   Do Until (vrPDFCreator.cCountOfPrintJobs = 0 And Dir(spdfname) <> "" ) Or Counter = 30
      Yield
      Sleep 1
      Yield
      Counter = Counter + 1
   Loop
   
   If Counter = 30 Then
      Msgbox ("CreatePDF - PDF Conversion never completed.  Process halted.")
      Goto skip
   End If
   
skip:
   With vrPDFCreator
      REM Blocage de l'imprimante
      .cPrinterStop = True
      
        REM remet l'imprimante par défaut d'origine
      .cDefaultprinter = sDefaultPrinter
      
         REM Vide le Cache de PDFCreator
      .cClearCache
   End With
   
   REM Restore and save original options, then close PDFCreator only if it had been started by this sub.
   Set vrPDFCreator.cOptions = PDFCreatorOptions
   
   REM Ferme l'application PDFCreator
   Call vrPDFCreator.cSaveOptions()
   Call vrPDFCreator.cClose
   
   Call RTIForAttachment.EmbedObject (EMBED_ATTACHMENT , "" ,  spdfname)
   Kill spdfname
   
   Call doc.Save(True, False)
   Call uidoc.Close(True)
   Call ws.EditDocument(True, doc)
   
   Exit Sub   
ErrorHandler:
   
   If Datatype(vrPDFCreator) <> 0 Then
      
      If Err = 4412 Then
         Call UIDoc.Close( True )
         Sleep 3
      End If
      
      Call vrPDFCreator.cErrorClear()
      
      If PrinterReset And sDefaultPrinter <> Cstr( PrtNamesColl.Item(1)) Then
         vrPDFCreator.cDefaultPrinter = Cstr( PrtNamesColl.Item(1))
         vrPDFCreator.cDefaultPrinter = sDefaultPrinter
      End If
      
      Set vrPDFCreator.cOptions = PDFCreatorOptions
      Call vrPDFCreator.cSaveOptions()
      Call vrPDFCreator.cClose()
   Else
      Msgbox "PDFCreator pas installé. Veuillez contacter votre répondant informatique."
      Exit Sub
   End If
End Sub

MessagePublié: 26 Oct 2009 à 13:47
par oguruma
j'ajouterai un yield histoire de rendre la main au système pour ne pas bloquer le poste par précaution

Do Until (vrPDFCreator.cCountOfPrintJobs = 0 And Dir(spdfname) <> "" ) Or Counter = 30
Yield
Sleep 1
Yield
Counter = Counter + 1
Loop

MessagePublié: 28 Juil 2010 à 09:17
par valerielc
Bonjour,

Avec ce code, j'ai l'erreur suivante :
Empty parentheses not legal on : PrinterDefautGet

Quelqu'un voit-il une solution ?

MessagePublié: 28 Juil 2010 à 16:31
par erenault
Avec PDFCreator dans certaines conditions, il faut que cette "imprimante" soit par défaut ...

MessagePublié: 28 Juil 2010 à 17:51
par Michael DELIQUE
PrinterDefautGet est une fonction que tu trouvera ici => http://forum.dominoarea.org/gestion-des ... 23584.html

mais tu vera ici => http://forum.dominoarea.org/pdfcreator- ... 14713.html que pdfcreator peut gérer lui meme l'imprimante par défaut