【问题标题】:How to open a pdf file at a specifc page with information in a word table如何在单词表中的特定页面打开pdf文件
【发布时间】:2013-04-17 19:30:18
【问题描述】:

我有一个包含 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

【问题讨论】:

  • 如果您在此处显示您找到的代码可能会有所帮助。给自己一个机会... :)
  • @KazJaw:give yourself a chance...我喜欢那句台词! :)
  • @KazJaw:我使用 Excel 宏中的代码编辑了帖子。
  • 此代码为该行显示“编译错误:未定义用户定义类型”:code Dim aRow As Row code 另外,您在哪里定义文档位置?
  • 不是提问和小记号的地方。请改用 cmets。

标签: vba pdf ms-word


【解决方案1】:

这是一种方法:

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

【讨论】:

  • 谢谢,这正是我想要的。它工作得很好。我只需要添加一些验证以避免错误。另一个站点确实是一个很好的参考。再次感谢!!
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 2015-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多