Exporting to seperate .ini files

G

Gunti

Hi,

I've got a problem.

I've got an excel sheet with contact information. I'm trying to spool it
into a tool a co-worker created. Thing is, he created it in a way where i
have to export each row (Adress, Location, Name) into a .ini file.

Following formats:

Each row a new .ini file which is called <fill in name from column 1).ini
In this .ini, i need the following to happen. I have about 2300 rows so
doing it manually won't happen haha.

First row: AdressKL=<fill in AdressData from excel>
Second row: LocationKL=<fill in LocationData from excel>

Is this possible with a tiny bit of vB knowledge??

Greets,
Gunti
 
J

Jim Thomlinson

There are a couple of API's specificaly designed for reading and writing ini
files. I wrapped them up in some code to make them a bit easier to use. Here
is a description of how to impliment that code.

Open a standard code module. Name the module modIniSettings. Paste the
following...
'***************
'API Function Declarations
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

Declare Function WritePrivateProfileString Lib "kernel32.dll" Alias
"WritePrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long

'Global Variables
Public INI_SETTINGS As clsINISettings
Public Sub Auto_Close()
Set INI_SETTINGS = Nothing
End Sub
'***********************************

Now add a Class Module to your project. Name the module clsINISettings.
Paste the following...

'********************************
Private c_strSection As String
Private c_strKey As String
Private c_strININame As String

Public Property Let Section(strSection As String)
c_strSection = strSection
End Property

Public Property Let Key(strKey As String)
c_strKey = strKey
End Property

Public Property Let ININame(strININame As String)
c_strININame = strININame
End Property

Public Function ReadINISettings() As Variant
Dim sDestination As String * 255
Dim lReturnVal As Long

On Error GoTo ErrorHandler

lReturnVal = GetPrivateProfileString(c_strSection, c_strKey, "", _
sDestination, Len(sDestination), c_strININame)

'lReturnVal will always equal the length of the returned string not
including vbNullChar 's at the end!!!
If lReturnVal <> 0 Then
sDestination = Left(sDestination, InStr(sDestination, Chr(0)) - 1)
'chr(0)=vbNullChar
ReadINISettings = Trim(sDestination)
Else
Err.Raise vbObjectError + 513 + 1001,
"clsINISettings.ReadINISettings", _
"Initialization File Error!"
End If

Exit Function
ErrorHandler:
MsgBox "Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description _
, vbCritical, "Initialization File Error"
End Function

Public Sub WriteINISettings(ByRef strWriteValue As String)
Dim lReturnVal As Long

On Error GoTo ErrorHandler

lReturnVal = WritePrivateProfileString(c_strSection, c_strKey,
strWriteValue, _
c_strININame)
If lReturnVal = 0 Then Err.Raise vbObjectError + 513 + 1001, _
"clsINISettings.WriteINISettings", "Initialization File Error!"
Exit Sub

ErrorHandler:
MsgBox "Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description _
, vbCritical, "Initialization File Error"
End Sub
'****************************************

Now to write an INI file is as easy as (paste this code in any standard
module or sheet)...

Sub test()
Set INI_SETTINGS = New clsINISettings
With INI_SETTINGS
.ININame = ThisWorkbook.Path & "\MyIni.ini"
.Section = "StartUp"
.Key = "UID"
.WriteINISettings ("Me")
.Key = "Server"
.WriteINISettings ("MyServer")
.Key = "DBName"
.WriteINISettings ("MyDB")
.Key = "Driver"
.WriteINISettings ("MyDriver")
.Section = "Test"
.Key = "Test"
.WriteINISettings ("That")
End With
Set INI_SETTINGS = Nothing
End Sub
 
J

Jim Thomlinson

Sorry I should have mentioned that some of that code was copied from another
website that escapes me at the moment. Suffice it to say that it is not all
my work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top