Tipp 7.2 - INI Wert ermitteln
Wie kann ich einen Wert aus einer INI-Datei ermitteln?
Mit dieser Funktion können Sie einen Wert aus einer INI-Datei ermitteln. 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.
Verwendete Win32-Api-Aufrufe und Typen: GetPrivateProfileString
Beispiel:
Option Explicit Public Declare Function GetPrivateProfileString _ Lib "kernel32" Alias _ "GetPrivateProfileStringA" _ (ByVal lpApplicationName As String, _ ByVal lpKeyName As Any, _ ByVal lpDefault As String, _ ByVal lpReturnedString As String, _ ByVal nSize As Long, _ ByVal lpFileName As String) As Long Public Function ReadMyIniSetting(sIniFilePath As String, _ sSection As String, _ sKey As String) As String '// ----------------------------------------------------- '// Methode: | Gibt e. Wert aus einer INI-Datei zurück '// ----------------------------------------------------- '// Parameter: | sIniFilePath = gültiger Pfad zu einer '// | INI-Datei '// | sSection = Sektionsname [...] '// | sKey = Schlüsselname '// ----------------------------------------------------- '// Rückgabe: | Wert, sofern vorhanden '// ----------------------------------------------------- '// Autor: | Stefan Kulpa '// | EDV Innovation & Consulting - Dormagen '// ----------------------------------------------------- Const MAX_LEN As Long = 1024 Dim sBuffer As String sBuffer = VBA.String(MAX_LEN, 0) If GetPrivateProfileString(sSection, _ sKey, _ vbNullString, _ sBuffer, _ Len(sBuffer), _ sIniFilePath) Then ReadMyIniSetting = _ VBA.Left(sBuffer, InStr(sBuffer, vbNullChar) - 1) End If End Function