Results 1 to 5 of 5
  1. #1
    tuniwes is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2015
    Posts
    43

    Opening all pdf files in a folder

    Solving this problem would mean a lot to me. Our design engineers are typically "inventive" in the number of ways they can name a drawing file(pdf) and add all sorts of suffixes to the basic 6 character drawing number
    The network folder structure itself is not a problem but, to date I have had to explicitly name the pdf file including the .pdf extension using the shell command and looping through more than 100 ways they have added extensions to file names.

    What I need to do via an event button is to open all PDF files based on the current record in a given folder....sounds simple but I'm struggling to find a way to do it

    For example
    Item= 12345
    Folder =12345
    In the folder
    12345nb.pdf
    12345FT.pdf
    12345FA.pdf



    I need to automatically open all 3 files in the folder without explicitly naming them...like open folder 12345*.*

    Any help in how to do this would be a game changer for me

    Thanks in advance

    Ian

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,521
    paste this code into a module. (Alt-F11, insert , module)


    usage:
    OPenAllFilesInDir "c:\temp"




    Code:
    Public Sub OpenAllFilesInDir(ByVal pvDir)
    Dim FSO, oFolder, oFile, oRX
    Dim sTxt As String, sFile As String
    Const kTARG = "C:\temp\File2Import.xlsx"
    
    
    On Error GoTo errGetFiles
    
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = FSO.GetFolder(pvDir)
    
    
    If Right(pvDir, 1) <> "\" Then pvDir = pvDir & "\"
    
    
    For Each oFile In oFolder.Files
     'If InStr(oFile.Name, ".xls") > 0 Then  'xl only
          OpenNativeApp oFile.Name
     'End If
    Next
    
    
    endit:
    Set oFile = Nothing
    Set oFolder = Nothing
    Set FSO = Nothing
    Exit Sub
    
    
    errGetFiles:
    If Err = 3265 Then     'catch error if NO Import table errors
      ' "no errors found"
       Resume Next
    Else
      MsgBox Err.Description, , Err
    End If
    End Sub



    this next code opens the file, no matter the extension:


    Code:
    Option Compare Database
    Option Explicit
    
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Const SW_SHOWNORMAL = 1
    Const SE_ERR_FNF = 2&
    Const SE_ERR_PNF = 3&
    Const SE_ERR_ACCESSDENIED = 5&
    Const SE_ERR_OOM = 8&
    Const SE_ERR_DLLNOTFOUND = 32&
    Const SE_ERR_SHARE = 26&
    Const SE_ERR_ASSOCINCOMPLETE = 27&
    Const SE_ERR_DDETIMEOUT = 28&
    Const SE_ERR_DDEFAIL = 29&
    Const SE_ERR_DDEBUSY = 30&
    Const SE_ERR_NOASSOC = 31&
    Const ERROR_BAD_FORMAT = 11&
    
    
    Public Sub OpenNativeApp(ByVal psDocName As String)
    Dim r As Long, msg As String
    
    
    r = StartDoc(psDocName)
    If r <= 32 Then
        'There was an error
        Select Case r
            Case SE_ERR_FNF
                msg = "File not found"
            Case SE_ERR_PNF
                msg = "Path not found"
            Case SE_ERR_ACCESSDENIED
                msg = "Access denied"
            Case SE_ERR_OOM
                msg = "Out of memory"
            Case SE_ERR_DLLNOTFOUND
                msg = "DLL not found"
            Case SE_ERR_SHARE
                msg = "A sharing violation occurred"
            Case SE_ERR_ASSOCINCOMPLETE
                msg = "Incomplete or invalid file association"
            Case SE_ERR_DDETIMEOUT
                msg = "DDE Time out"
            Case SE_ERR_DDEFAIL
                msg = "DDE transaction failed"
            Case SE_ERR_DDEBUSY
                msg = "DDE busy"
            Case SE_ERR_NOASSOC
                msg = "No association for file extension"
            Case ERROR_BAD_FORMAT
                msg = "Invalid EXE file or error in EXE image"
            Case Else
                msg = "Unknown error"
        End Select
    '    MsgBox msg
    End If
    End Sub
    
    
    Private Function StartDoc(psDocName As String) As Long
    Dim Scr_hDC As Long
    
    
    Scr_hDC = GetDesktopWindow()
    StartDoc = ShellExecute(Scr_hDC, "Open", psDocName, "", "C:\", SW_SHOWNORMAL)
    End Function





    OpenNativeApp() will open ANY file via its extension....
    .pdf files will open in acrobat,
    .doc files in word
    etc


    USAGE:
    OpenNativeApp "c:\folder\file.xls"
    'opens in excel
    or
    OpenNativeApp field
    'opens item in field in native app

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,858

  4. #4
    tuniwes is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Nov 2015
    Posts
    43
    Thanks Welshgasman, you put me on the right track, a very simple solution, just need to add a little error handling but now the engineers can call the files whatever they dream up and I'll be able to open them

    Private Sub Command81_Click()
    Dim MyFile As String


    Dim exename As String


    exename = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe /o/s"

    MyFile = Dir(Me.path & "*.pdf")


    Shell exename & " " & Chr(34) & Me.path & MyFile & Chr(34), vbMaximizedFocus
    MyFile = Dir()
    Do While MyFile <> ""
    Shell exename & " " & Chr(34) & Me.path & MyFile & Chr(34), vbMaximizedFocus
    MyFile = Dir()


    Loop
    End Sub

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,858
    I'm not sure what that first Shell exename is for? or the second MyFile = ?
    You only need to read once outside the loop and again inside the loop?

    Code:
    Sub TestDir()
    Dim strFile As String, strFolder As String
    
    strFolder = "C:\Temp\"
    
    strFile = Dir(strFolder & "*.pdf")
    Do While Len(strFile) > 0
        Debug.Print strFile
        strFile = Dir()
    Loop
    
    End Sub
    HTH

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Files in A Folder
    By DMT Dave in forum Access
    Replies: 1
    Last Post: 03-06-2019, 03:32 AM
  2. Replies: 10
    Last Post: 09-09-2015, 03:25 AM
  3. Replies: 1
    Last Post: 05-15-2015, 10:58 AM
  4. How to get name of files in a folder
    By behnam in forum Programming
    Replies: 1
    Last Post: 09-18-2014, 07:46 AM
  5. Moving ALL Files in a Folder
    By JoeM in forum Programming
    Replies: 4
    Last Post: 04-19-2013, 01:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums