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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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.

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.


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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

729 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