VBA & Macro Problems

How to Open a PDF from a Picture on a Word VBA UserForm

Elise WilliamsElise Williams Oct 10, 2026 870 views

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.

How to Open a PDF from a Picture on a Word VBA UserForm
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.
Before you start

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.

Solution 1Recommended

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.

1
Access the VBA Editor

Open your Word document and press ALT + F11 to access the VBA Editor environment.

2
Open the UserForm Design View

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.

3
Define the File Path Variable

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").

4
Insert the Shell Command

Add the execution command: Shell "cmd /c start " & Chr(34) & filePath & Chr(34), vbNormalFocus

5
Test the UserForm

Run the UserForm by pressing F5, then click the picture control to verify that the PDF opens in your default PDF application.

Use the Shell Function to Execute a Command Line
Path Formatting: Using Chr(34) wraps the file path in double quotes, which is essential if your directory folders or file name contain spaces.
Seamless VBA Support in WPS Office

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. 1. Download and Install: Download and install WPS Office on your Windows computer.
  2. 2. Open Your Document: Open your .docm or .doc file containing the VBA UserForm in WPS Writer.
  3. 3. Enable Macros: Go to the Developer tab on the top ribbon and click 'Enable Macros' to ensure your scripts run safely.
  4. 4. Run the UserForm: Launch your UserForm and click the picture to seamlessly trigger the PDF in WPS PDF.
Fully compatible with Microsoft Word documents and VBA scripts.Built-in PDF viewer to handle your external file links seamlessly.Lightweight installation with powerful built-in developer tools.Tabbed interface allows managing multiple documents and UserForms easily.
microsoft office alternative - wps office

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.