Opening an Adobe Acrobat PDF from MSI VBScript
I encountered this when trying to open release notes from a Windows Installer project. I couldn't just link to the PDF file the same way you link to an executable. It would be nice if you could, though, and automatically use the file extension to use the appropriate app to open it. Alas, it was not to be.
I have a installation "welcome" dialog with a button, and that button says, "View Release Notes". I create a custom VBScript action called OpenRelNotes, and associate this custom action with a DoAction event on the button. Now we just have to fill in the code for the custom action.
First, we have to assume that Adobe Acrobat is not in the path, and we have to fish for the executable.
const HKEY_CLASSES_ROOT = &H80000000
sComputer = "."
Set oReg = GetObject("winmgmts:\\" & sComputer & "\root\default:StdRegProv")
sKeyPath = "AcroExch.Document\CurVer"
oReg.GetStringValue(HKEY_CLASSES_ROOT, sKeyPath, vbNullString, sExeRegPath)
sKeyPath = sExeRegPath & "\shell\Open\command"
oReg.GetStringValue(HKEY_CLASSES_ROOT, sKeyPath, vbNullString, sExeFilePath)
After this is finished, we have a variable sExeFilePath that stores the path to the executable, but in a strange format:
"C:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe" "%1"
We need to replace that "%1" with the path to our PDF. Since I added the PDF to my MSI project as a support file, I need to get the path to the install temp directory. For this, we have to scarf the MSI session property "SUPPORTDIR", like so:
sPdfFilePath = Session.Property("SUPPORTDIR") & "\release-notes.pdf"
So now we need to replace "$1" with sPdfFilePath. I need to use VBScript's built-in regex functionality to do this:
Set oRegEx = New RegExp oRegEx.Pattern = "%1" sCommandLine = oRegEx.Replace(sExeFilePath, sPdfFilePath)
The sCommandLine variable holds the executable filepath and argument I need to open the PDF file. I use a WScript shell to open it thusly:
Set oWshShell = CreateObject("WScript.Shell")
oWshShell.Run(sCommandLine, 1, TRUE)
Set oWshShell = nothing
The "1" in the second parameter specifies that execution of the Acrobat Reader app does not take place in a console window.