Display PDF in Excel VBA UserForm

旧街凉风 提交于 2019-12-04 22:41:22

问题


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"

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?


回答1:


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



来源:https://stackoverflow.com/questions/41292195/display-pdf-in-excel-vba-userform

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!