Piloter une impression depuis Lotuscript/VB (ici TOPCAD)

par Daniel CORDIER Email

Ici le but et de lancer une impression en fonction du type de fichier (extension). 2 modes sont gérés, l'impression directe si l'application cible gère l'impression depuis l'explorateur Windows ou en pilotant une application (par message windows, ici nous pilotons une application de CAO (TOPCAD).
le code à été écrit en lotuscript mais doit être portable très facilement en VB.

Suite:

commencons d'abord par les déclarations :

Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (Byval hwnd As Long, Byval lpOperation As String, Byval lpFile As String,Byval lpParameters As String,Byval lpDirectory As String, Byval nShowCmd As Long) As Long
Declare Sub Sleep32 Lib "kernel32" Alias "Sleep" (Byval dwMilliseconds As Long)
Declare Function GetActiveWindow& Lib "User32" ()
Declare Function NextWindow Lib "user32" Alias "GetWindow" (Byval hwnd As Long, Byval wCmd As Long) As Long
Declare Function TxtWindow Lib "user32" Alias "GetWindowTextA" (Byval hwnd As Long, Byval lpString As String, Byval cch As Long) As Long
Declare Function idPapa Lib "user32" Alias "GetParent" (Byval hwnd As Long) As Long
Declare Function RecupHandleBureau Lib "user32" Alias "GetDesktopWindow" () As Long
Declare Sub keybd_event Lib "user32" (Byval bVk As Integer, Byval bScan As Integer, Byval dwFlags As Long, Byval dwExtraInfo As Long)
Dim CheminPlan As String
Dim CheminFichier As String
Dim TitreApplication As String
Dim WshShell As Variant

Const VK_MENU = &H12
Const VK_I = &H49
Const VK_Q = &H51
Const VK_W = &h57

ensuite la routine qui détermine quelle méthode je dois employer

Set WshShell = CreateObject("WScript.Shell")
If Right$(CheminFichier,3) = "D2D" Or Right$(CheminFichier,3) = "D3D" Then
result = Shell( "cmd.exe /C G:\topcad\topbend\D3D.cmd " & CheminFichier ,3)
sleep32 5000
imprimeTOPCAD
Else
ActiveWin=GetActiveWindow()
Retour=shellexecute(ActiveWin,"print",CheminFichier+Chr(0),""+Chr(0),cheminPlan +Chr(0),3)
End If

et enfin la méthode imprimeTOPCAD qui pilote l'application avec les routines associées

Sub imprimeTOPCAD()
Dim kelfenetre As Long
Dim NF As String
Dim TimeOUT As Integer

NF = "Topbend "

kelfenetre = 0
timeout = 0

Doevents
While kelfenetre = 0 And timeout < 60
kelfenetre = listeProcess(NF)
Sleep32 1000
timeout = timeout + 1
Doevents
Wend

If kelfenetre = 0 Then
Print "TOPCAD Non trouvé"
Exit Sub
End If

Sleep32 1000
AltW
Doevents

Sleep32 1000
AltQ
Doevents
End Sub


Function listeProcess( NF As String) As Long
Dim r As Long
Dim trouve As Integer
Dim hwnd As Long
Dim papa As Long
Dim titrefenetre As String * 255
Dim titre As String

TitreApplication = ""
trouve = False
listeProcess = 0
hwnd = NextWindow(RecupHandleBureau(), 5)

Do While hwnd <> 0 And trouve = False
TitreFenetre = String(255, 0)
r = TxtWindow(hwnd, TitreFenetre, 255)
papa = idPapa(hwnd)
If TitreFenetre <> String(255, 0) Then

Titre = TitreFenetre
Titre = Left(Titre, r)

If Left(Ucase(Trim(Titre)), 8) = Ucase(NF) Then
listeProcess = hwnd
TitreApplication = titre
trouve = True
WshShell.AppActivate TitreApplication
End If
End If
If trouve = False Then
Doevents
hwnd = NextWindow(hwnd, 2)
End If
Loop

End Function


Sub AltW()

WshShell.AppActivate TitreApplication
keybd_event VK_MENU, 0, 0, 0
keybd_event VK_W, 0, 0, 0
keybd_event VK_W, 0, 2, 0
keybd_event VK_MENU, 0, 2, 0
Sleep32 500

End Sub


Sub AltQ()

WshShell.AppActivate TitreApplication
keybd_event VK_MENU, 0, 0, 0
keybd_event VK_Q, 0, 0, 0
keybd_event VK_Q, 0, 2, 0
keybd_event VK_MENU, 0, 2, 0
Sleep32 500

End Sub

a savoir que pour lancer l'impression sous TOPCAD, je transmet à l'application un Alt-W qui va lancer une macro commande TOPCAD d'impression et un Alt-Q qui demande de quitter l'application.

Voila pour aujourd'hui, bon coding