PDFCreator : conversion de document notes en PDF

PDFCreator : conversion de document notes en PDF

Messagepar abertisch » 15 Oct 2009 à 11:47

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
Dernière édition par abertisch le 01 Déc 2009 à 16:41, édité 3 fois.
"Lotus, il n'y a qu'en rouleaux que ça fait pas mal au cul"
abertisch
Roi des posts
Roi des posts
 
Message(s) : 763
Inscrit(e) le : 25 Oct 2006 à 13:51
Localisation : Suisse

Messagepar oguruma » 26 Oct 2009 à 13:47

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
Bien à vous

http://www.dominoarea.org/oguruma/

Les téléphones PORTABLES dans les TGV y en a MARRRE de ces voyageurs qui ne respectent pas les autres ! ARRET DES PORTABLES SVP - Merci

Fumeurs ! respectez les non fumeurs !!!
Fumeurs ! respectez la loi de février 2007 et les lieux publics !!! (ie. hall de gares)
Avatar de l’utilisateur
oguruma
Super V.I.P.
Super V.I.P.
 
Message(s) : 4086
Inscrit(e) le : 16 Déc 2004 à 08:50
Localisation : LILLE

Messagepar valerielc » 28 Juil 2010 à 09:17

Bonjour,

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

Quelqu'un voit-il une solution ?
valerielc
Premier posts
Premier posts
 
Message(s) : 19
Inscrit(e) le : 07 Avr 2010 à 15:09

Messagepar erenault » 28 Juil 2010 à 16:31

Avec PDFCreator dans certaines conditions, il faut que cette "imprimante" soit par défaut ...
erenault
Premier posts
Premier posts
 
Message(s) : 25
Inscrit(e) le : 26 Mars 2008 à 11:29

Messagepar Michael DELIQUE » 28 Juil 2010 à 17:51

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
Cordialement

Michael (SMS-Phobique)
----------------------------
"La théorie, c'est quand on sait tout et que rien ne fonctionne. La pratique, c'est quand tout fonctionne et que personne ne sait pourquoi."
Albert EINSTEIN
Avatar de l’utilisateur
Michael DELIQUE
Administrateur
Administrateur
 
Message(s) : 12183
Inscrit(e) le : 16 Déc 2004 à 10:36
Localisation : Paris/Cergy


Retour vers Divers