Tipp 4.8 - Verschiedene Shell-Funktionen

Wie kann ich verschiedene Shell-Funktionen ausführen?

Mit dieser Funktion können Sie verschiedene Shell-Funktionen ausführen. Kopieren Sie einfach nachfolgenden Quellcode in die Zwischenablage und fügen Sie anschließend den Inhalt der Zwischenablage in ein neues Modul ein. Die Aufrufparameter finden Sie im Quellcode beschrieben.

Api-AufrufeVerwendete Win32-Api-Aufrufe und Typen: GetDesktopWindow, ShellExecute

Beispiel:

Option Explicit
 
Public Declare Function GetDesktopWindow _
    Lib "user32" () As Long
Public 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 Function RunShellExecute( _
                sTopic As String, _
                vFile As Variant, _
                vParams As Variant, _
                vDirectory As Variant, _
                lShowCmd As Long, _
                Optional bOpenAsIfFailed _
                As Boolean = False) As Long
  '// -----------------------------------------------------
  '// Methode:   | Führt verschiedene Shell-Funktionen aus;
  '//            | siehe Beispiele unten
  '// -----------------------------------------------------
  '// Parameter: | sTopic = Befehlstyp (open, play etc.)
  '//            | vFile = Datei oder Programmpfad
  '//            | vParams = Parameter für Programme
  '//            | vDirectory = Ordner für die "Ausführung"
  '//            | lShowCmd = Fenstermodus der Ausführung
  '//            | bOpenAsIfFailed = ruft im Fehlerfall den
  '//            | "Öffnen mit"-Dialog auf
  '// -----------------------------------------------------
  '// Rückgabe:  | True = Ausführung erfolgreich
  '// -----------------------------------------------------
  '// Autor:     | Stefan Kulpa
  '//            | EDV Innovation & Consulting - Dormagen
  '// -----------------------------------------------------
  Const SE_ERR_NOASSOC    As Long = 31
  Dim lResult             As Long
  Dim sCommand            As String
  lResult = ShellExecute(GetDesktopWindow(), _
      sTopic, _
      vFile, _
      vParams, _
      vDirectory, _
      lShowCmd)
  RunShellExecute = (lResult > 32)
  If lResult = SE_ERR_NOASSOC Then
    If bOpenAsIfFailed Then
      sCommand = _
      "rundll32.exe shell32.dll,OpenAs_RunDLL " & vFile
      Call Shell(sCommand, vbNormalFocus)
    End If
  End If
  '// -----------------------------------------------------
  '// Beispiele:
  '// -----------------------------------------------------
  '// sTopic = "Open"
  '// sFile = "c:\Eigene Dateien\Resources.doc"
  '// sParams = 0&
  '// sDirectory = 0&
  '// -----------------------------------------------------
  '// sTopic = "Play"
  '// sFile = "C:\PROGRA~1\MICROS~2\Videos\SEARCH.AVI"
  '// sParams = 0&
  '// sDirectory = 0&
  '// -----------------------------------------------------
  '// sTopic = "Open"
  '// sFile = "mailto:Stefan@Kulpa-Online.de"
  '// sParams = 0&
  '// sDirectory = 0&
  '// -----------------------------------------------------
  '// sTopic = "Open"
  '// sFile = "http://www.Kulpa-Online.de"
  '// sParams = 0&
  '// sDirectory = 0&
  '// -----------------------------------------------------
  '// sTopic = "Open"
  '// sFile = "C:\Programme\Internet Explorer\IEXPLORE.EXE"
  '// sParams = "http://www.Kulpa-Online.de"
  '// sDirectory = 0&
  '// -----------------------------------------------------
  '// sTopic = "Open"
  '// sFile = "explorer.exe"
  '// sParams = "/root,C:\Programme"
  '// sDirectory = 0&
  '// -----------------------------------------------------
  '// sTopic = "Open"
  '// sFile = "explorer.exe"
  '// sParams = "/e,/root,C:\Programme"
  '// sDirectory = 0&
  '// -----------------------------------------------------
  '// sTopic = "Open"
  '// sFile = "explorer.exe"
  '// sParams = "/e,C:\Programme"
  '// sDirectory = 0&
  '// -----------------------------------------------------
  '// sTopic = "Open"
  '// sFile = "explorer.exe"
  '// sParams = "/e,c:\"
  '// sDirectory = 0&
  '// -----------------------------------------------------
  '// sTopic = "Open"
  '// sFile = "explorer.exe"
  '// sParams = "/e," 'CurDir()
  '// sDirectory = 0&
  '// -----------------------------------------------------
  '// sTopic = "Open"
  '// sFile = "explorer.exe"
  '// 'Papierkorb
  '// sParams = _
  '// "/root,::{645FF040-5081-101B-9F08-00AA002F954E}"
  '// sDirectory = 0&
  '// -----------------------------------------------------
  '// Call RunShellExecute( _
  '//      sTopic, sFile, sParams, sDirectory, 1)
  '// -----------------------------------------------------
End Function