vba 显示文本链接以从 Excel 打开特定的 PDF 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4340696/
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-11 12:22:43  来源:igfitidea点击:

Display text link to open specific PDF page from Excel

pdfexcel-vbaexcel-2003vbaexcel

提问by Fiona

I found a VBA code online that opens up an internal (shared drive) PDF document page in IE (e.g. goes to page 8 of PDF file). I would like to display text in the cell for a user to click (e.g. "Click here to view").

我在网上找到了一个 VBA 代码,它可以在 IE 中打开一个内部(共享驱动器)PDF 文档页面(例如转到 PDF 文件的第 8 页)。我想在单元格中显示文本供用户单击(例如“单击此处查看”)。

Problem: The cell currently displays '0' and I have to go to the function bar and hit [Enter] to execute.

问题:单元格当前显示“0”,我必须转到功能栏并按 [Enter] 才能执行。

Excel Version: 2003

Excel 版本: 2003

Function call:

函数调用

=GoToPDFpage("S:\...x_2011.pdf",8)

VBA Code:

VBA 代码

Function GoToPDFpage(Fname As String, pg As Integer)
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Navigate Fname & "#page=" & pg
.Visible = True
End With
End Function

:EDIT:

:编辑:

I was able to display text, but it's still not a link like I wanted.

我能够显示文本,但它仍然不是我想要的链接。

="Click to view" & GoToPDFpage("S:\...x_2011.pdf",8)

Thank you for your help.

感谢您的帮助。

回答by CaBieberach

Try Menu->Data->Data Validation. In the 2nd tab you can write your message.

尝试菜单->数据->数据验证。在第二个选项卡中,您可以编写您的消息。

回答by CaBieberach

If you dont have a high complex workbook/worksheet you could try the following:

如果您没有高度复杂的工作簿/工作表,您可以尝试以下操作:

Turn the "Click to view" cell into a Hyperlink with following characteristics.

将“单击以查看”单元格转换为具有以下特征的超链接。

  • Make it point to itself
  • The text inside the cell must always be the string Page=plus the number that you what the pdf to open in. Eg.: Page=8
  • 让它指向自己
  • 单元格内的文本必须始终是字符串Page=加上您打开 pdf 的数字。例如:Page=8

Then go to the workseet module and paste the following code:

然后转到工作表模块并粘贴以下代码:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

    If Left(ActiveCell.Value, 4) = "Page" Then

        GoToPDFpage Range("A1").Value, Mid(ActiveCell.Value, 6)
        'This code asumes that the file address is writen in the cell A1

    End If
    '
End Sub
'

The above written code will trigger every time you run a hyperlink in the worksheet.
As the hyperlink always point to itself, the "Activecell.Value" will always have the page number that you want to open.

每次在工作表中运行超链接时,都会触发上述编写的代码。
由于超链接始终指向自身,“Activecell.Value”将始终包含您要打开的页码。

I'm assuming that you can put the file address in the cell A1. You could modify this portion to point to any other cell. (including: The cell to the right of the current hyperlink, etc).

我假设您可以将文件地址放在单元格 A1 中。您可以修改此部分以指向任何其他单元格。(包括:当前超链接右侧的单元格等)。

This might not be the best option, but if you only need a quick feature in a couple of cells, it might be enough.

这可能不是最好的选择,但如果您只需要几个单元格中的快速功能,这可能就足够了。

Hope it helps !

希望能帮助到你 !



EDIT:To make each HLink reference to itself, you can select all the cells where you have the links and then run this procedure:

编辑:要使每个 HLink 引用自身,您可以选择拥有链接的所有单元格,然后运行以下过程:

Sub RefHLink()
    Dim xCell As Range

    For Each xCell In Selection

        ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:="", SubAddress:= _
        xCell.Address, ScreenTip:="Click Here", TextToDisplay:="Page="
    Next xCell

End Sub

回答by boojiboy16

how about letting excel write a batch file then running it?

让excel写一个批处理文件然后运行它怎么样?

*edit paths to pdf and AcroRd32.exe

*编辑pdf和AcroRd32.exe的路径

Sub batfile()
  Dim retVal
  filePath = "path\pdf.bat"
  pg = 2
  Open filePath For Output As #1
  Print #1, "Start /Max /w " & Chr(34) & "Current E-book" & Chr(34) & " " & Chr(34) & "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" & Chr(34) & " /a " & Chr(34) & "page=" & pg & Chr(34) & "  " & Chr(34) & "H:\Documents\RPG\Dragonlance\New folder\Sample File.pdf" & Chr(34) & ""
  Close #1
  retVal = Shell(strFilePath)
End Sub