PDA

View Full Version : ShellExecute


CIUFFO
07-10-2003, 09:54
Con la sub sottoriportata (in un modulo di access2000) riesco tranquillamente ad aprire un file do Word contenuto nella cartella che contiene il mio database.
La domanda è: come mai non riesco ad aprire un file .txt anch'esso contenuto nella stessa cartella?!

Sub MyFILE()
Dim path, fs, s, h
Dim hwnd As Long

Set fs = CreateObject("Scripting.FileSystemObject")
h = CurrentDb.Properties("name").Value
s = fs.GetparentfolderName(h)

path = s + "\MioFile.doc"

hwnd = FindWindow(vbNullString, "MioFile.doc - Microsoft Word")
If hwnd = 0 Then ' 0 significa che il file non è in esecuzione.

ShellExecute hwnd, vbNullString, path, vbNullString, vbNullString, 0
Putfocus hwnd

Else
Exit Sub
End If

End Sub

CIUFFO
08-10-2003, 19:12
Praticamente mi servirebbe un metodo per aprire un file .txt mediante VB dal mio database

cionci
08-10-2003, 19:28
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
Const SW_SHOWNORMAL = 1

Private Sub Comando0_Click()
ShellExecute 0, "Open", "C:\boot.ini", vbNullString, vbNullString, SW_SHOWNORMAL
End Sub

Il primo parametro non è l'handle della finestra del programma aperto, ma l'handle della finestra del programma padre...

CIUFFO
09-10-2003, 09:12
Grazie mille,
ora funziona, ma il problema era il parametro 'nShowCmd' infatti io mettevo '0' , mi sono lasciato ingannare dalla frase sottoriportata presa dalla Api guide:
If lpFile specifies a document file, nShowCmd should be zero.
Mettevo zero perchè il mio file è una cartella (od un file) e non un eseguibile.
Riguardo al primo parametro io utilizzo l'handle restituito da FindWindow per controllare se il mio file è già aperto, se non lo è FindWindow restituisce 0 come hai messo tu ed apre il file, se è già aperto passa all' Else e con ShowWindow visualizza il mio file che era ridotto ad icona.
Ora il mio codice è così e funziona:

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 Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Const SW_SHOWNORMAL = 1


Sub MyFILE()
Dim path, fs, s, h
Dim hwnd As Long

Set fs = CreateObject("Scripting.FileSystemObject")
h = CurrentDb.Properties("name").Value
s = fs.GetparentfolderName(h)

path = s + "\MyFile"

hwnd = FindWindow(vbNullString, "C:\Documents and settings\Giovanni\Documenti\MyFolder\MyFile")
If hwnd = 0 Then ' 0 significa che il file non è in esecuzione.

ShellExecute hwnd, vbNullString, path, vbNullString, vbNullString, SW_SHOWNORMAL

Else
ShowWindow hwnd, SW_SHOWNORMAL
Exit Sub
End If

End Sub