Bonjour à tous,

Je dois pouvoir imprimer des états sur différentes imprimantes. Je pensais avoir trouvé mon bonheur avec ce module de la FAQ :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
Option Compare Database
Option Explicit
 
Const MAX_SIZE = 255
Const MAX_SECTION = 2048
 
Declare Function aht_apiGetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileInt" _
(ByVal strAppName As String, ByVal strKeyName As String, ByVal intDefault As Integer, _
ByVal strFilename As String) As Integer
Declare Function aht_apiGetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
(ByVal strAppName As String, ByVal strKeyName As String, ByVal strDefault As String, _
ByVal strReturned As String, ByVal lngSize As Long, ByVal strFilename As String) As Long
Declare Function aht_apiGetProfileString Lib "kernel32" Alias "GetProfileStringA" _
(ByVal strAppName As String, ByVal strKeyName As String, ByVal strDefault As String, _
ByVal strReturned As String, ByVal lngSize As Long) As Long
Declare Function aht_apiGetProfileInt Lib "kernel32" Alias "GetProfileInt" _
(ByVal strAppName As String, ByVal strKeyName As String, ByVal intDefault As Integer) As Integer
Declare Function aht_apiGetProfileSection Lib "kernel32" Alias "GetProfileSectionA" _
(ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Declare Function aht_apiGetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" _
(ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, _
ByVal lpFileName As String) As Long
Declare Function aht_apiWritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal strAppName As String, ByVal strKeyName As String, ByVal strValue As String, _
ByVal strFilename As String) As Integer
Declare Function aht_apiWriteProfileString Lib "kernel32" Alias "WriteProfileStringA" _
(ByVal strAppName As String, ByVal strKeyName As String, ByVal strValue As String) As Integer
 
 
Type aht_tagDeviceRec
   drDeviceName As String
   drDriverName As String
   drPort As String
End Type
 
Type aht_tagDEVMODE
   dmDeviceName(1 To 32) As Byte
   dmSpecVersion As Integer
   dmDriverVersion As Integer
   dmSize As Integer
   dmDriverExtra As Integer
   dmFields As Long
   dmOrientation As Integer
   dmPaperSize As Integer
   dmPaperLength As Integer
   dmPaperWidth As Integer
   dmScale As Integer
   dmCopies As Integer
   dmDefaultSource As Integer
   dmPrintQuality As Integer
   dmColor As Integer
   dmDuplex As Integer
   dmYResolution As Integer
   dmTTOption As Integer
   dmCollate As Integer
   dmFormName(1 To 32) As Byte
   dmLogPixels As Integer
   dmBitsPerPixel As Long
   dmPelsWidth As Long
   dmPelsHeight As Long
   dmDisplayFlags As Long
   dmDisplayFrequency As Long
   dmICMMethod As Long
   dmICMIntent As Long
   dmMediaType As Long
   dmDitherType As Long
   dmICCManufacturer As Long
   dmICCModel As Long
   dmDriverExtraBytes(1 To 1024) As Byte
 
End Type
 
Type aht_tagDEVMODEStr
   DMStr As String * 1024
End Type
 
Type aht_tagDEVNAMES
   dnDriverOffset As Integer
   dnDeviceOffset As Integer
   dnOutputOffset As Integer
   dnDefault As Integer
End Type
 
Type aht_tagDEVNAMEStr
   DNStr As String * 4
End Type
 
Type aht_tagMIP
   xLeftMargin As Long
   yTopMargin As Long
   xRightMargin As Long
   yBotMargin As Long
   fDataOnly As Long
   xFormSize As Long
   yFormSize As Long
   fDefaultSize As Long
   cxColumns As Long
   xFormSpacing As Long
   yFormSpacing As Long
   radItemOrder As Long
   fFastPrinting As Long
   fDataSheet As Long
End Type
 
Type aht_tagMIPSTR
   MIPStr As String * 28
End Type
 
Function ahtFillPrinterList(ctl As Control, varID As Variant, varRow As Variant, varCol As Variant, varCode As Variant)
 
   Static atagDevices() As aht_tagDeviceRec
   Static intCount As Integer
   Dim varRetval As Variant
 
   Select Case varCode
      Case acLBInitialize
         intCount = ahtGetPrinterList(atagDevices())
         varRetval = True
 
      Case acLBOpen
         varRetval = Timer
 
      Case acLBGetRowCount
         varRetval = intCount
 
      Case acLBGetColumnCount
         varRetval = 1
 
      Case acLBGetValue
         varRetval = atagDevices(varRow + 1).drDeviceName & " sur " & _
          atagDevices(varRow + 1).drPort
 
      Case acLBEnd
         Erase atagDevices
   End Select
   ahtFillPrinterList = varRetval
