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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
| Option Compare Database
Option Explicit
Const HWND_BROADCAST = &HFFFF
Const WM_WININICHANGE = &H1A
'''""""""""""""""""""""""""""""""""""""""""""""""""""'''
'
' API kernel32.dll
'
'''""""""""""""""""""""""""""""""""""""""""""""""""""'''
' lire dans un fichier INI
Private Declare Function apiGetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpDefault As String, _
ByVal lpReturnedString As String, ByVal nSize As Long, _
ByVal lpFileName As String) As Long
' renvoyer le répertoire Windows
Private Declare Function apiGetWindowsDirectory Lib "kernel32" _
Alias "GetWindowsDirectoryA" ( _
ByVal lpBuffer As String, ByVal nSize As Long) As Long
' écrire dans un fichier INI
Private Declare Function apiWritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, ByVal lpString As Any, _
ByVal lpFileName As String) As Long
'''""""""""""""""""""""""""""""""""""""""""""""""""""'''
'
' API user32.dll
'
'''""""""""""""""""""""""""""""""""""""""""""""""""""'''
' envoyer un message qui indique à Windows la mise à jour de WIN.INI
Private Declare Function apiSendMessage Lib "user32" _
Alias "SendMessageA" ( _
ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Integer, ByVal lParam As Any) As Long
Private strPath As String
Private lngNC As Long
Private strRet As String
Sub SwitchDefaultPrinter(Nom As String)
' modifie le nom de l'imprimante par défaut
strPath = String(260, 0)
' récupère le chemin de win.ini
strPath = Left$(strPath, apiGetWindowsDirectory(strPath, Len(strPath))) + "\win.ini"
strRet = String(255, 0)
lngNC = apiGetPrivateProfileString("Devices", Nom, "", strRet, 255, strPath)
strRet = Left(strRet, lngNC)
' écrit dans win.ini le nom de l'imprimante souhaitée
apiWritePrivateProfileString "windows", "device", Nom & "," & strRet, strPath
' signale à MS Windows de prendre en compte la modification de win.ini
apiSendMessage HWND_BROADCAST, WM_WININICHANGE, 0, "windows"
End Sub
Function GetDefaultPrinter() As String
' renvoie le nom de l'imprimante par défaut
strPath = String(260, 0)
strPath = Left$(strPath, apiGetWindowsDirectory(strPath, Len(strPath))) + "\win.ini"
strRet = String(255, 0)
lngNC = apiGetPrivateProfileString("windows", "device", "", strRet, 255, strPath)
strRet = Left(strRet, lngNC)
lngNC = InStr(strRet, ",")
GetDefaultPrinter = Left(strRet, lngNC - 1)
End Function
Function getPDF(ByVal strReport As String, _
Optional ByVal allPages As Integer = acPrintAll, _
Optional ByVal pStart As Integer, _
Optional ByVal pEnd As Integer)
Dim strOldPrinter As String
Dim strPdfPrinter As String
Dim dblStamp As Double
' récupération de l'imprimante par défaut
strOldPrinter = GetDefaultPrinter
' attribution de l'imprimante PDF
strPdfPrinter = "PDFCreator"
SwitchDefaultPrinter strPdfPrinter
' récupération de la date / heure courante
dblStamp = Now
' ajout du document dans la file d'attente
addDocument strReport & IIf(allPages <> acPrintAll, " [" & pStart & "-" & pEnd & "]", ""), dblStamp
' ouverture de l'état par automation
fOpenRemoteReport CurrentDb.Name, strReport, _
acViewPreview, allPages, _
IIf(allPages <> acPrintAll, pStart, 1), IIf(allPages <> acPrintAll, pEnd, 9999)
' réattribution de l'imprimante par défaut
SwitchDefaultPrinter strOldPrinter
' gestion de la file d'attente des documents
ScanPDFfiles
End Function
Function addDocument(ByVal strDocName As String, ByVal dblStamp As Double)
' ajouter un document dans la file d'attente
If Not isTable("tblPDFdoc") Then
' création de table
DoCmd.RunSQL "CREATE TABLE tblPDFdoc (doc TEXT, tim TEXT, done YESNO);"
End If
' SQL d'insertion
DoCmd.RunSQL "INSERT INTO tblPDFdoc VALUES (""" & strDocName & """, """ & Format(dblStamp, "yyyymmddhhnnss") & """, 0);"
End Function
Function isTable(tblName As String) As Boolean
' tester l'existence d'une table
On Error GoTo istblerr
Debug.Print CurrentDb.TableDefs(tblName).Name
isTable = True
Exit Function
istblerr:
isTable = False
Err.Clear
End Function
Sub ScanPDFfiles()
' traitement des fichiers en file d'attente
Dim strPath As String, currFile As String
Dim rec As DAO.Recordset
Dim fso As FileSystemObject
Set fso = New FileSystemObject
Dim intCount As Integer
On Error GoTo scanPDF
intCount = 0
' nous avons stocké le chemin de une propriété de la base
strPath = CurrentDb.Properties("workPath")
Set rec = CurrentDb.OpenRecordset("SELECT * FROM tblPDFdoc WHERE done = False ORDER BY tim;", dbOpenDynaset)
Do While Not rec.EOF
' fonction pour trouver le fichier dont la date est la plus proche
' de la date de demande d'édition
currFile = GetFirstFileName(rec!tim)
If Len(currFile) > 0 Then
' si le fichier a été trouvé on le renomme
fso.MoveFile strPath & currFile, strPath & rec!doc & IIf(intCount = 0, "", intCount) & ".pdf"
intCount = 0
' mise à jour de la table de la file d'attente
rec.edit
rec!done = True
rec.Update
End If
rec.MoveNext
Loop
rec.Close
Set rec = Nothing
Set fso = Nothing
Exit Sub
' traitement d'erreurs
scanPDF:
If Err.Number = 58 Then
' si le fichier existe déjà
' on rajoute un numéro au nom ...
intCount = intCount + 1
Resume
Else
MsgBox Err.Number & " - " & Err.Description
Err.Clear
Resume Next
End If
Set fso = Nothing
End Sub
Function GetFirstFileName(ByVal strStamp As String) As String
' fonction renvoyant le nom du fichier dont la date est la plus proche
' de la date de demande d'édition
Dim strPath As String
Dim strFic As String
Dim dblTargetFic As Double
' valeur maximum
dblTargetFic = CDbl("29991231235959")
' nous avons stocké le chemin de une propriété de la base
strPath = CurrentDb.Properties("workPath")
strFic = Dir(strPath & Left(strStamp, 4) & "*.pdf")
Do While Len(strFic) > 0
If CDbl(Left(strFic, Len(strFic) - 4)) >= CDbl(strStamp) Then
' parmi les fichiers dont la date est supérieure ou égale
' à la date de demande d'édition, on prend celle qui a la valeur
' minimale
dblTargetFic = Minus(dblTargetFic, CDbl(Left(strFic, Len(strFic) - 4)))
End If
strFic = Dir
Loop
GetFirstFileName = IIf(dblTargetFic <> 29991231235959#, dblTargetFic & ".pdf", "")
End Function
Function Minus(vA, vB)
' trouver le minimum entre deux valeurs
If vA > vB Then
Minus = vB
Else
Minus = vA
End If
End Function
Function fOpenRemoteReport(ByVal strMDB As String, _
ByVal strReport As String, _
ByVal aMode, ByVal aPage, _
ByVal iStart As Integer, ByVal iEnd As Integer) As Boolean
Dim objAccess As Access.Application
Dim lngRet As Long
' gestion d'erreurs
On Error GoTo fOpenRemoteReport_Err
If Len(Dir(strMDB)) > 0 Then
' creation de l'objet Access
Set objAccess = New Access.Application
With objAccess
'ouverture de la base
.OpenCurrentDatabase strMDB
'les commandes sont les memes que pour la base en cours
' hormis le "objAccess."
' ouverture de l'état
.DoCmd.OpenReport strReport, aMode
' impression des pages
.DoCmd.PrintOut aPage, iStart, iEnd, acHigh
' fermeture de l'état sans sauvegarde
.DoCmd.Close acReport, strReport, acSaveNo
End With
End If
fOpenRemoteReport_Exit:
' libération des objets
On Error Resume Next
objAccess.Quit
Set objAccess = Nothing
Exit Function
fOpenRemoteReport_Err:
fOpenRemoteReport = False
Select Case Err.Number
Case 7866:
'mdb ouverte en mode exclusif
MsgBox "The database you specified " & vbCrLf & strMDB & _
vbCrLf & "is currently open in exclusive mode. " & vbCrLf _
& vbCrLf & "Please reopen in shared mode and try again", _
vbExclamation + vbOKOnly, "Could not open database."
Case 2103:
'l'état n'existe pas
MsgBox "The report '" & strReport & _
"' doesn't exist in the Database " _
& vbCrLf & strMDB, _
vbExclamation + vbOKOnly, "report not found"
Case 7952:
'l"utilisateur a fermé le fichier mdb
fOpenRemoteReport = True
Case Else:
MsgBox "Error#: " & Err.Number & vbCrLf & Err.Description, _
vbCritical + vbOKOnly, "Runtime error"
End Select
Resume fOpenRemoteReport_Exit
End Function
'-------******************
Private Sub b_etat_service_pdf_Click()
qargs(13) = Me.LST_services.Value
getPDF ("info_dem_sem_par_projet")
End Sub |
Partager