Ouverture d'un fichier quelque soit l'emplacement

Azedes -  
gbinforme Messages postés 14930 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonsoir tout le monde,
je suis un débutant en vba et demande votre précieuse aide :idea: . En effet, je cherche un code vba permettant de chercher dans l'ordinateur et d'ouvrir un ficher renseigné dans un input box ou dans un code. Je souhaite que la recherche et l'ouverture se fasse sur n'importe quel poste sera installée l'appli sans que je ne sois contraint de modifier le code lorsque je change d'utilisateur ou de poste.

J'ai trouvé un code sur un autre forum que j'ai du mal à adapter

voici le code
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

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

Sub Macro1()
Dim NomFic As String, sPath As String
Dim x As Long
x = FindWindow("XLMAIN", Application.Caption)
sPath = Environ("USERPROFILE")
sPath = sPath & "\mes documents\SGIIOC\"
NomFic = "conditions générales.pdf"
ShellExecute x, "print", sPath & NomFic, "", "", 1

Mon adaptation

Dim NomFic As String, sPath As String
Dim x As Long
x = FindWindow("XLMAIN", Application.Caption)
sPath = Environ("USERPROFILE")
sPath = sPath & "\mes documents\Fiches 2017\VERIFICATION 2017"
NomFic = "AVRIL 2017"
ShellExecute x,"Open", sPath & NomFic

End Sub

Je tiens à vous dire que je suis un débutant nul en vba

D'avance merci pour votre aide

1 réponse

  1. gbinforme Messages postés 14930 Date d'inscription   Statut Contributeur Dernière intervention   4 744
     
    Bonjour,

    Je te propose d'adapter ainsi ton code :
    Option Explicit
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    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 
    
    Public Sub ouvrir_doc()
    Const Fic As String = "AVRIL 2017.pdf" ' fichier
    Const Rep As String = "\Fiches 2017\VERIFICATION 2017\" ' sous répertoire
    Dim Dcs As String ' mes documents
    Dim Nda As Long ' N° de l'application
    Dim Wsh As Object ' app. lecture registre
    Set Wsh = CreateObject("WScript.Shell")
    Nda = FindWindow("XLMAIN", Application.Caption)
    Dcs = Wsh.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal")
    ShellExecute Nda, "Open", Dcs & Rep & Fic, "", "", 1
    Set Wsh = Nothing
    End Sub
    
    --
     Toujours zen
    La perfection est atteinte, non pas lorsqu'il n'y a plus rien à ajouter, mais lorsqu'il n'y a plus rien à retirer.  Antoine de Saint-Exupéry
    0