• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

How do I open files without specifying the program

How can I open a file from vb without knowing which program you have to start. I want it automatically to use the registered program. f.e filename.doc => word / filename.pdf => Adobe Acrobat.

You can do this via start -> RUN.
So I Think It must be possible via VB also
0
FrancisIde
Asked:
FrancisIde
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
You can use this code here:

Public Sub OpenWithShellExecute(                                         ByVal pstrOperation As String, _
                                         ByVal pstrFichierAOuvrir As String)

'pstrOperation can be "Print" or "Open"
Dim lngResult As Long
Dim strDir As String

    lngResult = ShellExecute(me.Hwnd, pstrOperation, pstrFichierAOuvrir, vbNullString, "", IIf(pstrOperation = "Print", SW_HIDE, SW_SHOW))

    'If the function fails, the return value is an error value that is less than or equal to 32.
    If lngResult >= 33 Then Exit Sub
   
    Select Case lngResult
        Case ERROR_FILE_NOT_FOUND
            MsgBox "Le document est introuvable (" & pstrFichierAOuvrir & ").", vbExclamation + vbOKOnly
        Case SE_ERR_NOASSOC
            MsgBox "Ce type de document n'est pas ASSOCIÉ à une application Windows." & vbCrLf & vbCrLf & "Appuyer sur le bouton OK et vous pourrez l'associer.", vbExclamation + vbOKOnly
            strDir = Space$(260)
            lngResult = GetSystemDirectory(strDir, Len(strDir))
            strDir = Left$(strDir, lngResult)
            Call ShellExecute(me.Hwnd, vbNullString, "RUNDLL32.EXE", "shell32.dll,OpenAs_RunDLL " & pstrFichierAOuvrir, strDir, vbNormalFocus)
        Case Else
            MsgBox "Un problème empêche d'ouvrir ce document( " & pstrFichierAOuvrir & "). (Code: " & lngResult & ")", vbCritical + vbOKOnly
    End Select
End Sub
0
 
aminerdCommented:
Try this:

FileName = "www.microsoft.com" 'The file to open
Shell "start.exe " + FileName
0
 
mcriderCommented:
If you're doing to use the shell "start" command, do it this way:


FileName = "www.microsoft.com" 'The file to open
shell "start.exe " & FileName, vbHide

That way you don't get that annoying DOS box that pops up to run start.exe.  Also keep in mind that not all systems have start.exe on them.  


Personally I would do it this way:

'ADD THE FOLLOWING TO A MODULE:
Private Const SW_SHOWNORMAL = 1
Private Declare Function GetDesktopWindow Lib "user32" () 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



'THEN YOU CAN CALL IT LIKE THIS:
Private Sub Command1_Click()
   Dim Scr_hDC As Long
   Dim DocName As String
   
   On Error Resume Next
   DocName = "www.experts-exchange.com"
   Scr_hDC = GetDesktopWindow
   LaunchDocument = ShellExecute(Scr_hDC, "Open", DocName, "", "C:\", SW_SHOWNORMAL)
End Sub


If you use this, please reject the current answer and accept this comment as the answer...


Hope this helps!


Cheers!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mcriderCommented:
By the way, in both my examples you can replace the URL references with filenames... for example:

   DocName="My Word Doc.doc"


Cheers!
0
 
Erick37Commented:
"HOWTO: Use ShellExecute to Launch Associated File"

http://support.microsoft.com/support/kb/articles/q170/9/18.asp
0
 
FrancisIdeAuthor Commented:
I didn't use the simple way. Start.exe but used the API function.

Thanks for the help you all
0
 
mcriderCommented:
FrancisIde,

Did you use the API example as defined in my comment?  If so, you awarded the points to the wrong person...


0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now