Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display PDF in Excel VBA UserForm

Tags:

excel

pdf

vba

adobe

I am running Excel 2016, which may be relevant if the below is a compatibility issue...

In short, I am trying to display a PDF, embedded in a UserForm in Excel.

I have a UserForm, say UserForm1.

I have enabled the following extra references:

  • Microsoft Visual Basic for Applications Extensibility 5.3
  • Adobe Acrobat Browser Control Type Library 1.0

This allows me to add the Adobe PDF Reader as an "Additional Control"

Additional Controls dialog

The control appears as a hatched box icon (bottom left), which I'm not sure it's meant to. Then if I try to add one of these objects to UserForm1 (both programmatically and in design view) it gives me an error

Element not found

For reference, the relevant lines of VBA I was using were:

Dim PDFviewer As AcroPDF
Set PDFviewer = PDForm.Frame1.Controls.Add("AcroPDF.PDF.1")

Which I took from this Adobe forums thread: https://forums.adobe.com/thread/1065554

Resources online suggest it might be that the AcroPDF control is no longer supported. If so, is there another way to achieve what I want?

like image 875
Wolfie Avatar asked Dec 22 '16 21:12

Wolfie


1 Answers

As an alternative to using the AcroPDF, try using the WebBrowser Object.

It requires including the additional control

Microsoft Web Browser

Add a WeBrowser on the UserForm named WebBrowser1

Private Sub UserForm_Click()
    Me.WebBrowser1.Navigate "about:blank"
    Me.WebBrowser1.Document.write "<HTML><Body><embed src=""C:\temp\SO_Answers\test.pdf"" width=""100%"" height=""100%"" /></Body></HTML>"
End Sub

You can just .Navigate to the PDF directly, but, to quote my comment:

"It's safer to use the html part, depending on the machine settings, sometimes direct navigation will initiate download instead of display."

like image 124
cyboashu Avatar answered Nov 14 '22 08:11

cyboashu