ktacks
ktacks

Reputation: 11

Program a button to open PDF located in subfolders

I am trying to program a button in Access2010 that would allow the user to open a pdf file with the same name that is entered into a text box on a form. The idea is that the tester will enter the test number in the Test Number field, which is a text box, then the end user can click a button that will open that pdf file. I have tried to name an unbound text box and then set it to a string variable, but I cannot get it to read anything when I step into the code. The pdf files are located in T:\Lab\PHOTO1\ and then there are subfolders numbered like 32 33 34..etc. When the numbers start to the next set such as 35000, then a new folder is created with the name 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

Upvotes: 1

Views: 7096

Answers (2)

Wayne G. Dunn
Wayne G. Dunn

Reputation: 4312

@ktacks I had to add this 'Answer' due to text limitations... Paste the code shown below into a new module, then call it like:

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

Upvotes: 1

Wayne G. Dunn
Wayne G. Dunn

Reputation: 4312

I have created the environment you have (unbound text box, set to path, etc.) and I have no problem opening a PDF. Since I have no idea what your code looks like, it would be helpful if you could post the code and the results at the line you encounter.

Actually, the code I have developed is 'version independent' and will open any type of file (as long as you have the associated program installed). i.e. no need to change code if 'Office11 or Office14, etc.)

Upvotes: 0

Related Questions