Fermer un fichier depuis vb

Résolu
lrinet - 1 mai 2013 à 17:39
 lrinet - 1 mai 2013 à 20:12
Bonjour,
j'espere avoir posté au bon endroit.
voila j'ai placé un bout de code pour faire fonctionner mon usine a gaz
depuis vb je permet d'ouvrir le fichier d'un prog,
je peux le lire .......
cependant je ne parviens pas à le fermer depuis vb,
j'ai testé plusieurs codes c'est l'echec
quelqu'un aurait il une piste??
merci de vos conseils....
------------------------------------------------------------------------
voici le code que j'ai utilisé pour ouvrir
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 Sub UserForm_Initialize()

Fichier = "C:\Documents and Settings\TESTS\PHILIPS fr732_00_dfu_fra.pdf"
'ouvrir le fichier
'Stop
MsgBox "ouverture de " & Fichier
ACTION = "OPEN" ' action might be OPEN, NEW or other, depending on what you need to do
lngErr = ShellExecute(0, ACTION, Fichier, "", "", 1)

'verifier que le fichier est fermé sinon fermer le fichier
'recherche toute info et astuce

End Sub

3 réponses

ucfoutu Messages postés 18038 Date d'inscription lundi 7 décembre 2009 Statut Modérateur Dernière intervention 11 avril 2018 211
1 mai 2013 à 19:32
Bonjour,
Lorsque la fonction ShelleExecute réussit, elle retourne un long, qui est le handle de l'application ainsi lancée
Il va te falloir alors utiliser d'autres fonctions de l'Api de pour, à partir de ce Handle, en déterminer la fenêtre et le processus et détruire tout cela depuis ton appli VB

________________________
Réponse exacte ? => "REPONSE ACCEPTEE" facilitera les recherches.
Pas d'aide en ligne installée ? => ne comptez pas sur moi pour simplement répéter son contenu. Je n'interviendrai que si nécessité de la compléter.
3
ucfoutu Messages postés 18038 Date d'inscription lundi 7 décembre 2009 Statut Modérateur Dernière intervention 11 avril 2018 211
1 mai 2013 à 19:34
complétion :
qui est le handle de l'application ainsi lancée === >> qui est le handle d'instance de l'application ainsi lancée


________________________
Réponse exacte ? => "REPONSE ACCEPTEE" facilitera les recherches.
Pas d'aide en ligne installée ? => ne comptez pas sur moi pour simplement répéter son contenu. Je n'interviendrai que si nécessité de la compléter.
3
Salut ucfoutu,
merci de ta reponse,
pendant ce temps j'ai trouvé une solution qui ferme l'executable et du coups le fichier, ce n'est pas grave pour l'instant
en voici le code:

'Close Application
'CloseApp KillAll=False -Only first occurrence
' KillAll=True -All occurrences
' NeedYesNo=True -Prompt to kill
' NeedYesNo=False -Silent kill
Private Function CloseAPP(AppNameOfExe As String, Optional KillAll As Boolean False, Optional NeedYesNo As Boolean True) As Boolean
Dim oProcList As Object
Dim oWMI As Object
Dim oProc As Object
'Stop
CloseAPP = False
' step 1: create WMI object instance:
Set oWMI = GetObject("winmgmts:")
If IsNull(oWMI) = False Then
' step 2: create object collection of Win32 processes:
Set oProcList = oWMI.InstancesOf("win32_process")
' step 3: iterate through the enumerated collection:
For Each oProc In oProcList
'MsgBox oProc.Name
' option to close a process:
If UCase(oProc.Name) = UCase(AppNameOfExe) Then
If NeedYesNo Then
If MsgBox("Kill " & oProc.Name & vbNewLine & "Are you sure?", vbYesNo + vbCritical) = vbYes Then
oProc.Terminate (0)
'no test to see if this is really true
CloseAPP = True
'Stop
End If 'MsgBox("Kill "
Else 'NeedYesNo
oProc.Terminate (0)
'no test to see if this is really true
CloseAPP = True
'Stop
End If 'NeedYesNo
'continue search for more???
If Not KillAll And CloseAPP Then
Exit For 'oProc In oProcList
'Stop
End If 'Not KillAll And CloseAPP

'Stop
End If 'IsNull(oWMI) = False
Next 'oProc In oProcList
Else 'IsNull(oWMI) = False
'report error
End If 'IsNull(oWMI) = False
' step 4: close log file; clear out the objects:
Set oProcList = Nothing
Set oWMI = Nothing
End Function
'**************************************

'No frills killer
Private Function CloseAPP_B(AppNameOfExe As String)
Dim oProcList As Object
Dim oWMI As Object
Dim oProc As Object
Stop
' step 1: create WMI object instance:
Set oWMI = GetObject("winmgmts:")
If IsNull(oWMI) = False Then
' step 2: create object collection of Win32 processes:
Set oProcList = oWMI.InstancesOf("win32_process")
' step 3: iterate through the enumerated collection:
For Each oProc In oProcList
' option to close a process:
If UCase(oProc.Name) = UCase(AppNameOfExe) Then
oProc.Terminate (0)
Stop
End If 'IsNull(oWMI) = False
Next 'oProc In oProcList
Else 'IsNull(oWMI) = False
'report error
Stop
End If 'IsNull(oWMI) = False
' step 4: close log file; clear out the objects:
Set oProcList = Nothing
Set oWMI = Nothing
End Function
'**************************************

Maintenant je n'ai plus qu'a rajouter dans mon sub la fermeture avec le code suivant:
Executable = "AcroRd32.exe"
'Stop
MsgBox IIf(CloseAPP(Executable, True, False), Executable & " est fermé", Executable & " est déja fermé")


en attendant mieux
0
Rejoignez-nous