How to Open a PDF from a Picture on a Word VBA UserForm
Question details
The user wants to trigger a PDF file to open in the system's default PDF viewer by clicking on an image inside a Microsoft Word VBA UserForm, without interrupting the form's data entry process.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Developing a Word VBA UserForm and needing to provide access to external PDF documents via image clicks.
- Observed behavior
- The developer needs the PDF to open in a separate default application (like WPS PDF or Adobe Reader) while the UserForm remains active and responsive.
Ensure the target PDF file is saved in a known directory on your computer and verify its exact file path before adding it to your VBA script.
Use the Shell Function to Execute a Command Line
The most reliable method to open a file in its default application from VBA is using the Shell function combined with the Windows command prompt.
By utilizing the Windows 'start' command through the VBA Shell function, you can launch any file type in its default application. This approach ensures that your UserForm remains open and active while the PDF viewer launches in a separate window.
Open your Word document and press ALT + F11 to access the VBA Editor environment.
In the Project Explorer, double-click your UserForm to open the design view. Then, double-click the Picture control to automatically generate a Picture_Click() event subroutine.
Inside the subroutine, declare a string variable by typing 'Dim filePath As String' and assign your PDF's absolute path to it (e.g., filePath = "C:\example.pdf").
Add the execution command: Shell "cmd /c start " & Chr(34) & filePath & Chr(34), vbNormalFocus
Run the UserForm by pressing F5, then click the picture control to verify that the PDF opens in your default PDF application.

Execute Macros and UserForms with WPS Office
WPS Office provides robust support for VBA and macros, allowing you to run your Word UserForms and execute Shell commands seamlessly without altering your existing code. You can edit documents, run scripts, and view PDFs all in one lightweight suite.
- 1. Download and Install: Download and install WPS Office on your Windows computer.
- 2. Open Your Document: Open your .docm or .doc file containing the VBA UserForm in WPS Writer.
- 3. Enable Macros: Go to the Developer tab on the top ribbon and click 'Enable Macros' to ensure your scripts run safely.
- 4. Run the UserForm: Launch your UserForm and click the picture to seamlessly trigger the PDF in WPS PDF.

Frequently Asked Questions
Can I open files other than PDFs using this VBA method?
Yes, the 'cmd /c start' command will open any specified file using the default application assigned to that file type in your Windows operating system.
Why does the command prompt flash when clicking the picture?
The brief flash happens because the Shell function briefly calls the Windows command prompt (cmd.exe) to execute the start command. You can minimize this visual interruption by using the 'vbHide' parameter instead of 'vbNormalFocus'.
What happens if the PDF file path is incorrect?
If the file path is invalid or the file does not exist, the command will fail silently or display a command prompt error. It is recommended to include error handling in your VBA code to check if the file exists using the Dir() function before executing the Shell command.
Will this VBA code work on a Mac?
No, this specific script uses Windows Command Prompt (cmd) which is exclusively for Windows. To achieve this on a Mac, you would need to use the MacScript function or AppleScriptTask to call a macOS terminal command.




