Results 1 to 7 of 7

Thread: Error Handling

  1. #1
    Join Date
    May 2009
    Posts
    104

    Unanswered: Error Handling

    I have a button that opens a pdf from a predefined folder with a name that is defined by a field in a record. This works great but some computers have another path for Adobe especially if the computer has a different version of windows. We have XP comptuers and Windows 7 computers here. My code is:

    Private Sub Command53_Click()
    Dim stAppName As String
    stAppName = "C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe U:\ProductionDrawings\" & [Part Number] & ".pdf"
    Call Shell(stAppName, 1)
    End Sub

    I know i need some error handling but i haven't had to do very much VB error handling so i'm not sure of the correct syntax. So i think it should read

    Private Sub Command53_Click()
    Dim stAppName As String
    stAppName = "C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe U:\ProductionDrawings\" & [Part Number] & ".pdf"
    Call Shell(stAppName, 1)
    On Error GoTo Next
    Dim stAppName As String
    stAppName = "C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe U:\ProductionDrawings\" & [Part Number] & ".pdf"
    Call Shell(stAppName, 1)
    End Sub

    Or something like this.
    Any help would be greatly appreciated.

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    you are probably better off reading the location of Acrobat from the registry
    its at
    acrobat | shell | open | command
    ms access read registry - Google Search
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Here's a solution that allows to properly open a file without knowing where the corresponding application is located or even what application must be called. The only condition is that the file type must be registered in Windows (and the corresponding application must be installed, of course):
    Code:
    Declare Function ShellExecute 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
    
    Function ShellExec(Byval FileName as String)
    
        Const SW_SHOWNORMAL = 1
        
        ShellExecute &H0, vbNullString, FileName, vbNullString, vbNullString, SW_SHOWNORMAL
    End Function
    FileName must be the full path to the file you want to open. Ex:
    Code:
    ShellExec "C:\Documents and Settings\SinnDHo\My documents\Windows Azure No SQL White Paper.pdf"
    Have a nice day!

  4. #4
    Join Date
    May 2009
    Posts
    104
    I tried both of the suggestions and can't seem to figure out how to integrate your ideas into what code i have. This is a little outside my current skill-set. Maybe i can trouble you for a little more help. They both seem to be exactly what i need i just need a little help with getting the code right. Thank you in advance for all your help.

  5. #5
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    1. Create a new module and paste the following code into it:
    Code:
    Declare Function ShellExecute 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
    
    Function ShellExec(Byval FileName as String)
    
        Const SW_SHOWNORMAL = 1
        
        ShellExecute &H0, vbNullString, FileName, vbNullString, vbNullString, SW_SHOWNORMAL
    
    End Function
    2. Change the existing code like this:
    Code:
    Private Sub Command53_Click()
    
        ShellExec "U:\ProductionDrawings\" & [Part Number] & ".pdf"
    
    End Sub
    Have a nice day!

  6. #6
    Join Date
    May 2009
    Posts
    104
    Thank you Sinndho that works perfectly. I actually like i better that what i had because it doesn't error out when it can't find the file,it just doesn't do anything.

  7. #7
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    You're welcome!
    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •