Forum Discussion
RoderickSullivan
Jul 05, 2024Copper Contributor
Excel hyperlink default software to open FileName .pdf
A hyperlink to a ".pdf" type file is created in an excel cell. Clicking on this hyperlink opens the file. The problem is that the default software to do this is Adobe Acrobat not the default softwa...
NikolinoDE
Jul 10, 2024Gold Contributor
Explanation of ShellExecute Function and VBA Code
The ShellExecute function in the shell32.dll library is used to perform various operations on files, such as opening them with their associated applications. This function is part of the Windows API and is commonly used in VBA to interact with the operating system.
ShellExecute Function Declaration
Vba Code is untested backup your file.
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
- hwnd: Handle to the parent window. Typically set to 0.
- lpOperation: The operation to perform (e.g., "open", "print"). In this case, "open" is used.
- lpFile: The file to operate on (e.g., the path to your PDF file).
- lpParameters: If the lpFile parameter specifies an executable file, lpParameters is a string that specifies the parameters to be passed to the application.
- lpDirectory: The default (working) directory. Typically set to "" (empty string).
- nShowCmd: Flags that specify how an application is to be displayed when it is opened. For example, 1 means "normal" (the application is open and in the foreground).
VBA Code Explanation
Here is the VBA code provided earlier, with comments to explain each part:
Vba Code is untested backup your file.
' Declare the ShellExecute function from the shell32.dll library
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
' Subroutine to open a PDF file
Sub OpenPDF(filePath As String)
' Call the ShellExecute function with parameters to open the PDF file
ShellExecute 0, "open", filePath, "", "", 1
End Sub
' Subroutine to open a PDF file whose path is specified in a cell
Sub OpenPDFfromCell()
Dim filePath As String
' Get the file path from cell A1 on Sheet1
filePath = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
' Call the OpenPDF subroutine to open the PDF file
OpenPDF(filePath)
End Sub
Declare Function ShellExecute...: This line declares the external function from the Windows API.
- Sub OpenPDF(filePath As String): This subroutine calls ShellExecute to open the PDF file specified by filePath.
- Sub OpenPDFfromCell(): This subroutine retrieves the PDF file path from cell A1 on Sheet1 and then calls OpenPDF.
Steps to Use the VBA Code
- Open the VBA Editor:
- Press Alt + F11.
- Insert a New Module:
- In the VBA editor, go to Insert > Module.
- Paste the Code:
- Paste the VBA code provided above into the new module.
- Assign Macro to a Button (Optional):
- Go to the Developer tab, click on Insert, choose Button (Form Control), draw the button on your sheet, and assign the OpenPDFfromCell macro to it.
Ensuring Excel Uses the Default PDF Viewer
While the VBA solution provides a way to open PDFs using the default application, it's crucial to ensure that your system settings are correctly configured to use your preferred PDF viewer. Here’s how to double-check and adjust these settings:
Verify Default App Settings in Windows
- Open Default Apps Settings:
- Press Windows + I to open the Settings app.
- Go to Apps > Default apps.
- Set Default PDF Viewer:
- Scroll down and click on Choose default apps by file type.
- Find .pdf in the list and make sure your preferred PDF viewer (e.g., Microsoft Edge, Adobe Reader, etc.) is set as the default app for this file type.
Shell Settings Command
- Open Command Prompt as Administrator:
- Press Windows + X and choose Command Prompt (Admin) or Windows PowerShell (Admin).
- Run the Following Command:
- Ensure the command points to your preferred PDF viewer executable:
Cmd Code is untested.
ftype pdffile="C:\Path\To\Your\PDFViewer.exe %1"
Additional Steps
If you still encounter issues, consider the following additional steps:
- Update Excel: Ensure that your Excel is updated to the latest version.
- Check for Conflicting Software: Sometimes third-party software or PDF viewers might conflict with the default settings.
- Reinstall the PDF Viewer: Uninstalling and reinstalling your preferred PDF viewer might reset and enforce the default file associations.
By following these steps and using the provided VBA solution, you should be able to ensure that your Excel hyperlinks open PDFs with your preferred default application. The text, steps and codes were created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
RoderickSullivan
Jul 11, 2024Copper Contributor
Thank you for your explanation of ShellExecute Function and VBA Code. I will work my way through your notes.
Did you have any comments on the pictures showing my registry settings?
Regards Roderick
- RoderickSullivanSep 22, 2024Copper Contributor
Further to our discussion, I recently had cause to uninstall the Default App I use to open .PDF files and then to re-install it. As a result the problem I was have with Excel Hyperlink not using my chosen default App is now fixed and all works as it is supposed to.