PDA

View Full Version : [SLEEPER:] Open any file in any application from VBA



hr_700
06-02-2018, 03:36 AM
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

Logit
06-02-2018, 07:40 AM
.
Try this (untested) :



Sub Auto_Open()
Dim x As Variant
Dim Path As String

' Set the Path variable equal to the path of your program's installation
Path = "C:\Program Files\BC-Wedge\BC-Wedge.exe" '<--- edit path as required

x = Shell(Path, vbNormalFocus)
End Sub

hr_700
06-02-2018, 08:02 AM
Thank you for your try .. let me explain more , I will build user form with one text box and command button then there is one main path folder like C:Intel\Logs then this path has sub folders and this is updated every day by adding folders , so what I want is to write the file name in text box then click button to go directly to subfolder as per the text box input and by double click I can open the file either pdf or word or excel or outlook etc....

hr_700
06-03-2018, 01:10 AM
please find my attached .

thanks

hr_700
06-09-2018, 07:07 AM
any luck gents :hi:

SamT
06-09-2018, 03:24 PM
write the file name in text box then click button to go directly to subfolder as per the text box inputNo typos, or misspeilings allowed, Must include path, also with no tpyos, or missppellings allowed. If Path not included, system must search each subfolder for any files with that name. There may be more than one. :devil2:

Why not just use a file finder Dialog, then parse Windows registry for which Application opens that file type?