Results 1 to 3 of 3

Thread: [RESOLVED] Opening a PDF with default application

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Resolved [RESOLVED] Opening a PDF with default application

    Okay so I have an Excel database that uses a form to lookup values in a table and based on the value gets a file path.

    The issue is that the files were originally all .doc files. I used the following code to open them:

    VB Code:
    1. Public Function OpenFile(strfile As String)
    2.      Dim x As Variant
    3.      'call word to the program
    4.      'C:\WINDOWS\SYSTEM32\WINWORD.exe
    5.      x = Shell("WINWORD.exe " & strfile, 1)
    6. End Function
    VB Code:
    1. Private Sub OpenButton_Click()
    2. 'set the sheet
    3. Set Sheet = ActiveWorkbook.Worksheets("DCA_Data")
    4. Dim strfile As String
    5.  
    6. If listi = 0 Then
    7.     'error if nothing is selected
    8.     response = MsgBox("No Value Selected", 0, "No Values")
    9. Else
    10.     'call the path
    11.     strfile = Sheet.Cells(listi, 4)
    12.     'open the path
    13.     OpenFile (strfile)
    14. End If
    15. End Sub

    It is now required that I open the files as PDFs. I have converted all of the files and have updated the tables. I need to modify this code so that it opens the PDF using the computer's default application. This file will be on a server so many different computers have access to it. This means that different versions and programs may be being used. Any help would be much appreciated. Thanks.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Opening a PDF with default application

    Use the ShellExecute API as it opens the passed files with their default associated application. Only the file to open's path is required, not the program itself.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    4. ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    5.  
    6. Private Const SW_SHOWNORMAL As Long = 1
    7. Private Const SW_SHOWMINIMIZED As Long = 2
    8. Private Const SW_SHOWMAXIMIZED As Long = 3
    9.  
    10. Private Const ERROR_FILE_NOT_FOUND As Long = 2&
    11. Private Const ERROR_PATH_NOT_FOUND As Long = 3&
    12. Private Const ERROR_BAD_FORMAT As Long = 11&
    13. Private Const SE_ERR_ACCESSDENIED As Long = 5
    14. Private Const SE_ERR_ASSOCINCOMPLETE As Long = 27
    15. Private Const SE_ERR_DDEBUSY As Long = 30
    16. Private Const SE_ERR_DDEFAIL As Long = 29
    17. Private Const SE_ERR_DDETIMEOUT As Long = 28
    18. Private Const SE_ERR_DLLNOTFOUND As Long = 32
    19. Private Const SE_ERR_FNF As Long = 2
    20. Private Const SE_ERR_NOASSOC As Long = 31
    21. Private Const SE_ERR_OOM As Long = 8
    22. Private Const SE_ERR_PNF As Long = 3
    23. Private Const SE_ERR_SHARE As Long = 26
    24.  
    25. Private Sub Command1_Click()
    26.     On Error GoTo MyError
    27.     Dim lRet As Long
    28.     lRet = ShellExecute(Application.Hwnd, "Open", "C:\MyPath\MyFile.pdf", vbNullstring, "C:\", SW_SHOWNORMAL)
    29.     If lRet <= 32 Then
    30.         Select Case lRet
    31.             Case 0
    32.                 'MsgBox "The operating system is out of memory or resources."
    33.             Case ERROR_FILE_NOT_FOUND
    34.                 'MsgBox "The specified file was not found."
    35.             Case ERROR_PATH_NOT_FOUND
    36.                 'MsgBox "The specified path was not found."
    37.             Case ERROR_BAD_FORMAT
    38.                 'MsgBox "The .EXE file is invalid (non-Win32 .EXE or error in .EXE image)."
    39.             Case SE_ERR_ACCESSDENIED
    40.                 'MsgBox "The operating system denied access to the specified file."
    41.             Case SE_ERR_ASSOCINCOMPLETE
    42.                 'MsgBox "The filename association is incomplete or invalid."
    43.             Case SE_ERR_DDEBUSY
    44.                 'MsgBox "The DDE transaction could not be completed because other DDE transactions were being processed."
    45.             Case SE_ERR_DDEFAIL
    46.                 'MsgBox "The DDE transaction failed."
    47.             Case SE_ERR_DDETIMEOUT
    48.                 'MsgBox "The DDE transaction could not be completed because the request timed out."
    49.             Case SE_ERR_DLLNOTFOUND
    50.                 'MsgBox "The specified dynamic-link library was not found."
    51.             Case SE_ERR_FNF
    52.                 'MsgBox "The specified file was not found."
    53.             Case SE_ERR_NOASSOC
    54.                 'MsgBox "There is no application associated with the given filename extension."
    55.             Case SE_ERR_OOM
    56.                 'MsgBox "There was not enough memory to complete the operation."
    57.             Case SE_ERR_PNF
    58.                 'MsgBox "The specified path was not found."
    59.             Case SE_ERR_SHARE
    60.                 'MsgBox "A sharing violation occurred."
    61.         End Select
    62.     End If
    63.     Exit Sub
    64. MyError:
    65.     MsgBox Err.Number & " - " & Err.Description
    66. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    6

    Re: Opening a PDF with default application

    Thanks man. That works awsome. I don't know how you guys figure this stuff out, but you rule. Thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width