AT_Pdf.bas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. Option Compare Database
  2. ' ** Access Toolbox Module **
  3. ' on 2017-02-28,
  4. ' @author: Olivier Massot
  5. ' V 1.0
  6. ' Operations on PDF Files
  7. ' ! Requires AT_Access
  8. ' ! Requires PDF Creator 1.7.0
  9. Public Function export_pdf(report_name As String, _
  10. dir_path As String, _
  11. Optional pdf_name As String = "", _
  12. Optional filter As String = "", _
  13. Optional replace As Boolean = True) As Boolean
  14. On Error GoTo err
  15. export_pdf = False
  16. If Not report_exists(report_name) Then GoTo errNotExist
  17. If Right(dir_path, 1) <> "\" Then dir_path = dir_path & "\"
  18. If Dir(dir_path, vbDirectory) = "" Then GoTo errDirNotExist
  19. If Not Len(pdf_name) > 0 Then pdf_name = report_name
  20. If Right(nomPDF, 4) <> ".pdf" Then pdf_name = pdf_name & ".pdf"
  21. If Dir(dir_path & pdf_name) <> "" Then
  22. If replace = False Then
  23. If MsgBox("Un fichier PDF portant ce nom existe déjà, voulez-vous l'écraser?", vbYesNo) = vbNo Then
  24. GoTo cancel_
  25. End If
  26. End If
  27. On Error GoTo errDelFile
  28. Kill repCible & nomPDF
  29. On Error GoTo err
  30. End If
  31. If Not report_is_opened(report_name) Then DoCmd.OpenReport nomEtat, acViewNormal, , filter, acHidden
  32. DoCmd.OutputTo acOutputReport, report_name, acFormatPDF, dir_path & pdf_name
  33. DoCmd.Close acReport, report_name, acSaveNo
  34. exportPDF = True
  35. fin:
  36. Exit Function
  37. errNotExist:
  38. MsgBox "Erreur: l'état demandé n'existe pas ('" & report_name & "')"
  39. GoTo fin
  40. errDirNotExist:
  41. MsgBox "Erreur: le répertoire cible '" & repCible & "' n'existe pas, veuillez le créer ou contacter un administrateur."
  42. GoTo fin
  43. cancel_:
  44. MsgBox "Export PDF annulé"
  45. GoTo fin
  46. errDelFile:
  47. MsgBox "Impossible de supprimer le fichier existant, il est peut-être ouvert?" & vbNewLine & "Opération annulée"
  48. GoTo fin
  49. err:
  50. MsgBox "Erreur de l'export PDF : " & vbNewLine & err.Description
  51. GoTo fin
  52. End Function
  53. Public Sub pdf_append(ByVal path_pdf1 As String, ByVal path_pdf2 As String, ByVal result_path As String)
  54. 'Créé le fichier PDF 'result_path' en indérant le PDF 2 à la suite du PDF 1
  55. ' usage:
  56. ' >> pdf_append "C:\path\to\file1.pdf", "C:\path\to\file2.pdf", "C:\path\to\file1&2.pdf"
  57. ' ATTENTION: nécessite PDF Creator 1.7
  58. On Error GoTo err
  59. Dim oPdf As Object
  60. On Error GoTo errPdfforge
  61. Set oPdf = CreateObject("pdfforge.pdf.pdf")
  62. On Error GoTo err
  63. oPdf.MergePDFFiles_2 Array(path_pdf1, path_pdf2), result_path, True
  64. fin:
  65. Exit Sub
  66. err:
  67. MsgBox "Erreur lors de la fusion des pdfs: " & err.Description
  68. GoTo fin
  69. errPdfforge:
  70. MsgBox "Erreur: Pdfforge 1.7 est nécessaire pour utiliser cette fonction, opération annulée." & vbNewLine & _
  71. "Essayez d'installer PDF Creator 1.7.0"
  72. GoTo fin
  73. End Sub
  74. Public Sub ExcelVersPDF(cheminExcel As String, Optional cheminPDF As String = "", Optional feuille As Integer = 1, Optional ecraser As Boolean = True)
  75. 'exporte la page demandée du document excel en pdf
  76. '! Requires Micosoft Excel XX.X Object Library
  77. Dim objExcel As Excel.Application
  78. Dim objExcelDoc As Excel.Workbook
  79. Dim objExcelFeuille As Excel.Worksheet
  80. If Dir(cheminExcel) = "" Then GoTo errSource
  81. If Dir(repFichier(cheminPDF), vbDirectory) = "" Then GoTo errRepCible
  82. If Len(cheminPDF) = 0 Then cheminPDF = Split(cheminExcel, ".")(0) & ".pdf"
  83. If feuille < 1 Then feuille = 1
  84. If Right(cheminPDF, 4) <> ".pdf" Then cheminPDF = cheminPDF & ".pdf"
  85. If Dir(cheminPDF) <> "" Then
  86. If ecraser = False Then
  87. If MsgBox("Un fichier portant ce nom existe déjà, voulez-vous le remplacer", vbYesNo) = vbNo Then GoTo annule
  88. End If
  89. On Error GoTo errSuppr
  90. Kill cheminPDF
  91. On Error GoTo err
  92. End If
  93. Set objExcel = CreateObject("Excel.Application")
  94. objExcel.Visible = True
  95. Set objExcelDoc = objExcel.Workbooks.Open(cheminExcel)
  96. If Not objExcel Is Nothing Then
  97. objExcelDoc.ExportAsFixedFormat xlTypePDF, cheminPDF, xlQualityStandard, , , feuille, feuille, False
  98. End If
  99. fin:
  100. On Error Resume Next
  101. objExcelDoc.Close False
  102. objExcel.Quit
  103. Set objExcelDoc = Nothing
  104. Set objExcel = Nothing
  105. Exit Sub
  106. err:
  107. MsgBox "Erreur: " & err.Description
  108. GoTo fin
  109. errSource:
  110. MsgBox "Erreur: Fichier Excel (.xls) introuvable"
  111. GoTo fin
  112. errSuppr:
  113. MsgBox "Impossible de supprimer le fichier existant, il est peut-être ouvert?" & vbNewLine & "Opération annulée"
  114. GoTo fin
  115. annule:
  116. MsgBox "Opération annulée"
  117. GoTo fin
  118. errRepCible:
  119. MsgBox "Erreur: le répertoire cible n'existe pas, veuillez le créer ou prévenir un administrateur"
  120. GoTo fin
  121. End Sub
  122. Public Sub WordVersPDF(cheminWord As String, Optional cheminPDF As String, Optional ecraser As Boolean = True)
  123. 'exporte la page demandée du document word en pdf
  124. '! Requires Micosoft Word XX.X Object Library
  125. Dim objWord As Word.Application
  126. Dim objWordDoc As Word.Document
  127. If Dir(cheminWord) = "" Then GoTo errSource
  128. If Dir(repFichier(cheminPDF), vbDirectory) = "" Then GoTo errRepCible
  129. If Len(cheminPDF) = 0 Then cheminPDF = Split(cheminWord, ".")(0) & ".pdf"
  130. If Right(cheminPDF, 4) <> ".pdf" Then cheminPDF = cheminPDF & ".pdf"
  131. If Dir(cheminPDF) <> "" Then
  132. If ecraser = False Then
  133. If MsgBox("Un fichier portant ce nom existe déjà, voulez-vous le remplacer", vbYesNo) = vbNo Then GoTo annule
  134. End If
  135. On Error GoTo errSuppr
  136. Kill cheminPDF
  137. On Error GoTo err
  138. End If
  139. Set objWord = CreateObject("Word.Application")
  140. objWord.Visible = True
  141. Set objWordDoc = objWord.Documents.Open(cheminWord)
  142. If Not objWord Is Nothing Then
  143. objWordDoc.ExportAsFixedFormat cheminPDF, wdExportFormatPDF, False, wdExportOptimizeForPrint, wdExportAllDocument
  144. End If
  145. fin:
  146. On Error Resume Next
  147. objWordDoc.Close False
  148. objWord.Quit
  149. Set objWordDoc = Nothing
  150. Set objWord = Nothing
  151. Exit Sub
  152. err:
  153. MsgBox "Erreur: " & err.Description
  154. GoTo fin
  155. errSource:
  156. MsgBox "Erreur: Fichier Word (.doc / .docx) introuvable"
  157. GoTo fin
  158. errSuppr:
  159. MsgBox "Impossible de supprimer le fichier existant, il est peut-être ouvert?" & vbNewLine & "Opération annulée"
  160. GoTo fin
  161. annule:
  162. MsgBox "Opération annulée"
  163. GoTo fin
  164. errRepCible:
  165. MsgBox "Erreur: le répertoire cible n'existe pas, veuillez le créer ou prévenir un administrateur"
  166. GoTo fin
  167. End Sub