Close An Acrobat Reader PDF file from Visual Basic 6

Posted on 2009-04-14
Medium Priority
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 400 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 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!


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 800 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 800 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Viewers will learn the different options available in the Backstage view in Excel 2013.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

589 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