Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Close PDF Application - Access VBA

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

670 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