Solved

Close PDF Application - Access VBA

Posted on 2010-09-06
4
1,111 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
[X]
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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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