anhtuan1066
Thành viên gạo cội




- Tham gia
- 10/3/07
- Bài viết
- 5,802
- Được thích
- 6,912
Tôi có 1 đoạn code dùng để lấy toàn bộ các đuôi file trên máy tính (file extension) và kiểm tra xem chương trình nào chạy mỗi 1 đuôi file ấy
Máy tôi cài Win7 + Office 2010 chạy tốt nhưng không biết trên Windows XP thì thế nào
Code như sau:
Nhờ các bạn (nhất là những ai đang xài Windows XP) test giúp đoạn code trên bằng 2 cách:
- Mở file Excel mới, code toàn bộ code cho vào module và chạy Sub Main
- Chạy code trong file đính kèm dưới đây (bấm nút Run code)
Rất mong hồi âm từ các bạn! Các ơn trước
ANH TUẤN
Máy tôi cài Win7 + Office 2010 chạy tốt nhưng không biết trên Windows XP thì thế nào
Code như sau:
PHP:
Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
(ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
PHP:
Function GetTempFile(ByVal Extension As String) As String
Dim tmpFolder As String, tmpFile As String, tmpExt As String
Extension = Replace(Extension, ".", "")
With CreateObject("Scripting.FileSystemObject")
tmpFolder = .GetSpecialFolder(2).Path
tmpFile = .BuildPath(tmpFolder, .GetTempName)
tmpExt = .GetExtensionName(tmpFile)
tmpFile = Left(tmpFile, Len(tmpFile) - Len(tmpExt)) & Extension
.CreateTextFile tmpFile, True
GetTempFile = tmpFile
End With
End Function
PHP:
Function GetAssociatedProgram(ByVal Extension As String) As String
Dim assProg As String, tmpFile As String, p1 As String, p2 As String
On Error Resume Next
Extension = Replace(Extension, ".", "")
tmpFile = GetTempFile(Extension)
assProg = Space$(1024)
With CreateObject("Scripting.FileSystemObject")
FindExecutable tmpFile, "", assProg
p1 = UCase(.GetFile(tmpFile).ParentFolder.Path)
p2 = UCase(.GetFile(assProg).ParentFolder.Path)
If p1 <> p2 Then GetAssociatedProgram = Left(assProg, InStr(assProg, Chr(0)) - 1)
.DeleteFile tmpFile, True
End With
End Function
PHP:
Function GetFileExtensions()
Dim sComm As String, txtStream As Object, tmpFile, tmpArr, Arr(), n As Long
On Error Resume Next
With CreateObject("Scripting.FileSystemObject")
tmpFile = .GetTempName
sComm = "assoc" & " >" & tmpFile
CreateObject("Wscript.Shell").Run "cmd /c " & sComm, 0, True
Set txtStream = .OpenTextFile(tmpFile, 1)
tmpArr = Split(txtStream.ReadAll, vbCrLf)
txtStream.Close
ReDim Arr(LBound(tmpArr) To UBound(tmpArr))
For n = LBound(tmpArr) To UBound(tmpArr)
Arr(n) = Trim(Split(tmpArr(n), "=")(0))
Next
GetFileExtensions = Arr
.DeleteFile tmpFile, True
End With
End Function
PHP:
Sub Main()
Dim ExtArr, Arr(), lR As Long, n As Long, tmp
With Sheet1
ExtArr = GetFileExtensions
ReDim Arr(UBound(ExtArr, 1), 1 To 2)
For lR = LBound(ExtArr, 1) To UBound(ExtArr, 1)
tmp = GetAssociatedProgram(CStr(ExtArr(lR)))
If Trim(tmp) <> "" Then
Arr(n, 1) = CStr(ExtArr(lR))
Arr(n, 2) = tmp
n = n + 1
End If
Next
.Range("A2").Resize(n, 2).Value = Arr
End With
End Sub
- Mở file Excel mới, code toàn bộ code cho vào module và chạy Sub Main
- Chạy code trong file đính kèm dưới đây (bấm nút Run code)
Rất mong hồi âm từ các bạn! Các ơn trước
ANH TUẤN