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

x
?
Solved

Close PDF Application - Access VBA

Posted on 2010-09-06
4
Medium Priority
?
1,133 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

916 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