|
Option Explicit
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
Dim flag As Boolean, dic
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Len(Target) = 0 Then Exit Sub
If Not flag Then Call reset
If dic.Count = 0 Then Exit Sub
Dim mark, i, t
mark = Split("pdf xls xlsx doc docx")
For i = 0 To UBound(mark)
t = Target & "." & mark(i)
If dic.exists(t) Then
Call ShellExecute(0, "Open", dic(t), 0, 0, 0)
Exit For
End If
Next
If i = UBound(mark) + 1 Then
Target.Offset(, 1) = "!"
End If
End Sub
Sub reset()
test
End Sub
Sub test()
Dim filename(), n, fso, t, i
Set dic = CreateObject("scripting.dictionary")
Set fso = CreateObject("scripting.filesystemobject")
Call getfilename(fso, filename, n, ThisWorkbook.Path)
If n = 0 Then Exit Sub
For i = 1 To UBound(filename)
t = Split(filename(i), "\")
dic(LCase(t(UBound(t)))) = filename(i)
Next
flag = True
End Sub
Sub getfilename(fso, filename, n, pth)
Dim spth, t
If Right(pth, 1) <> "\" Then pth = pth & "\"
Set spth = fso.getfolder(pth)
For Each t In spth.Files
n = n + 1: ReDim Preserve filename(1 To n)
filename(n) = spth & "\" & t.Name
Next
For Each t In spth.subfolders
Call getfilename(fso, filename, n, t)
Next
End Sub |
|