Em dùng được code này để tìm file nào đó nhưng đối với máy em thì dùng được nhưng qua máy người khác dù là win 7 hay win XP gì thì cũng báo lỗi. Không biết máy đó thiếu file gì không. Nếu vậy thì em phải cài đặt hoặc copy file gì để máy người khác hiểu đoạn code trên.Mong anh chị chỉ dẫn.
Public Function BrowseForFile(pstrPath, pstrFilter) Set objDialog = CreateObject("MSComDlg.CommonDialog") objDialog.Filter = pstrFilter objDialog.InitialDir = pstrPath objDialog.Flags = &H80000 + &H4 + &H8 intResult = objDialog.ShowOpen BrowseForFile = objDialog.FileName End Function
Em dùng được code này để tìm file nào đó nhưng đối với máy em thì dùng được nhưng qua máy người khác dù là win 7 hay win XP gì thì cũng báo lỗi. Không biết máy đó thiếu file gì không. Nếu vậy thì em phải cài đặt hoặc copy file gì để máy người khác hiểu đoạn code trên.Mong anh chị chỉ dẫn.
Public Function BrowseForFile(pstrPath, pstrFilter) Set objDialog = CreateObject("MSComDlg.CommonDialog") objDialog.Filter = pstrFilter objDialog.InitialDir = pstrPath objDialog.Flags = &H80000 + &H4 + &H8 intResult = objDialog.ShowOpen BrowseForFile = objDialog.FileName End Function
Nói chung là thằng CommonDialog rất khó chịu. Dùng trên VB không có vấn đề nhưng trong VBA thì phiền phức lắm
Dùng cái khác đi, khỏi suy nghĩ cho mất công (thiếu chi control chứ)
Trong Excel có thiếu chi control để làm việc này. Ví dụ:
Mã:
Function BrowseForFile(ByVal strPath As String, ByVal strFilter As String) As String
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.Clear
.Filters.Add "", strFilter
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show Then BrowseForFile = .SelectedItems(1)
End With
End Function
Sub Test()
MsgBox BrowseForFile("D:\", "*.xls")
End Sub
thank anh đã hướng dẫn. Nhưng giờ em muốn sử dụng cái trên thì làm sao anh. Qua tìm hiểu trên mạng thì em được biết cần file ComDlg32.ocx,MSVBVM60.DLL trong system32. Nhưng em không biết làm sao để máy người khác hiểu.Nếu zậy mình cài đặt hoặc copy 2 file này là được sao, mong anh giải thích kĩ giùm em với.
Public Function BrowseForFile(pstrPath, pstrFilter) Set objDialog = CreateObject("MSComDlg.CommonDialog") objDialog.Filter = pstrFilter objDialog.InitialDir = pstrPath objDialog.Flags = &H80000 + &H4 + &H8 intResult = objDialog.ShowOpen BrowseForFile = objDialog.FileName End Function
Biểu người ta cài VB6 vô máy là tự nhiên xài được liền
---------------------------------- Mà bạn loay hoay chi với mấy cái ComDlg32.ocx chi cho mệt vậy... Code tôi cho ở trên chạy được trên mọi máy, tác dụng cũng y chang code của bạn. Vậy tại sao không dùng?
Có hàng chục kiểu Dialog, cớ gì cứ phải khư khư ôm lấy cái không xài được?
Nếu vậy bạn dùng API là chắc ăn nhất
Ví dụ code trên Excel:
Mã:
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Mã:
Function BrowseForFile(ByVal strPath As String, ByVal strFilter As String) As String
Dim OpenFile As OPENFILENAME
Dim lReturn As Long, hWnd As Long
strFilter = "(" & strFilter & ")" & Chr(0) & strFilter & Chr(0)
With OpenFile
.lStructSize = Len(OpenFile)
.hwndOwner = hWnd
[COLOR=#ff0000][B].hInstance = Application.hInstance[/B][/COLOR]
.lpstrFilter = strFilter
.nFilterIndex = 1
.lpstrFile = String(257, 0)
.nMaxFile = Len(OpenFile.lpstrFile) - 1
.lpstrFileTitle = OpenFile.lpstrFile
.nMaxFileTitle = OpenFile.nMaxFile
.lpstrInitialDir = strPath
.lpstrTitle = "File Dialog using Windows API Function"
.flags = 0
End With
lReturn = GetOpenFileName(OpenFile)
If lReturn Then BrowseForFile = OpenFile.lpstrFile
End Function
Chú ý dòng màu đỏ là ứng dụng trên Excel. Nếu là ACAD thì nó phải khác.
Có thể là .hInstance = ThisDrawing.Application.LocaleId (hoặc gì đó tôi không biết)
Bạn thử xem
Nếu vậy bạn dùng API là chắc ăn nhất Ví dụ code trên Excel:
Mã:
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long Private Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String lpstrCustomFilter As String nMaxCustFilter As Long nFilterIndex As Long lpstrFile As String nMaxFile As Long lpstrFileTitle As String nMaxFileTitle As Long lpstrInitialDir As String lpstrTitle As String flags As Long nFileOffset As Integer nFileExtension As Integer lpstrDefExt As String lCustData As Long lpfnHook As Long lpTemplateName As String End Type
Mã:
Function BrowseForFile(ByVal strPath As String, ByVal strFilter As String) As String Dim OpenFile As OPENFILENAME Dim lReturn As Long, hWnd As Long strFilter = "(" & strFilter & ")" & Chr(0) & strFilter & Chr(0) With OpenFile .lStructSize = Len(OpenFile) .hwndOwner = hWnd [COLOR=#ff0000][B].hInstance = Application.hInstance[/B][/COLOR] .lpstrFilter = strFilter .nFilterIndex = 1 .lpstrFile = String(257, 0) .nMaxFile = Len(OpenFile.lpstrFile) - 1 .lpstrFileTitle = OpenFile.lpstrFile .nMaxFileTitle = OpenFile.nMaxFile .lpstrInitialDir = strPath .lpstrTitle = "File Dialog using Windows API Function" .flags = 0 End With lReturn = GetOpenFileName(OpenFile) If lReturn Then BrowseForFile = OpenFile.lpstrFile End Function
Chú ý dòng màu đỏ là ứng dụng trên Excel. Nếu là ACAD thì nó phải khác. Có thể là .hInstance = ThisDrawing.Application.LocaleId (hoặc gì đó tôi không biết) Bạn thử xem