Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

ouvrir un fichier à partir d'un formulaire (suite)

1 réponse
Avatar
reserve-naturelle
Bonjour,

je cherche à ouvrir un fichier à l’aide d’un bouton dans un formulaire. Gilbert m’a gentillment proposé le code suivant :


Option Explicit

Private Declare Function ShellExec 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 Enum TypeOpen
Hide = 0
Normal = 1
Minimized = 2
Maximized = 3
Restore = 9
End Enum

Public Enum OP
OpExecute = 1
OpPrint = 2
End Enum

Private Const ERROR_SUCCESS = 32&
Private Const ERROR_NO_ASSOC = 31&
Private Const ERROR_OUT_OF_MEM = 0&
Private Const ERROR_FILE_NOT_FOUND = 2&
Private Const ERROR_PATH_NOT_FOUND = 3&
Private Const ERROR_BAD_FORMAT = 11&

Function OpenFileExtend(FileName As String, Optional Window As TypeOpen
=Minimized, Optional Operation As OP = OpExecute) As Variant
Dim lRet As Long
Dim varTaskID As Variant
Dim stRet As String

'Dans Excel (et Word?) remplacer hWndAccessApp par Application.Hwnd
lRet = ShellExec(hWndAccessApp, _
IIf(Operation = OpPrint, "print", "open"), FileName,
vbNullString,
vbNullString, Window)
If lRet > ERROR_SUCCESS Then
stRet = vbNullString
lRet = -1
Else
Select Case lRet
Case ERROR_NO_ASSOC:
varTaskID = Shell("rundll32.exe shell32.dll,OpenAs_RunDLL " &
FileName, 1)
lRet = (varTaskID <> 0)
Case ERROR_OUT_OF_MEM:
stRet = "Erreur: Pas assez de mémoire pour exécuter"
Case ERROR_FILE_NOT_FOUND:
stRet = "Erreur : Fichier non trouvé"
Case ERROR_PATH_NOT_FOUND:
stRet = "Erreur : Chemin non trouvé"
Case ERROR_BAD_FORMAT:
stRet = "Erreur : Type de fichier inconnu"
Case Else:
End Select
End If
OpenFileExtend = lRet & IIf(stRet = "", vbNullString, ",
" & stRet)
End Function

mais étant néophyte, je ne comprend pas trop où l'on indique le chemin d'accès au fichier.

Concrètement, je precise ma question, je dois ouvrir un fichier qui se trouve dans c:\serena\xxx.r avec le logiciel tinn-R qui se trouve dans c:\program file\tinn-r\bin\tinn.exe

merci

1 réponse

Avatar
Fabien
reserve-naturelle a écrit :
Bonjour,

je cherche à ouvrir un fichier à l’aide d’un bouton dans un formulaire. Gilbert
m’a gentillment proposé le code suivant :


Option Explicit

Private Declare Function ShellExec 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 Enum TypeOpen
Hide = 0
Normal = 1
Minimized = 2
Maximized = 3
Restore = 9
End Enum

Public Enum OP
OpExecute = 1
OpPrint = 2
End Enum

Private Const ERROR_SUCCESS = 32&
Private Const ERROR_NO_ASSOC = 31&
Private Const ERROR_OUT_OF_MEM = 0&
Private Const ERROR_FILE_NOT_FOUND = 2&
Private Const ERROR_PATH_NOT_FOUND = 3&
Private Const ERROR_BAD_FORMAT = 11&

Function OpenFileExtend(FileName As String, Optional Window As TypeOpen
=Minimized, Optional Operation As OP = OpExecute) As Variant
Dim lRet As Long
Dim varTaskID As Variant
Dim stRet As String

'Dans Excel (et Word?) remplacer hWndAccessApp par Application.Hwnd
lRet = ShellExec(hWndAccessApp, _
IIf(Operation = OpPrint, "print", "open"), FileName,
vbNullString,
vbNullString, Window)
If lRet > ERROR_SUCCESS Then
stRet = vbNullString
lRet = -1
Else
Select Case lRet
Case ERROR_NO_ASSOC:
varTaskID = Shell("rundll32.exe shell32.dll,OpenAs_RunDLL " &
FileName, 1)
lRet = (varTaskID <> 0)
Case ERROR_OUT_OF_MEM:
stRet = "Erreur: Pas assez de mémoire pour exécuter"
Case ERROR_FILE_NOT_FOUND:
stRet = "Erreur : Fichier non trouvé"
Case ERROR_PATH_NOT_FOUND:
stRet = "Erreur : Chemin non trouvé"
Case ERROR_BAD_FORMAT:
stRet = "Erreur : Type de fichier inconnu"
Case Else:
End Select
End If
OpenFileExtend = lRet & IIf(stRet = "", vbNullString, ",
" & stRet)
End Function

mais étant néophyte, je ne comprend pas trop où l'on indique le chemin d'accès
au fichier.

Concrètement, je precise ma question, je dois ouvrir un fichier qui se trouve
dans c:serenaxxx.r avec le logiciel tinn-R qui se trouve dans c:program
filetinn-rbintinn.exe

merci


Salut,
Ben je pense que la lecture de ceci t'eclairera ;-)
http://officesystemaccess.seneque.net/apiopenfileextend.htm
@+