PDA

View Full Version : Solved: ShellExecute for PDF documents with OpenActions



Peanutmc
12-18-2009, 12:00 PM
Hopefully some of you smart Excel VBA programmers can help with this one.

Hi! I'm currently working on a VBA project within ArcMap. Although many of you will be unfamiliar with this program, my question can be extended to most of you since ShellExecute can be used for programs that support VBA.

My previous design was simplified, and used the standard 'Shell' command. I was able to pass OpenActions without any difficulties. The code for this was:


Dim strPath1, strPath2, strPath3 as string

strPath1 = """C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"""
strPath2 = "/A page=13=OpenActions""
strPath3 = "C:\DocumentA.pdf"

Shell strPath1 & " " & strPath2 & " " & strPath3, vbNormalFocus

This worked like a charm. However, it's extremely limiting as the program cannot easily be distributed since it requires Adobe Reader 9.0.
After some research I discovered that ShellExecute can perform this task using any version of Reader, however it seems to be unable to accept the OpenActions (in this situation page #). Here's the code for the ShellExecute command.


Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hWnd As Long, ByVal lpszOp As String, _
ByVal lpszFile As String, ByVal lpszParams As String, _
ByVal LpszDir As String, ByVal FsShowCmd As Long) _
As Long
Private Declare Function GetDesktopWindow Lib "User32" () As Long

Const SW_SHOWNORMAL = 1

Private Sub cmdPDF_Click()

nDT = GetDesktopWindow()

nApp = ShellExecute(nDT, "Open", "C:\DocumentA.pdf", "", "C:\", SW_SHOWNORMAL)
End Sub

I've tried several different methods, placing the 'OpenAction' in several different places with no success. If anyone can tell me...

a) If it's possible to use OpenActions with the ShellExecute

b) How to solve this issue if it is

Cheers,
Cameron

lucas
12-18-2009, 12:03 PM
You have two posts and they are both identical in different forums. Not nice. I have deleted your other post.

Please do not do this again.

Peanutmc
12-18-2009, 12:05 PM
Sorry about that. I wasn't entirely sure where to make the post as the topic spans many programs that use VBA. It wont happen again.

lucas
12-18-2009, 12:22 PM
If you feel the need to spread your question please just post a thread for instance in the excel forum with a link to your question.

The problem is that you get different people trying to help you and they don't know what has already beed addressed.

Thanks for understanding.

I have no knowledge of the app that you mention but we do have some smart guys on shellExecute who will probably try to offer suggestions.

Welcome to the board and I apologize if I was short in my first response.

Tommy
12-18-2009, 01:12 PM
Are you getting any kind of error? I ran your code in the shellexecute and it took a while to load but it worked.

Hey Steve are you keeping busy?

lucas
12-18-2009, 01:23 PM
Hi Tommy, great to catch you. I have been out of pocket for a few weeks......just read your email yesterday..

It's gotten really slow for me the last few months. How about yourself, staying hooked up?

Peanutmc
12-18-2009, 01:24 PM
Hey Tommy

I should have been a bit more clear. That code for ShellExecute works perfectly fine. What I need to do is add an OpenAction parameter for the pdf document.

<partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf>

This is what I'm currently having difficulty completing.

Cheers,
Cameron

Tommy
12-18-2009, 01:55 PM
Peanutmc, I will have to try at home I have the full suite of adobe and it keeps firing the acrobat version 6. So if some else can help ......


Hey Steve,

We're working 50's and still can't get it all done. We have 4 jobs with valleys and I get to train 2 nubees on how to detail them. They both are pretty sharp, and are ready so I may be out of pocket myself fro a few weeks LOL.

Tommy
12-20-2009, 07:52 AM
I hate to say it but this is the only way I could get it to work. I hope someone else will have a berrter solution.

Sub v()

Dim strPath1, strPath2, strPath3 As String
Dim RetVal
RetVal = 0
On Error Resume Next
strPath1 = "C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe"
strPath2 = "/A page=13=OpenActions"
strPath3 = "C:\DocumentA.pdf"
RetVal = Shell(strPath1 & " " & strPath2 & " " & strPath3, vbNormalFocus)
If RetVal = 0 Then
strPath1 = "C:\Program Files\Adobe\Reader 7.0\Reader\AcroRd32.exe"
RetVal = Shell(strPath1 & " " & strPath2 & " " & strPath3, vbNormalFocus)
End If
If RetVal = 0 Then
strPath1 = "C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"
RetVal = Shell(strPath1 & " " & strPath2 & " " & strPath3, vbNormalFocus)
End If
If RetVal = 0 Then Call MsgBox("Unable to open PDF File!", vbExclamation, "Bad PDF File!")
On Error GoTo 0
End Sub

Peanutmc
12-22-2009, 08:08 AM
Thanks for the input Tommy. I had considered this option as well, and currently I'm using it. I am however going to contribute a couple hours and attempt to tackle this a slightly different way.

If I manage to get it working, I'll share the code. I know most of the people using the form are unfamiliar with ArcGIS. That being said, the code generating should be compatible with most VBA programs.

Peanutmc
12-22-2009, 11:28 AM
Well after much frustration and few successes, I've finally been able to pass parameters for a PDF while opening Acrobat from its default location via the registry. I'm certain there are other methods to do this, but this method is short and sweet.

I'm posting this with the assumption that it will likely work with other programs that can pass command line parameters. Also, I expect this solution could be used for most if not all programs incorporating VBA.

VBA is highly limited by its access to APIs. These can be manually coded, but can be extremely time consuming and confusing. Fortunately, the procedure required for ShellExecute, through the Shell32.dll (or Shell.dll on 16bit), is reasonably simple.

SOLUTION


Option Explicit
'Declare the ShellExecute function by accessing the Shell library's procedure
Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" _
'Declare arguments
(ByVal hWnd As Long, ByVal lpszOp As String, _
ByVal lpszFile As String, ByVal lpszParams As String, _
ByVal LpszDir As String, ByVal FsShowCmd As Long) _
As Long

'Used to display a window
Const SW_SHOWNORMAL = 1

'Open pdf occurs on button click
Private Sub cmdPDF_Click()
Dim strPath, strParam As String

strPath = "C:\Example.pdf"
strParam = " /A " & Chr(34) & "page=14" & Chr(34) & strPath

Call ShellExecute(0&, "open", "AcroRd32.exe", strParam, "", SW_SHOWNORMAL)
End Sub

Hopefully this can help someone else out!

Cheers,
Cameron

efz
03-03-2010, 02:46 PM
thanks aswell really interesting