我正在尝试在Access2010中编写一个按钮,允许用户打开一个与表单上的文本框中输入相同名称的pdf文件。想法是测试人员将在测试编号字段中输入测试编号,这是一个文本框,然后最终用户可以单击将打开该pdf文件的按钮。我试图命名一个未绑定的文本框,然后将其设置为一个字符串变量,但是当我进入代码时,我无法读取它。 pdf文件位于T:\ Lab \ PHOTO1 \,然后有子文件夹编号为32 33 34 ..等。当数字开始下一个设置(如35000)时,将创建一个名为35的新文件夹。
Public Sub PDF1()
Dim fso As New FileSystemObject
Dim strTestno As String
Dim strPath As String
Dim fldbaseFolder As Folder
Dim fldSubFolder As Folder
Dim frmForm1 As Form
'Set frmForm1 = [Form_Test Request Form]
'set strTestno to value in text box
strTestno = [Form_Test Request Form].Test_Number_Combo1
'set your path
strPath = "C:\Users\usb14322\Desktop\TEMP\"
DirFile = strPath & strTestno & ".pdf"
'Get a referemce to the Folder object
Set fldbaseFolder = fso.GetFolder(strPath)
'Iterate through subfolders.
For Each fldSubFolder In fldbaseFolder.SubFolders
If strTestno = "" Then
MsgBox "File does not exist"
Else
Application.FollowHyperlink "fldSubFolder" & "strTestno" & ".pdf"
'Len(Dir(DirFile & strTestno).open
'Debug.Print strTestno
End If
Next
End Sub
@ktacks由于文本限制,我不得不添加这个'答案'...将下面显示的代码粘贴到一个新模块中,然后调用它:
strExt = Right(strFileName, 3) ' Get file extension
fvstr_Application = GetAssociatedProgram(strExt) ' Find Program
strDocPath = "<Your file folder\" & strFileName
strLaunch = fvstr_Application & " " & Chr(34) & strDocPath & Chr(34)
DoEvents
lvstr_AppID = Shell(strLaunch, 1) ' Run specified Application
DoEvents
<NEW MODULE>
Option Compare Database
Option Explicit
Global gv_Version As String
Declare Function FindExecutable Lib _
"shell32.dll" Alias "FindExecutableA" _
(ByVal lpFile As String, ByVal lpDirectory _
As String, ByVal lpResult As String) As Long
Declare Function GetTempFileName Lib _
"kernel32" Alias "GetTempFileNameA" (ByVal _
lpszPath As String, ByVal lpPrefixString _
As String, ByVal wUnique As Long, ByVal _
lpTempFileName As String) As Long
Declare Function GetTempPath Lib _
"kernel32" Alias "GetTempPathA" (ByVal _
nBufferLength As Long, ByVal lpBuffer As _
String) As Long
Public Function GetAssociatedProgram(ByVal _
Extension As String) As String
' This function will return the path to the program that is registered to handle
' certain types of files. In our case, we pass 'mdb' as the extension and expect
' to get where 'C:\Program Files\Microsoft....\msaccess.exe' is located.
Dim Path As String
Dim FileName As String
Dim nRet As Long
Const MAX_PATH As Long = 260
'Create a temporary file
Path = String$(MAX_PATH, 0)
If GetTempPath(MAX_PATH, Path) Then
FileName = String$(MAX_PATH, 0)
If GetTempFileName(Path, "~", 0, FileName) Then
FileName = Left$(FileName, _
InStr(FileName, vbNullChar) - 1)
'Rename it to use supplied extension
Name FileName As Left$(FileName, _
InStr(FileName, ".")) & Extension
FileName = Left$(FileName, _
InStr(FileName, ".")) & Extension
Path = String$(MAX_PATH, 0) 'Get name of associated EXE
Call FindExecutable(FileName, _
vbNullString, Path)
GetAssociatedProgram = Left$( _
Path, InStr(Path, vbNullChar) - 1)
Kill FileName 'Delete the temporary file
End If
End If
End Function
我创建了你拥有的环境(未绑定的文本框,设置为路径等),我打开PDF没有问题。由于我不知道您的代码是什么样的,如果您可以在遇到的行上发布代码和结果,将会很有帮助。
实际上,我开发的代码是“版本独立的”并且将打开任何类型的文件(只要您安装了相关的程序)。即,如果'Office11或Office14等,则无需更改代码)