Solved

Close PDF Application - Access VBA

Posted on 2010-09-06
4
1,098 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

760 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

21 Experts available now in Live!

Get 1:1 Help Now