End Function
 
Function ahtGetPrinterList(atagDevices() As aht_tagDeviceRec) As Integer
 
   Dim astrPrinters() As String
   Dim intCount As Integer
   Dim varPrinters As Variant
 
   varPrinters = ahtGetProfileSection("DEVICES")
 
   If Len(varPrinters & "") = 0 Then
      ahtGetPrinterList = 0
   Else
      intCount = GetDevices(varPrinters, atagDevices())
   End If
   ahtGetPrinterList = intCount
End Function
 
Private Function GetDevices(ByVal strPrinters As String, atagDevices() As aht_tagDeviceRec) As Integer
 
   Dim intI As Integer
   Dim strBuffer As String
   Dim intCount As Integer
 
   For intI = 1 To Len(strPrinters)
      If Mid$(strPrinters, intI, 1) = Chr$(0) Then
         intCount = intCount + 1
      End If
   Next intI
 
   ReDim atagDevices(1 To intCount)
 
   For intI = 1 To intCount
      strBuffer = ahtGetToken(strPrinters, Chr$(0), intI)
      atagDevices(intI).drDeviceName = ahtGetToken(strBuffer, "=", 1)
      strBuffer = ahtGetToken(strBuffer, "=", 2)
      atagDevices(intI).drDriverName = ahtGetToken(strBuffer, ",", 1)
      atagDevices(intI).drPort = ahtGetToken(strBuffer, ",", 2)
   Next intI
   GetDevices = intCount
End Function
 
 
Function ahtGetDefaultPrinter(dr As aht_tagDeviceRec) As Boolean
 
   Dim strBuffer As String
 
   strBuffer = ahtGetINIString("Windows", "Device")
   Debug.Print strBuffer & vbCrLf
 
   If Len(strBuffer) > 0 Then
      With dr
         .drDeviceName = ahtGetToken(strBuffer, ",", 1)
         .drDriverName = ahtGetToken(strBuffer, ",", 2)
         .drPort = ahtGetToken(strBuffer, ",", 3)
      End With
      ahtGetDefaultPrinter = True
   Else
      ahtGetDefaultPrinter = False
   End If
End Function
 
Function ahtSetDefaultPrinter(dr As aht_tagDeviceRec) As Boolean
 
   Dim strBuffer As String
 
   Debug.Print "Je passe dans le SetDefault"
 
   strBuffer = dr.drDeviceName & ","
   strBuffer = strBuffer & dr.drDriverName & ","
   strBuffer = strBuffer & dr.drPort
 
   ahtSetDefaultPrinter = (aht_apiWriteProfileString("Windows", _
    "Device", strBuffer) <> 0)
End Function
 
 
Function ahtGetToken(ByVal strValue As String, ByVal strDelimiter As String, ByVal intPiece As Integer) As Variant
 
   Dim intPos As Integer
   Dim intLastPos As Integer
   Dim intNewPos As Integer
 
   On Error GoTo ahtGetTokenExit
 
   strDelimiter = Left(strDelimiter, 1)
 
   If (InStr(strValue, strDelimiter) = 0) Or (intPiece <= 0) Then
      ahtGetToken = strValue
   Else
      intPos = 0
      intLastPos = 0
      Do While intPiece > 0
         intLastPos = intPos
         intNewPos = InStr(intPos + 1, strValue, strDelimiter)
         If intNewPos > 0 Then
               intPos = intNewPos
               intPiece = intPiece - 1
         Else
               intPos = Len(strValue) + 1
               Exit Do
         End If
      Loop
      If intPiece > 1 Then
         ahtGetToken = Null
      Else
         ahtGetToken = Mid$(strValue, intLastPos + 1, intPos - intLastPos - 1)
      End If
   End If
 
ahtGetTokenExit:
   Exit Function
 
ahtGetTokenErr:
   MsgBox "Error in ahtGetToken: " & Error & " (" & Err & ")"
   Resume ahtGetTokenExit
End Function
 
