Hi Guys I would like to open any file through its path

August 21, 2013 at 10:51:23
Specs: Windows 7
its been a headache so far pls help, here is my code

Option Explicit



Private Declare Function GetDesktopWindow _
Lib "user32" () _
As Long

Private 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

Private Const SW_HIDE As Long = 0
Private Const SW_NORMAL As Long = 1
Private Const SW_MAXIMIZE As Long = 3
Private Const SW_MINIMIZE As Long = 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 Long

Dim hWndDesk As Long

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)
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 OpenFileGo()


Dim currentcell, requesterCell As Range
Dim FilePath, FileName, PathName As String
'Dim shtOrigin As Worksheet
Dim UserRow, UserColumn As Long

Set currentcell = ActiveCell
Set requesterCell = ActiveCell
Set currentcell = ActiveCell
UserRow = ActiveCell.Row
UserColumn = ActiveCell.Column
PathName = currentcell.Value

Cells.Select
Selection.Hyperlinks.Delete
Cells(UserRow, UserColumn).Select

Dim Ret
'// Substitute here your Doc full path
'Ret = ShellOper(PathName, "Open", SW_MAXIMIZE)
Ret = ShellOper(PathName, "Open", SW_HIDE)

'// 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
Sub OpenDirGo()




Dim currentcell, requesterCell As Range
Dim FilePath, FileName, PathName As String
'Dim shtOrigin As Worksheet
Dim UserRow, UserColumn As Long

Set currentcell = ActiveCell
Set requesterCell = ActiveCell
Set currentcell = ActiveCell
UserRow = ActiveCell.Row
UserColumn = ActiveCell.Column
PathName = currentcell.Value

Cells.Select
Selection.Hyperlinks.Delete
Cells(UserRow, UserColumn).Select


Dim Ret
'// Substitute here your Doc full path
Ret = ShellOper(PathName & "\", "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


Sub Type_Group()

ActiveSheet.Range("$A$3:$CV$1000000").AutoFilter Field:=9, Criteria1:= _
Array("AVI", "BMP", "CSV", "DB", "DGN", "DOC", "DOCM", "DOCX", "DOT", "DWG", "DXF", "HTM", "HTML", "JPG", "MOV", "MP3", "MPP", "MSG", _
"PDF", "PDX", "PID", "PNG", "PPS", "PPT", "RAR", "RTF", "SAT", "TIF", "STP", "TXT", "WMF", "XLS", "XLSM", "XLSX", "XLW", "XML", "ZIP", "ZIPX"), Operator:=xlFilterValues
SendKeys ("^{HOME}") '//Ctrl-Home


End Sub


See More: Hi Guys I would like to open any file through its path

Report •


#1
August 21, 2013 at 11:08:50
You put this under VB.NET/C#. It's obviously not C# because it looks nothing like C#. It's obviously not VB.NET, because otherwise you'd be using System.Diagnostics.Process.Start().

So what are you using, what do you want to happen, what is happening, and what have you tried?

How To Ask Questions The Smart Way


Report •

#2
August 21, 2013 at 11:15:37
I would like it it to access files through its directory, eg if I type a directory path in a cell and run the macro it should look in the cell and open the file. ive tried with hyperlinks, but some files have been named with hashes etc, so its not what i want

Report •

#3
August 21, 2013 at 12:03:07
So, you've answered 1 out of the 4 posted questions.

I'm going to assume you're using Excel and VBA, because you're talking about macros and cells.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
August 21, 2013 at 13:09:02
correct im coding in visual basic app from excel

Report •

#5
August 21, 2013 at 13:11:36
So far Im able to open a folder, pdf, doc, jpeg, avi, but not excel

Report •

#6
August 21, 2013 at 13:40:00
My VBA skill is just above nill, but if you post some examples of the file paths
that are giving you trouble, I might be able to offer some HYPERLINK() solutions.

MIKE

http://www.skeptic.com/


Report •

#7
August 21, 2013 at 13:42:57
thanks , ive tried hyperlinks they work, but not as consistent as i would like

Report •

Ask Question