I suggest using the Shell command.
Private Sub Text1_Click(ByVal lvarX As Long, ByVal lvarY As Long) Dim retVal As Double Dim appPathAndName As String Dim filePathAndName As String On Error GoTo ErrHandler appPathAndName = "AcroRd32.exe" filePathAndName = "C:\Temp\myPDF.pdf" retVal = Shell(appPathAndName & " " & filePathAndName, vbNormalFocus) Exit Sub ErrHandler: MsgBox ("Error # " & Str(retVal) & vbCrLf & Err.Description) End Sub
You may have to specify the full path to Acrobat Reader or ensure it's included into the "path" environment variable.
I use the ShellExecute command, and that eliminates the need for specifying that the pdf should open in AcroRd32.exe. Just let Windows open the file in its default software. Use this code:
Private Sub Text1_Click(ByVal lvarX As Long, ByVal lvarY As Long) Dim filePathAndName As String filePathAndName = "C:\Temp\myPDF.pdf" ShellExecute 0, "Open", filePathAndName, "", "", vbNormalNoFocus End Sub
and just make sure you put the following line at the very beginning of all the code in your vba (not just inside the function, but at line 1 in your file):
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
You could also use the standard PI-ProcessBook "Button" object.
Create one "Button" symbol that will open one of your PDF files. Enable scripting for this Button object.
Then call the following code to change the PDF file name every time you change from one Element to the other:
Dim objButtonDef As ButtonDefinition Set objButtonDef = Button1.GetDefinition() objButtonDef.Action = "manual.pdf" Button1.SetDefinition objButtonDef
Once this button is working in Run mode, you can move it over the Value objet and make it in invisible by changing its "Visible" property.
Philippe @ Awegon
Thanks very much to all of you for the great advice. I chose to implement the ShellExecute command and it works flawlessly.
Np. Good thing with that is that it works great with any file type, so if you have a need to open different files you could very well create a generic OpenFile(string filePathAndName) function.
I have a similar challenge. Using a string builder data reference in AF, I am generating a dynamic file path string. I want to get a button into ProcessBook that will let me open the file at the location of the string value. So, in Asle Frantzen 's example, how would I open a file at the location of the value of an AF attribute, instead of C:\Temp\myPDF.pdf as per the line below? :
- filePathAndName = "C:\Temp\myPDF.pdf"
For what it's worth, Button Links port to PI Vision, but VBA doesn't, so that's important to consider if you may eventually migrate there. You might have to edit the Button Link because of web server security, but if it's a button link, we will be able to evaluate it in our secret project, if it's VBA, probably not.
PS. I may be somewhat biased, but PI World 2018 is a great place to find out about our latest initiatives including tracking me down and chatting.
I can only hope for a unicorn to whisk me to PI World 2018.
I'd settle for a dynamic link on a button.