Function ahtGetPrivateIniString(ByVal strGroup As String, ByVal strItem As String, ByVal strFile As String) As Variant
 
 
   Dim intChars As Integer
   Dim strBuffer As String
 
   strBuffer = String(MAX_SIZE, 0)
   intChars = aht_apiGetPrivateProfileString(strGroup, strItem, "", strBuffer, MAX_SIZE, strFile)
   ahtGetPrivateIniString = Left(strBuffer, intChars)
End Function
 
Function ahtGetPrivateProfileSection(ByVal strGroup As String, ByVal strFile As String) As Variant
 
   Dim strBuffer As String
   Dim intCount As Integer
 
   strBuffer = Space(MAX_SECTION)
   intCount = aht_apiGetPrivateProfileSection(strGroup, strBuffer, MAX_SECTION, strFile)
   ahtGetPrivateProfileSection = Left(strBuffer, intCount)
End Function
 
Function ahtGetProfileSection(ByVal strGroup As String) As Variant
 
   Dim strBuffer As String
   Dim intCount As Integer
 
   strBuffer = Space(MAX_SECTION)
   intCount = aht_apiGetProfileSection(strGroup, strBuffer, MAX_SECTION)
   ahtGetProfileSection = Left(strBuffer, intCount)
End Function
 
Function ahtGetINIString(ByVal strGroup As String, ByVal strItem As String) As Variant
 
   Dim intChars As Integer
   Dim strBuffer As String
 
   strBuffer = String(MAX_SIZE, 0)
   intChars = aht_apiGetProfileString(strGroup, strItem, "", strBuffer, MAX_SIZE)
   ahtGetINIString = Left(strBuffer, intChars)
End Function
 
Function ahtGetPrivateINIInt(ByVal strGroup As String, ByVal strItem As String, ByVal strFile As String) As Variant
   ahtGetPrivateINIInt = aht_apiGetPrivateProfileInt(strGroup, strItem, -1, strFile)
End Function
 
Function ahtGetINIInt(ByVal strGroup As String, ByVal strItem As String) As Variant
 
   ahtGetINIInt = aht_apiGetProfileInt(strGroup, strItem, -1)
End Function
 
 
Function fChargementImprimantes()
    ' Fonction permettant de remplir une fois la table des imprimantes
    ' A lancer si nouvelle imprimante créée ou nouveau driver installé
    ' Cocher la case st_selection pour sélectionner les imprimantes
 
    Dim i As Integer
    Dim itNbPrt As Integer
    Dim rs As Recordset
    Static atagDevices() As aht_tagDeviceRec
 
On Error GoTo GestErr
 
    Set rs = CurrentDb().OpenRecordset("tbPrtList", dbOpenDynaset)
 
    ' Suppression des enregistrements de la table
 
    DoCmd.RunSQL "DELETE * FROM tbPrtList"
 
    ' Détermine le nombre d'imprimantes en cours sur le poste
    itNbPrt = ahtGetPrinterList(atagDevices())
 
    ' Ajoute les imprimantes dans la table pour les impressions
    For i = 1 To itNbPrt
        rs.AddNew
            rs![No_Prt].Value = i
            rs![tx_PrtNom].Value = atagDevices(i).drDeviceName
            rs![tx_PrtPort].Value = atagDevices(i).drPort
            rs![tx_PrtDriver].Value = atagDevices(i).drDriverName
        rs.Update
    Next i
 
FinLoad:
    ' Fermeture des objets
    rs.Close
 
    Set rs = Nothing
Exit Function
' Gestion des erreurs
GestErr:
 
    MsgBox "Erreur dans fChargementImprimantes : " & Error & " (" & Err & ")"
 
Resume FinLoad
 
End Function
 
Function fMultiImpression(stNomFichier As String)
 
' Impression d'états sur une ou plusieurs imprimantes
' Pré-requis : L'état doit être en imprimante par défaut
' Paramètre  : nom de l'état
 
On Error GoTo GestErr
 
If stNomFichier = "" Then Exit Function
 
Dim rs As Recordset
Dim dr As aht_tagDeviceRec
Dim tst As aht_tagDeviceRec
 
Dim stDvDefault As String   ' Unité de l'imprimante par défaut
Dim stDrDefault As String   ' Driver de l'imprimante par défaut
Dim stPrDefault As String   ' Port de l'imprimante par défaut
 
