Solved

Close PDF Application - Access VBA

Posted on 2010-09-06
4
1,099 Views
Last Modified: 2012-05-10
I have opened a pdf application in access vba to create a file but i am having problems closing it in vba.  The code used to open it is detailed below.

ShellExecuteA Application.hWndAccessApp, "open", sOutFile, vbNullstring, vbNullstring, 1

All idea's welcome - file or application can be closed (prefer application)
0
Comment
Question by:SweetingA
  • 2
4 Comments
 

Expert Comment

by:magdy99
ID: 33615031
0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 500 total points
ID: 33615112
This is the issue with doing this.
Once you open the file via automation, you loose control of it.
Then Windows has control of it.

This is why you need all this custom code to do something simple like close the file.

Remember that using the code you posted, you are not really using VBA to open the file, you are using the API.
When you use VBA to open an Excel file, you are actually creating an Instance of Excel that you can manipulate in code (Open, Close Hide, Save, ...etc)

This is not the case with your code
 
Another approach would be to simply "Display" the PFD in a web browser control on a form, then simply close the form normally.
Or put a "close" button on the form and set the "Cancel" property to Yes, so simply pressing "Esc" closes the form.
;-)

Here is a sample

;-)

JeffCoachman
Access-BasicLinkFormOnlyPDFWebBr.mdb
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33615175
when u use shellexecute it returns you the processid, you can then send a message telling it to close it

Call PostMessage(lRv, &H10, 0, 0)

where lRv is the return value from shellexecute

you need to add the following api dec in your module though

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


Alternatively you could use FindWindow
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

find your window, again returns lRv and use that to close it

    Dim lHwnd As Long
   
    lHwnd = FindWindow(vbNullString, sTitle)
    If lHwnd = 0 Then
        MsgBox "Error finding target window handle"
        Exit Sub
    End If

    PostMessage lHwnd, &H10, 0, 0
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 33619910
Glad I could help..
;-)

Jeff
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

920 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

15 Experts available now in Live!

Get 1:1 Help Now