Close An Acrobat Reader PDF file from Visual Basic 6

Posted on 2009-04-14
Last Modified: 2013-11-25
Have a VB6 form that opens and displays a PDF file.  When exiting the VB6 form, I want to close the PDF file.  After returning to the calling program, I want to Kill the PDF file that is residing in a directory.

I have submitted the code that I a using along with note commemts.
Would appreciate any help.


General Declarations
    Private Const SW_SHOWNORMAL = 0.75
    Private Const SW_CLOSE = True
    Private 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
Form Activate
        '=== Put in the routine to display the PDF file ==================================
        'Note: the PassString(6) carries the directory path and name of the previously
        ' saved PDF file
            ShellExecute Me.hwnd, "open", PassString(6), vbNullString, _
            vbNullString, SW_SHOWNORMAL
            'Note: The above code seems to work fine.
This is my attempt to close the file so that when 
Form Unload
        ShellExecute Me.hwnd, "close", PassString(6), vbNullString,  vbNullString, 1
         ' Note: the code executes but the PDF file is not closed, it just moves down to
        ' the task bar at bottom of screen

Open in new window

Question by:JackVannoy

Author Comment

ID: 24159958
I really need help on this problem, so  would like to change the number of points from 500 to 100 but not sure how to do this.

LVL 39

Assisted Solution

itsmeandnobodyelse earned 100 total points
ID: 24163708
You need to send a WM_CLOSE message to the top window of the Acrobat Reader to close it.

In C++ it was

   HWND hwndAR = FindWindow("title of the acrobat window");
   if (hwndAR != NULL)
       PostMessage(hwndAR, WM_CLOSE, NULL, NULL);

I hope you know how you can convert that to VB6.

Author Comment

ID: 24164127
In my comment wishing to change the points, I said from 500 to 100.  That was in error.  I really want to change the points from 500 to 1000.
Sorry about that.

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.


Author Comment

ID: 24164143
Sorry, I didn't realize that 500 was the maximum points for any one question.

LVL 23

Accepted Solution

gecko_au2003 earned 200 total points
ID: 24165239
Code is from the below website

which will explain how it works - obviously for the paths for the application if you are wanting to distribute this then you may want to consider using the FSO ( file system object ) to search the program files folder to get the correct path for the application.
Listing B
Option Explicit
 'API's Function Declarations
 Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
 Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
    ByVal hwnd As Long, _
    ByVal nIndex As Long) As Long
 Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
    ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) As Long
 Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As Any, _
    ByVal lpWindowName As String) As Long
 'API Constants
 Public Const GWL_STYLE = -16
 Public Const WS_DISABLED = &H8000000
 Public Const WM_CANCELMODE = &H1F
 Public Const WM_CLOSE = &H10
 Public Function IsTaskRunning(sWindowName As String) As Boolean
    Dim hwnd As Long, hWndOffline As Long
    On Error GoTo IsTaskRunning_Eh
    'get handle of the application
    'if handle is 0 the application is currently not running
    hwnd = FindWindow(0&, sWindowName)
    If hwnd = 0 Then
        IsTaskRunning = False
        Exit Function
        IsTaskRunning = True
    End If
        Exit Function
    Call ShowError(sWindowName, "IsTaskRunning")
 End Function
 Public Function EndTask(sWindowName As String) As Integer
    Dim X As Long, ReturnVal As Long, TargetHwnd As Long
    'find handle of the application
    TargetHwnd = FindWindow(0&, sWindowName)
    If TargetHwnd = 0 Then Exit Function
    If IsWindow(TargetHwnd) = False Then
        GoTo EndTaskFail
    'close application
        If Not (GetWindowLong(TargetHwnd, GWL_STYLE) And WS_DISABLED) Then
            X = PostMessage(TargetHwnd, WM_CLOSE, 0, 0&)
        End If
    End If
    GoTo EndTaskSucceed
    ReturnVal = False
    MsgBox "EndTask: cannot terminate " & sWindowName & " task"
    GoTo EndTaskEndSub
    ReturnVal = True
    EndTask% = ReturnVal
 End Function
 Public Function ShowError(sText As String, sProcName As String)
   'this function displays an error that occurred
    Dim sMsg As String
    sMsg = "Error # " & Str(Err.Number) & " was generated by " _
         & Err.Source & vbCrLf & Err.Description
    MsgBox sMsg, vbCritical, sText & Space(1) & sProcName
    Exit Function
 End Function

Open in new window

LVL 23

Expert Comment

ID: 24165247

the 2 above sites should help you on your quest :)
LVL 27

Assisted Solution

jjafferr earned 200 total points
ID: 24166376
This code worked for me.

You will have to get the running program name, and your best bet is the Task manager, from the Processes tab.

just change this line:
CONST strprogram = "outlook.exe"
CONST strprogram = "AcroRd32.exe"


Author Closing Comment

ID: 31570009
The reason for the point split is that I took portions of all of the three experts who responded, and arrived at a soultion.

Thank all of you very much for your responses.


Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many companies are making the switch from Microsoft to Google Apps ( Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now