PDA

View Full Version : Solved: Closing Adobe after printing



BoardBabe
03-30-2005, 12:50 AM
Hello everybody,

I have a program which opens and prints attachments. The opening is no problem. When the user wants to print an attachment, I want that attachment to be printed without Adobe opening. The attachment is printed but I still have Adobe in the taskbar and running. How can I prevent this?

Thank you :)

Killian
04-01-2005, 04:48 AM
Hi!
I assume Acrobat is opening based on the file association in windows. I can't find a way of accessing it with GetObject (even when I cheat and find the class name another way) so the only way would then be use Windows API to find the process and kill it - a bit hardcore really...
However, if you set a reference to Acrobat in your project (Tools|References) you can take control by opening Acrobat when you start, keep it minimized then close it when you're done. Below is an example of doing that. Enjoy :)

Sub test()

'### requires a reference to Acrobat in VBEditor: Tools|References
Dim AcroApp As Acrobat.CAcroApp

'open Acrobat and minimize
Set AcroApp = CreateObject("AcroExch.App")
AcroApp.Minimize (1)
AcroApp.Show

'do stuff, i'll just wait 5 secs
Application.Wait (Now + TimeValue("0:00:05"))

''exit app and clean up
AcroApp.Exit
Set AcroApp = Nothing

End Sub

BoardBabe
04-01-2005, 07:13 AM
Thank you for your answer, Killian.

But I only have Acrobat Access 2.0 Type Library and Acrobat Control for ActiveX, so I dont get the Acrobat.CAcroApp what you wrote in your message.
What should I do now?

I use VBA Access.

Thank you very much.

Killian
04-01-2005, 09:16 AM
So now it gets interesting...
It can be done with the Windows API. Insert a new module and paste in the code below. You'll need to change the string constant for the Window text to match what is shown in your version of Acrobat.
You will then be able to use the MinimizeAcrobat and KillAcrobat routines as you need.
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Declare Function CloseWindow Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetLastError Lib "kernel32" () As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long

'###!!! the text in the main window of the app you want to close !!!###
Const myWindowText = "Adobe Acrobat Standard"

Const WM_CLOSE = &H10
Dim myHwnd As Long

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean

Dim sSave As String, Ret As Long
Dim a As Long

Ret = GetWindowTextLength(hwnd)
sSave = Space(Ret)
GetWindowText hwnd, sSave, Ret + 1
If InStr(1, sSave, myWindowText) <> 0 Then
myHwnd = hwnd
End If
'continue enumeration
EnumWindowsProc = True

End Function

Public Sub KillAcrobat()
myHwnd = 0
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
If myHwnd <> 0 Then SendMessage myHwnd, WM_CLOSE, -1, 0
End Sub

Public Sub MinimizeAcrobat()
myHwnd = 0
EnumWindows AddressOf EnumWindowsProc, ByVal 0&
If myHwnd <> 0 Then CloseWindow myHwnd
End Sub

BoardBabe
04-02-2005, 08:59 AM
Thank you so much Killian!

As soon as I get back from my holiday, I'll give it a try and let you know if it worked!

Thanks ;)

BoardBabe
04-10-2005, 11:37 PM
Hello Killian,

I found the solution with a mix of your code and the code I already had ;)

Thank you so much!

Killian
04-11-2005, 07:34 AM
Your welcome!

Marked as solved...