如何解决在VBA中打开特定页码上的PDF
| 我正在尝试在访问表单上创建一个按钮,以允许用户查看表单中包含数据的相应页面(在这种情况下,部件号显示在表单上,我希望按钮可以打开零件标准文件以显示该零件的蓝图/图表) 我已尝试在文件路径的末尾使用Adobe的页面参数#page = pagenum,但是这样做不起作用。 这是我的代码(基本,我知道),但是我试图弄清楚该去哪里。出于显而易见的原因,我已经将文件路径简单地精简了-注意:这不是URL,而是文件路径(如果这很重要)。Private Sub Command80_Click()
Dim loc As String \'location of file
\'loc = Me.FileLoc
loc = \"G:\\*\\FileName.pdf#page=1\"
Debug.Print loc
\'Debug.Print Me.FileLoc
\'Debug.Print Me.FileName
Application.FollowHyperlink loc
End Sub
这样可以吗?我将继续阅读其他用户的帖子,以期找到一种解决方案,如果确实找到了解决方案,我会在此处注明。
谢谢!
更新资料
我已经找到了一种方法来执行此操作,只是现在我有1个小并发症。许多用户可能会使用不同版本的Acrobat或不同位置访问我的数据库。这是我的工作代码:
Private Sub Command2_Click()
pat1 = \"\"\"C:\\Program Files\\Adobe\\Reader 9.0\\Reader\\AcroRd32.exe\"\"\"
pat2 = \"/A \"\"page=20\"\"\"
pat3 = \"\"\"G:\\*\\FileName.pdf\"\"\"
Shell pat1 & \" \" & pat2 & \" \" & pat3,vbNormalFocus
End Sub
现在,这是我的关注。此代码从特定的文件路径打开AcroRd32.exe,如果我的用户将其存储在其他位置或使用其他版本,则此操作将无效。有没有人建议如何解决这个问题?
再次感谢! :)
解决方法
正确的方法可能是在系统注册表中查找acrobat reader可执行文件的位置。我发现这通常比其应有的麻烦多,尤其是如果我对将要安装程序的所有位置都有一定的控制权(例如,在单个Intranet中)。通常我最终会使用我编写的以下函数:
\'---------------------------------------------------------------------------------------
\' Procedure : FirstValidPath
\' Author : Mike
\' Date : 5/23/2008
\' Purpose : Returns the first valid path found in a list of potential paths.
\' Usage : Useful for locating files or folders that may be in different locations
\' on different users\' computers.
\' Notes - Directories must be passed with a trailing \"\\\" otherwise the function
\' will assume it is looking for a file with no extension.
\' - Returns Null if no valid path is found.
\' 5/6/11 : Accept Null parameters. If all parameters are Null,Null is returned.
\'---------------------------------------------------------------------------------------
\'
Function FirstValidPath(ParamArray Paths() As Variant) As Variant
Dim i As Integer
FirstValidPath = Null
If UBound(Paths) - LBound(Paths) >= 0 Then
For i = LBound(Paths) To UBound(Paths)
If Not IsNull(Paths(i)) Then
If Len(Dir(Paths(i))) > 0 Then
FirstValidPath = Paths(i)
Exit For
End If
End If
Next i
End If
End Function
该函数采用参数数组,因此您可以根据需要传递尽可能多的路径:
PathToUse = FirstValidPath(\"C:\\Program Files\\Adobe\\Reader 9.0\\Reader\\AcroRd32.exe\",_
\"C:\\Program Files\\Acrobat\\Reader.exe\",_
\"C:\\Program Files (x86)\\Acrobat\\Reader.exe\",_
\"C:\\Program Files\\Acrobat\\12\\Reader.exe\")
pat1 = \"\"\"\" & PathToUse & \"\"\"\"
, 注册表项是更好的选择,与文件位置不同,它们在系统之间具有一致性。
下面是三个函数,两个支持一个,以及一个测试这些函数的宏。
GetARE()(获取Adobe Reader可执行文件)根据在作为参数传递的预定义位置中的版本搜索返回正确的路径。这消除了为每个版本键入许多不同的关键位置的麻烦,并且在将来的版本发布并安装到用户的系统上时提供了一定的覆盖范围。
我已经安装了Reader的早期版本,以测试InstallPath密钥位置是否一致,直到存在相当过时的版本为止。实际上,mwolfe02和我俩都在同一位置拥有密钥,尽管我使用的是版本11,而在撰写本文时,他却使用的是10。我只能在x64系统上进行测试,但是您可以轻松地修改下面的代码以搜索x64和x86键。我希望像Adobe这样的大公司都遵守其惯例,因此即使发布了新版本的Reader,它也可能会在相当长的一段时间内运行而无需进行大量修改。
我写得很快,期望命名约定效率低下和不一致。
确实,确保路径几乎总是返回的最佳方法是使用\“ * / Acrobat Reader / XX.YY / InstallPath / \”循环通过VBA在注册表中循环搜索版本号,然后包含可执行文件基于对适当目录中的适当候选人的检查;但是,这并不是一个非常划算的解决方案。我的测试表明,版本之间在安装路径可以找到的位置以及可执行文件的名称可能有很多一致性,因此我选择了效率更高的方法(如果持久性较低)。
RegKeyRead()和RegKeyExists()来自:
http://vba-corner.livejournal.com/3054.html
我还没有修改他们的代码。考虑到对那个帖子的作者的感谢,该代码无论如何都不复杂,但是确实为我省去了自己编写代码的麻烦。
Function RegKeyRead(i_RegKey As String) As String
Dim myWS As Object
On Error Resume Next
\'access Windows scripting
Set myWS = CreateObject(\"WScript.Shell\")
\'read key from registry
RegKeyRead = myWS.RegRead(i_RegKey)
End Function
Function RegKeyExists(i_RegKey As String) As Boolean
Dim myWS As Object
On Error GoTo ErrorHandler
\'access Windows scripting
Set myWS = CreateObject(\"WScript.Shell\")
\'try to read the registry key
myWS.RegRead i_RegKey
\'key was found
RegKeyExists = True
Exit Function
ErrorHandler:
\'key was not found
RegKeyExists = False
End Function
Function GetARE(i_RegKey As String) As String
Dim InPath As String
Dim InKey As String
Dim Ind As Integer
Dim PriVer As String
Dim SubVer As String
Dim Exists As Boolean
Exists = False
PriVer = 1
SubVer = 0
For Ind = 1 To 1000
If SubVer > 9 Then
PriVer = PriVer + 1
SubVer = 0
End If
Exists = RegKeyExists(i_RegKey + \"\\\" + PriVer + \".\" + SubVer + \"\\InstallPath\\\")
SubVer = SubVer + 1
If Exists = True Then
SubVer = SubVer - 1
InKey = i_RegKey + \"\\\" + PriVer + \".\" + SubVer + \"\\InstallPath\\\"
InPath = RegKeyRead(InKey)
GetARE = InPath + \"\\AcroRd32.exe\"
Exit For
End If
Next
End Function
Sub test()
Dim rando As String
rando = GetARIP(\"HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Adobe\\Acrobat Reader\")
MsgBox (rando)
End Sub
,我记得Acrobat阅读器曾经包含一些ActiveX PDF阅读器对象,这些对象可用于Microsoft Office。其他公司也开发了类似的产品,其中一些(基本形式)甚至是免费提供的。
那可能是一个解决方案,不是吗?然后,您必须检查您的activeX PDF阅读器在其方法中是否支持直接页面访问,并将其与您的应用程序一起分发,或将其安装在用户的计算机上。它将避免您与后续的Acrobat Reader版本有关的所有开销,特别是当市场上有较新的版本并且您必须更新客户端界面时。
, 只是为了增加mwolfe02的答案,以下是一个函数,该函数尝试检索给定文件类型的可执行文件(它也使用引用的Levy注册表命令):
Function GetShellFileCommand(FileType As String,Optional Command As String)
Const KEY_ROOT As String = \"HKEY_CLASSES_ROOT\\\"
Dim sKey As String,sProgramClass As String
\' All File Extensions should start with a \".\"
If Left(FileType,1) <> \".\" Then FileType = \".\" & FileType
\' Check if the File Extension Key exists and Read the default string value
sKey = KEY_ROOT & FileType & \"\\\"
If RegKeyExists(sKey) Then
sProgramClass = RegKeyRead(sKey)
sKey = KEY_ROOT & sProgramClass & \"\\shell\\\"
If RegKeyExists(sKey) Then
\' If no command was passed,check the \"shell\" default string value,for a default command
If Command = vbNullString Then Command = RegKeyRead(sKey)
\' If no Default command was found,default to \"Open\"
If Command = vbNullString Then Command = \"Open\"
\' Check for the command
If RegKeyExists(sKey & Command & \"\\command\\\") Then GetShellFileCommand = RegKeyRead(sKey & Command & \"\\command\\\")
End If
End If
End Function
所以,
Debug.Print GetShellFileEx(\"PDF\")
输出:
\"C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe\" \"%1\"
您只需用要打开的文件替换\“%1 \”并添加所需的任何参数。
, 这是您可能会使用的代码。
Private Sub CommandButton3_Click()
Dim strFile As String
R = 0
If TextBox7 = \"CL\" Then
R = 2
\' Path and filename of PDF file
strFile = \"E:\\Users\\Test\\Cupertino Current system.pdf\"
ActiveWorkbook.FollowHyperlink strFile
End If
if R = 0 Then
MsgBox \"Wrong Code\"
ComboBox1 = \"\"
TextBox1 = Empty
\'ComboBox1.SetFocus
End If
End Sub
只需要走正确的路即可。希望这对您有帮助
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。