Dim stTstDevDefault As String   ' Unité de l'imprimante par défaut
Dim stTstDrDefault As String   ' Driver de l'imprimante par défaut
Dim stTstPrtDefault As String   ' Port de l'imprimante par défaut
 
 
    'Stockage des paramètres de l'imprimante par défaut actuel
    If ahtGetDefaultPrinter(dr) Then
       stDvDefault = dr.drDeviceName
       stDrDefault = dr.drDriverName
       stPrDefault = dr.drPort
    End If
 
    Debug.Print "stDefault" & vbCrLf & stDvDefault & vbCrLf & stDrDefault & vbCrLf & stPrDefault & vbCrLf & "stdefault"
 
Set rs = CurrentDb().OpenRecordset("SELECT * FROM tbPrtList WHERE st_selection = true")
 
    If Not rs.EOF And Not rs.BOF Then
        MsgBox "Je suis ici"
 
        While Not rs.EOF
            ' Chargement des paramètres de l'imprimante sélectionnée
            Debug.Print "Imprimante spécifique : " & vbCrLf & rs.Fields("[tx_PrtNom]") _
            & vbCrLf & rs.Fields("[tx_PrtDriver]") & vbCrLf & rs.Fields("[tx_PrtPort]") _
            & vbCrLf & "Imprimante spécifique"
            dr.drDeviceName = rs.Fields("[tx_PrtNom]")
            dr.drDriverName = rs.Fields("[tx_PrtDriver]")
            dr.drPort = rs.Fields("[tx_PrtPort]")
            ' L'imprimante devient imprimante par défaut
 
            ahtSetDefaultPrinter dr
 
            If ahtGetDefaultPrinter(dr) Then
                stTstDevDefault = dr.drDeviceName
                stTstDrDefault = dr.drDriverName
                stTstPrtDefault = dr.drPort
            End If
 
            Debug.Print "stTstDefault" & vbCrLf & stTstDevDefault & vbCrLf & stTstDrDefault & vbCrLf & stTstPrtDefault & vbCrLf & "stTstdefault"
 
            ' Impression
 
            DoCmd.OpenReport stNomFichier, , acViewNormal
 
            ' Fermeture fichier
            DoCmd.Close acReport, stNomFichier
 
            rs.MoveNext
        Wend
 
    End If
 
RestoreDftPrt:
 
    ' fermeture des objets
    rs.Close
     Set rs = Nothing
 
    ' Restauration de l'imprimante par défaut
    dr.drDeviceName = stDvDefault
    dr.drDriverName = stDrDefault
    dr.drPort = stPrDefault
    Debug.Print "stDefault" & vbCrLf & dr.drDeviceName & vbCrLf & dr.drDriverName & vbCrLf & dr.drPort & vbCrLf & "stdefault"
 
    ahtSetDefaultPrinter dr
 
Exit Function
 
' Gestion des erreurs
GestErr:
 
    MsgBox "Erreur dans fMultiImpression : " & Error & " (" & Err & ")"
Resume RestoreDftPrt
 
End Function
La function FMultiImpression ne m'envoie pas mon rapport sur l'imprimante souhaitée. Tous mes rapports sont imprimés systématiquement sur mon imprimante par défaut.

J'ai posé des pièges sous forme de debug.print et impression des valeurs pour essayer de comprendre d'où venait le problème, mais je ne vois vraiment pas.

Voici la table des imprimantes disponibles sur mon PC.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
no_Prt	tx_PrtNom	tx_PrtPort	tx_PrtDriver	st_Selection
1	Win2PDF	Ne00:	winspool	No
2	PDFCreator	Ne01:	winspool	No
3	PDF995	Ne02:	winspool	Yes
4	Microsoft XPS Document Writer	Ne03:	winspool	No
5	HP DeskJet 660C	LPT1:	winspool	No
6	Fax	Ne04:	winspool	No
7	EasyCoder C4 (203 dpi)	LPT1:	winspool	No
8	Dell AIO Printer A940	Ne05:	winspool	Yes
9	CAPTURE FAX BVRP	NUL:	winspool	No
10	Brother PT-9500PC	Ne06:	winspool	No
11	Brother PT-3600	Ne07:	winspool	No
12	Brother HL-1250	LPT1:	winspool	No
13	Brother DCP-135C Printer	Ne08:	winspool	No
14	Beeld printer voor PaperPort	Ne09:	winspool	No
Mon imprimante par défaut est la PDFCreator de la ligne 3. tous mes documents y sont imprimés. Si je change pour n'importe quelle autre imprimante, c'est cette imprimante qui reçoit alors tous mes documents.

Quelqu'un aurait-il une idée pour m'aider à passer outre de ce problème ?

En vous remerciant,

Geache