Consulting

Results 1 to 7 of 7

Thread: Solved: Closing Adobe after printing

  1. #1

    Solved: Closing Adobe after printing

    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

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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

    [VBA]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[/VBA]
    K :-)

  3. #3
    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.

  4. #4
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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.
    [VBA]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
    [/VBA]
    K :-)

  5. #5
    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

  6. #6
    Hello Killian,

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

    Thank you so much!

  7. #7
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Your welcome!

    Marked as solved...
    K :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •