file - VBA:文件打开到 PDF 页面

标签 file pdf file-io excel vba

我有一个 Excel 电子表格。电子表格中的一列包含 pdf 文件的名称;页码 (name of file;5)。

如何编写 VBA 函数,以便当用户单击该列中的任何单元格时,文件名和页码作为变量传递,并且 pdf 文件打开到指定的页面?

最佳答案

为达到这个:

  • 为工作表的 SelectionChanged 事件创建一个事件处理程序。
  • 为单元格的值创建一个解析函数。
  • 创建一个使用页码参数启动 Acrobat 的子例程。

  • 请参阅以下示例代码。常数应根据您的系统进行更改。此代码需要粘贴到工作表的 VBA 宏编辑器中。
    Private Const MyPathColumn As Integer = 3
    Private Const PathToAcrobatExe As String = _
                  "C:\Program Files\Adobe\Reader 8.0\Reader\Acrord32.exe"
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        On Error GoTo errHnd
        If Target.Cells.Count = 1 Then
            If Target.Column = MyPathColumn Then
                If Len(Target.text) > 0 Then
                    Dim filePath As String
                    Dim pageNumber As Integer
                    Call ParsePath(Target.text, filePath, pageNumber)
                    Call RunPdf(filePath, pageNumber)
                End If
            End If
        End If
        Exit Sub
    errHnd:
        Call MsgBox("Error Opening File", vbCritical)
    End Sub
    
    Private Function getShellCommand(filePath As String, _
                             pageNumber As Integer) As String
        getShellCommand = PathToAcrobatExe & " /A ""page=" _
                       & CStr(pageNumber) & """ """ _
                       & filePath & """"
    End Function
    
    Private Sub RunPdf(filePath As String, pageNumber As Integer)
        Call Shell(getShellCommand(filePath, pageNumber), _
                   vbMaximizedFocus)
    End Sub
    
    Private Sub ParsePath(text As String, ByRef filePath As String, _
                          ByRef pageNumber As Integer)
        Dim parts() As String
        parts = Split(text, ";")
        filePath = parts(0)
        pageNumber = CInt(parts(1))
    End Sub
    

    关于file - VBA:文件打开到 PDF 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2494997/

    相关文章:

    windows - 检测文件是否在 Windows 中打开的编程方法是什么?

    c - 使用 strcmp 终止文件写入不起作用

    c# - 将两个或多个 Crystal Reports 合并为一个 PDF

    pdf - 调整 PDF 比例以进行打印

    image - 缩小以适合FOP中的图像

    c - 使用 fscanf 从包含整数数组的文件中读取

    c# - 从服务器读取文件总是被映射 C :\drive

    file - 将 mws 文件转换为不带 Maple 的文本

    php - 用PHP编写二进制文件

    java - 读取文本文件的最后一行后出现 NoSuchElementException 错误