Die elegante Methode ...
22.05.2002 23:15:15
Thomas Risi
Hallo PeterHier bietet sich eine Klasse zur Verwendung an ...
Füge in das Excel-Projekt eine Klasse ein, und benenne sie mit 'clsIni'. Dann fügst Du folgenden Code ein.
Option ExplicitPrivate 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
Private Declare Function WritePrivateProfileString_ Lib "kernel32" _
Alias "WritePrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpFileName As String) As Long
' Datei = Pfad zur ini-Datei
' Sektion = Zu ändernde Section
' Schlüssel = Zu ändernder Key
Public Function GetPrivateProfileString(Datei$, Sektion$, Key$) As String
Dim A As Long, Wert As String
Wert = Space$(255)
A = GetPrivateProfileString_(Sektion$, Key$, "", Wert$, Len(Wert$), Datei$)
GetPrivateProfileString = Left$(Wert$, A&)
End Function
Public Function WritePrivateProfileString(Datei$, ByVal Sektion$, ByVal Key$, ByVal Wert$)
Dim A As Long
A = WritePrivateProfileString_(Sektion$, Key$, Wert$, Datei$)
End Function
In einem normalen Modul erstellst Du dann nur noch eine Instanz der Klasse und kannst auf deren Methoden zugreifen.
Option ExplicitDim iniClass As clsIni ' Klasse einbinden
Sub Test_Schreiben()
Dim Path$: Path = "C:\Excel\TestXL.ini"
Dim Section$: Section = "TEST"
Dim Key$: Key = "LastUsedRange"
Set iniClass = New clsIni ' Instanz erstellen
iniClass.WritePrivateProfileString Path, Section, Key, "C8"
iniClass.WritePrivateProfileString Path, Section, "HelloMsg", "Hi, wie geht's ?"
Set iniClass = Nothing
End Sub
Sub Test_Lesen()
Dim Path$: Path = "C:\Excel\TestXL.ini"
Dim Section$: Section = "TEST"
Dim Key$: Key = "LastUsedRange"
Set iniClass = New clsIni ' Instanz erstellen
MsgBox iniClass.GetPrivateProfileString(Path, Section, "HelloMsg")
Set iniClass = Nothing
End Sub
Code eingefügt mit Syntaxhighlighter 1.13
Um alle Daten beim Start der excel-Tabelle zur Verfügung zu haben, liest Du diese am Besten im 'WorkbookOpen'-Ereignis aus. Das Interessante an der 'Klasse' ist, daß man sie nur einmal erstellt, und unverändert in andere Projekte (Excel, Word, Access, usw.) übernehmen kann.
GrußThomas
------------------------------
Internet: Homepage
eMail: Thomas.Risi@t-online.de
------------------------------