Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3203
  • Last Modified:

Close An Acrobat Reader PDF file from Visual Basic 6

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

3 Solutions
JackVannoyAuthor Commented:
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.

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.
JackVannoyAuthor Commented:
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.

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

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

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



the 2 above sites should help you on your quest :)
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"

JackVannoyAuthor Commented:
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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now