Consulting

Results 1 to 6 of 6

Thread: Open any file in any application from VBA

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Post Open any file in any application from VBA

    Hello everyone ,
    I would like to write a function which would basically allow the user to open any file from Excel.
    I've got a user form which records the path and filename of a range of documentation, which will be in Excel, Word, PPT, PDF, HTML,... etc.
    I would like to have some code which would just open the file as if it had been double-clicked in windows.

    I've seen below code which is used for 32 bit not for 64 bit , then I tried to convert it to 64 bit but still not working so i hope someone can help me with this as I'm still leaning excel vba .

    Private Declare PtrSafe Function GetDesktopWindow _
        Lib "user32" () As LongPtr
    
     Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" _
        Alias "ShellExecuteA" ( _
        ByVal hwnd As LongPtr, _
        ByVal lpOperation As String, _
        ByVal lpFile As String, _
        ByVal lpParameters As String, _
        ByVal lpDirectory As String, _
        ByVal nShowCmd As LongPtr) _
        As LongPtr
        Private Const SW_HIDE As LongPtr = 0
        Private Const SW_NORMAL As LongPtr = 1
        Private Const SW_MAXIMIZE As LongPtr = 3
        Private Const SW_MINIMIZE As LongPtr = 6
        '// ShellExecute API.
        'Use Windows file associations to provide the easiest way to process operations > Open, Print,Play or Explore
        'by passing just the FullPathFilename of the item.
    
    Public Function ShellOper(strFileExe As String, _
        Optional strOperation As String, _
        Optional nShowCmd As Double) As LongPtr
        Dim hWndDesk As LongPtr
        hWndDesk = GetDesktopWindow()
        If Len(strOperation) = 0 Then strOperation = "Open"
        If Len(Dir(strFileExe)) = 0 Then GoTo ErrH
        '// Failure >> <=32
        ShellOper = ShellExecute(hWndDesk, strOperation, strFileExe, 0, 0, nShowCmd)
        If ShellOper <= 32 Then
            MsgBox "Couldn't " & strOperation & " " & strFileExe
        End If
        Exit Function
        ErrH:
        ShellOper = -1
     End Function
    
    Sub Tester()
        Dim Ret
        'Substitute here your Doc full path
        Ret = ShellOper("C:Intel\Logs", "Open", SW_MAXIMIZE)
        'Typical errors
        '-1 No File Exists
        '0 The operating system is out of memory or resources.
        'SE_ERR_FNF = 2 The specified file was not found.
        'SE_ERR_PNF = 3 The specified path was not found.
        'SE_ERR_ACCESSDENIED = 5 The operating system denied access to the specified file.
        'SE_ERR_OOM = 8 There was not enough memory to complete the operation.
        'ERROR_BAD_FORMAT = 11 The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).
        'SE_ERR_SHARE = 26 A sharing violation occurred.
        'SE_ERR_ASSOCINCOMPLETE = 27 The filename association is incomplete or invalid.
        'SE_ERR_DDETIMEOUT = 28 The DDE transaction could not be completed because the request timed out.
        'SE_ERR_DDEFAIL = 29 The DDE transaction failed.
        'SE_ERR_DDEBUSY = 30 The DDE transaction could not be completed because other DDE transactions were being processed.
        'SE_ERR_NOASSOC = 31 There is no application associated with the given filename extension.
        'SE_ERR_DLLNOTFOUND = 32 The specified dynamic-link library was not found.
    End Sub



    Thanks
    Last edited by Aussiebear; 12-20-2024 at 02:52 AM.

Posting Permissions

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