Musterlösungen

Aus VBA-wiki
Zur Navigation springen Zur Suche springen
Die druckbare Version wird nicht mehr unterstützt und kann Darstellungsfehler aufweisen. Bitte aktualisiere deine Browser-Lesezeichen und verwende stattdessen die Standard-Druckfunktion des Browsers.

Die folgenden Musterlösungen sollen nicht als 'Standardlösungen' gelten, sondern lediglich die Suche nach passenden Beispielen zu ganz speziellen Aufgaben darstellen.

Bei diesen Musterlösungen wird auf eine ausführliche Erklärung größtenteils verzichtet. Hier geht es nicht wie sonst in diesem Wiki um das Verständnis, sondern um eine funktionierende Lösung, die man gleich verwenden kann ;-)

Diese Lösungsvorschläge sind anwendungsunabhängig, wodurch sie also in Excel, PowerPoint und Word einsetzbar sind.

Datei in seinem Programm öffnen

Öffnet die angegebene Datei in dem Programm, welches dafür vorgesehen ist. Eine Textdatei wird zum Beispiel standardmäßig im Notepad geöffnet.

Im Kopf des (Klassen-)Moduls (unter 'Option Explicit'):

#If VBA7 Then
    Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal strOperation As String, ByVal strFile As String, _
        ByVal strParameters As String, ByVal strDirectory As String, ByVal lngShowCmd As Long) As Long
#Else
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal strOperation As String, ByVal strFile As String, _
        ByVal strParameters As String, ByVal strDirectory As String, ByVal lngShowCmd As Long) As Long
#End If

Dazu passend die Prozedur:

Public Sub OpenFile(ByVal strFullName As String)
    
    ' Prüfen, ob diese Datei existiert
    If Dir(strFullName) = "" Then Exit Sub
    
    ShellExecute 0, "Open", strFullName, "", "", 1
End Sub

Hyperlink öffnen

Öffnet den Hyperlink im Standard-Browser. Wenn dieser schon läuft, wird ein neuer Reiter geöffnet.

Im Kopf des (Klassen-)Moduls (unter 'Option Explicit'):

#If VBA7 Then
    Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal strOperation As String, ByVal strFile As String, _
        ByVal strParameters As String, ByVal strDirectory As String, ByVal lngShowCmd As Long) As Long
#Else
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal strOperation As String, ByVal strFile As String, _
        ByVal strParameters As String, ByVal strDirectory As String, ByVal lngShowCmd As Long) As Long
#End If

Dazu passend die Prozedur:

Public Sub FollowHyperlink(ByVal strHyperlink As String)
    
    ' WICHTIG: Der Hyperlink muss mit 'http://' bzw. 'https://' beginnen!
     If InStr(1, strHyperlink, "http", vbTextCompare) = 0 Then
        strHyperlink = "http://" & strHyperlink
    End If
    
    ShellExecute 0, "Open", "Explorer.exe", "/e," & strHyperlink, "", 1
End Sub