问题描述
我有一个产品表,其中有特定产品用户手册的 pdf.我将模型名称及其文件路径存储在我的产品表中(在 Access 中).我在 Access 中创建了一个表单,它允许用户按产品名称进行搜索,它缩小了文件数量,并在列表框中显示了搜索结果.然而,我最大的问题是打开实际的 PDF.它打开文件,但我必须准确地存储文件路径,并且文件的路径很长.有没有办法在不使用 Followhyperlink 命令的情况下打开 PDF 超链接?或者有没有办法在我的列表框中只显示 pdf 的文件名而不是整个路径名?如果我更改产品表中的显示文本,它不会打开超链接,我会收到错误消息.任何帮助将不胜感激!
I have a table of products where there is say a pdf for a specific products user manual. I'm storing the model name and it's file path in my products table (in Access). I've created a form in Access that allows the user to search by product name and it narrows down the number of files and shows the results from the search in a list box. However my biggest problem is opening the actual PDF. It opens the file, but I have to store the file path exactly how it is and the path of the files are long. Is there a way to open the PDF hyperlinks without using the Followhyperlink command? Or is there a way that I can show only the file name of the pdf in my list box rather than the entire path name? If I change the display text in my products table it doesn't open the hyperlink, I get an error. Any help would be greatly appreciated!
推荐答案
Application.FollowHyperLink() 存在安全问题,尤其是在打开网络驱动器上的文件时.见例如在这里:http://blogannath.blogspot.de/2011/04/microsoft-access-tips-tricks-opening.html
Application.FollowHyperLink() has problems with security, especially when opening files on a network drive. See e.g. here: http://blogannath.blogspot.de/2011/04/microsoft-access-tips-tricks-opening.html
更好的方法是 ShellExecute() API 函数.基本上它看起来像这样(从 http://access.mvps.org/access/api/api0018.htm ):
A better method is the ShellExecute() API function.Essentially it looks like this (trimmed from http://access.mvps.org/access/api/api0018.htm ):
' This code was originally written by Dev Ashish.
' http://access.mvps.org/access/api/api0018.htm
Private Declare Function apiShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) _
As Long
Public Const WIN_NORMAL = 1 'Open Normal
Private Const ERROR_SUCCESS = 32&
Public Function fHandleFile(stFile As String) As Boolean
Dim lRet As Long
lRet = apiShellExecute(hWndAccessApp(), "Open", stFile, vbNullString, vbNullString, WIN_NORMAL)
If lRet > ERROR_SUCCESS Then
' OK
fHandleFile = True
Else
Select Case lRet
' Handle various errors
End Select
fHandleFile = False
End If
End Function
现在为您的列表框:将其设置为 2 列,第一列是模型名称,第二列是文件路径.将第二列的列宽设置为0,这样就看不见了.
Now for your listbox:Set it to 2 columns, the first being the model name, the second the file path.Set the column width of the second column to 0, so it will be invisible.
并且在双击事件中,使用第二列(文件路径)调用 fHandleFile:
And in the doubleclick event, call fHandleFile with the second column (file path):
Private Sub lstManuals_DblClick(Cancel As Integer)
Call fHandleFile(Me.lstManuals.Column(1))
End Sub
这篇关于在 Access 中打开超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!