Tidligere postede jeg noget kode til at læse data fra ini filer.
Efterfølgende har jeg erfaret at den kode er meget langsom, hvis man skal læse mange informationer, da det tager tid at oprette instanser af word.
Derfor har jeg fundet en anden, meget effektiv kode. Det er ikke mig der har skrevet koden, så jeg har indsat kilde henvisning i bunden af dette indlæg.
Håber der er nogen, der kan bruge denne information til noget :)
Hilsen Flash
Kode start:
----------------
Option Explicit
Private 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 Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long
Private Const c_ini_file_name = "Test.Ini"
Private c_WinDir As String
Private c_profile_string As String
Private c_ini_string As String
Private c_ini_no_of_chars As Integer
Sub aIniWriteTest()
WriteIniFileString "Test", "SubTest", 123
End Sub
Sub aIniReadTest()
MsgBox ReadIniFileString("Test", "SubTest")
End Sub
'' ***************************************************************************
'' Purpose : Read from an INI file
''
Public Function ReadIniFileString( _
ByVal Sect As String, _
ByVal Keyname As String) As String
Dim worked As Long
Dim RetStr As String * 128
Dim StrSize As Long
c_ini_no_of_chars = 0
c_ini_string = ""
If Sect = "" Or Keyname = "" Then
MsgBox "Section Or Key To Read Not Specified !!!", vbExclamation, "INI"
Else
c_profile_string = ""
RetStr = Space(128)
StrSize = Len(RetStr)
worked = GetPrivateProfileString(Sect, Keyname, "", RetStr, StrSize, _
ThisWorkbook.Path & "\" & c_ini_file_name)
If worked Then
c_ini_no_of_chars = worked
c_ini_string = Left$(RetStr, worked)
End If
End If
ReadIniFileString = c_ini_string
End Function
'' ***************************************************************************
'' Purpose : Write to an INI file
''
Public Function WriteIniFileString( _
ByVal Sect As String, _
ByVal Keyname As String, _
ByVal Wstr As String) As String
Dim worked As Long
c_ini_no_of_chars = 0
c_ini_string = ""
If Sect = "" Or Keyname = "" Then
MsgBox "Section Or Key To Write Not Specified !!!", vbExclamation, "INI"
Else
worked = WritePrivateProfileString(Sect, Keyname, Wstr, _
ThisWorkbook.Path & "\" & c_ini_file_name)
If worked Then
c_ini_no_of_chars = worked
c_ini_string = Wstr
End If
WriteIniFileString = c_ini_string
End If
End Function
----------------
Kode slut
Kilde:
http://www.bygsoftware.com/Excel/VBA/ini_files.htm