0

我有一个包含 3 列表格的 Word 文档。我正在尝试创建一个宏,它将在第 3 列的页码处打开一个 pdf 文件(名称存储在第 2 列中)。

我在 Excel 中找到了一个宏,当我选择页码单元格时会自动打开它,但在 Word 中没有。许多用户正在使用 Word 文档并且不想切换到 Excel。

最好是使用键盘快捷键激活宏,如果在表中,它将在光标所在行中指定的页面打开文件。如果光标不在表中,可能会显示错误。

谢谢。

[编辑]

这是 Excel 宏的代码。请注意,在此示例中,Adobe Reader 路径和程序存储在单元格 B1 中,文件位于单元格 B2 中。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

   If Target.Row > 4 And Target.Column = 1 And Target.Value > 0 Then

      vAdobe = ActiveSheet.Cells(1, 2)
      vDocument = ActiveSheet.Cells(2, 2)

      vPage = Target.Value

      result = Shell(vAdobe & " /A ""page=" & vPage & """ " & vDocument, vbNormalFocus)

   End If
End Sub
4

1 回答 1

0

这是一种方法:

Sub OpenPDF()
Dim aRow As Row
Dim vDocument As String
Dim vPage As String
Dim vAdobe As String
Dim result As Long

vAdobe = <File path to Acrobat.exe here.>
If Selection.Information(wdWithInTable) = True Then
    Set aRow = Selection.Range.Rows(1)
    vDocument = Trim(Replace(Replace(aRow.Cells(2).Range.Text, "", ""), vbCr, ""))
    vPage = Trim(Replace(Replace(aRow.Cells(3).Range.Text, "", ""), vbCr, ""))
    result = Shell(vAdobe & " /A ""page=" & vPage & """ " & vDocument, vbNormalFocus)
Else: 'Some error message here.
End If
End Sub

您可能需要使用那些 Replace 方法;我发现有必要删除从表格单元格中提取的任何文本末尾出现的一些表格字符。

另一个需要设置参考的选项可以在这里找到:http ://www.myengineeringworld.net/2012/07/vba-macro-to-open-pdf-file.html

于 2013-04-19T20:28:44.130 回答