PDA

View Full Version : nho chi day 2



lovely
16-09-2002, 12:59
Toi lam mot chuong trinh xu ly ten co so du lieu. Toi tao mot truong la duong dan den 1 file ma toi can mo(*.txt).Phai lam the nao de mo no day.Toi biet co the dung ham shell nhung khong biet cau lenh nhu the nao .chi cho toi nhe !

vuminhphan
16-09-2002, 14:07
bạn muốn mở file đó trong chương trình của bạn hay là ở đâu?

có thể sử dụng function ShellExecute(), sau đây là ví dụ trong msnd:

Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As _
String, ByVal lpszFile As String, ByVal lpszParams As String, _
ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long

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

Const SW_SHOWNORMAL = 1

Const SE_ERR_FNF = 2&
Const SE_ERR_PNF = 3&
Const SE_ERR_ACCESSDENIED = 5&
Const SE_ERR_OOM = 8&
Const SE_ERR_DLLNOTFOUND = 32&
Const SE_ERR_SHARE = 26&
Const SE_ERR_ASSOCINCOMPLETE = 27&
Const SE_ERR_DDETIMEOUT = 28&
Const SE_ERR_DDEFAIL = 29&
Const SE_ERR_DDEBUSY = 30&
Const SE_ERR_NOASSOC = 31&
Const ERROR_BAD_FORMAT = 11&

Function StartDoc(DocName As String) As Long
Dim Scr_hDC As Long
Scr_hDC = GetDesktopWindow()
StartDoc = ShellExecute(Scr_hDC, "Open", DocName, _
"", "C:\", SW_SHOWNORMAL)
End Function

Private Sub Form_Click()
Dim r As Long, msg As String
r = StartDoc("C:\WINDOWS\ARCADE.BMP")
If r <= 32 Then
'There was an error
Select Case r
Case SE_ERR_FNF
msg = "File not found"
Case SE_ERR_PNF
msg = "Path not found"
Case SE_ERR_ACCESSDENIED
msg = "Access denied"
Case SE_ERR_OOM
msg = "Out of memory"
Case SE_ERR_DLLNOTFOUND
msg = "DLL not found"
Case SE_ERR_SHARE
msg = "A sharing violation occurred"
Case SE_ERR_ASSOCINCOMPLETE
msg = "Incomplete or invalid file association"
Case SE_ERR_DDETIMEOUT
msg = "DDE Time out"
Case SE_ERR_DDEFAIL
msg = "DDE transaction failed"
Case SE_ERR_DDEBUSY
msg = "DDE busy"
Case SE_ERR_NOASSOC
msg = "No association for file extension"
Case ERROR_BAD_FORMAT
msg = "Invalid EXE file or error in EXE image"
Case Else
msg = "Unknown error"
End Select
MsgBox msg
End If
End Sub

vuminhphan
16-09-2002, 19:23
đơn giản hơn, bạn có thể làm như sau:

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

Public Const conSwNormal = 1
ShellExecute hwnd, "open", "c:\vmp.txt", vbNullString, vbNullString, conSwNormal

lovely
17-09-2002, 11:39
Kho hieu qua
Y toi tao 1 from
Gom 1 command
va 1 textbox
Khi command_click
Thi se mo file(*txt) ma co duong dan da nhap trong textbox(Mo ngoai chuong trinh cua toi).Neu duong dan trong textbox khong ton tai thi msgbox"khong tim thay file tren"

vuminhphan
17-09-2002, 13:29
vậy thì dùng hàm Shell hoặc ShellExecute cũng được :)

bị admin nhắc nhở cái dzụ tiếng Việt rùi mà còn tái phạm huh :) hihihi