在 Excel VBA 用户窗体中显示 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41292195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:45:14  来源:igfitidea点击:

Display PDF in Excel VBA UserForm

excelvbapdfadobe

提问by Wolfie

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

我正在运行 Excel 2016,如果以下是兼容性问题,这可能是相关的...

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

简而言之,我正在尝试显示嵌入在 Excel 用户窗体中的 PDF。

I have a UserForm, say UserForm1.

我有一个用户表单,比如说UserForm1

I have enabled the following extra references:

我启用了以下额外参考:

  • Microsoft Visual Basic for Applications Extensibility 5.3
  • Adobe Acrobat Browser Control Type Library 1.0
  • Microsoft Visual Basic for Applications Extensibility 5.3
  • Adobe Acrobat 浏览器控件类型库 1.0

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

这允许我将其添加Adobe PDF Reader为“附加控件”

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

该控件显示为一个带阴影的框图标(左下角),我不确定它是不是这样。然后,如果我尝试将这些对象之一添加到UserForm1(以编程方式和在设计视图中),它会给我一个错误

Element not found

未找到元素

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

作为参考,我使用的 VBA 的相关行是:

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

我从这个 Adob​​e 论坛线程中获取的: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?

在线资源表明可能不再支持 AcroPDF 控件。如果是这样,还有另一种方法可以实现我想要的吗?

回答by cyboashu

As an alternative to using the AcroPDF, try using the WebBrowserObject.

作为使用 的替代方法AcroPDF,请尝试使用WebBrowser对象。

It requires including the additional control

它需要包括额外的控制

Microsoft Web Browser

Microsoft Web Browser

Add a WeBrowser on the UserForm named WebBrowser1

在名为的用户窗体上添加一个 WebBrowser 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 .Navigateto the PDF directly, but, to quote my comment:

您可以直接.Navigate访问 PDF,但是,引用我的评论:

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

“使用html部分更安全,取决于机器设置,有时直接导航会启动下载而不是显示。”