Link to home
Start Free TrialLog in
Avatar of Zoodiaq
Zoodiaq

asked on

Reading data from ini-file using VBA i Outlook 2003

I have an ini-file with som data, and need to read the data with a vba-script created within Outlook 2003. I have another script in word where I used the command System.PrivateProfileString That command doesn't work in Outlook.

Then I found a solution stating, that I should add a reference to Microsoft Word object Library and use the following command:

Dim wd As Word.Application 'for testing
Set wd = New Word.Application ' create the Word application object
wd.System.PrivateProfileString xxxxxxxx
Set wd = Nothing ' destroy the Word application object 'for testing

That solution works, but has two drawbacks. 1) Its slow 2)it opens a word instance, and it doesn't close that word instance again. So its really not a solution.

What to do?
SOLUTION
Avatar of bluntTony
bluntTony
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The link I provided is actually for Excel, but the concept is the same for Outlook VBA as well.
Avatar of David Lee
Hi, Zoodiaq.

I've been using this code to read/write .ini files.

http://www.pc4u.com/plus/technotes/RegistryVB.html
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zoodiaq
Zoodiaq

ASKER

Thx everyone

bluntTonys I got i working, but how do I write to an ini with that method.

chris_bottomley your methos looks simpler, fewer line, but i can't get it to work. Outlook cant even start the macro with F8. Am I missing some code?
'My' macros are functions you use in your normal code rather than something you call via F8.

For example in the main code:

Dim myparam As Variant
   
    myparam = GetINIString("outlook", "fred")

i.e. the sample module below when executed returns a blankstring the first time and "Hello World" the second

Chris
Sub getsetparam()
Dim myparam As Variant
    
    myparam = GetINIString("outlook", "fred")
    WriteINIString "outlook", "fred", "Hello WOrld"
    myparam = GetINIString("outlook", "fred")
 
End Sub

Open in new window

To write, add the following declaration and function to your code.
Actually both bits of code are doing the same thing, using the same functions from Kernel32. The example I have posted is just a bit more 'flowery'
To use this function you would call it like this:
WritePrivateProfileString32 IniFileName, "SECTION",  "Data", "Value"
(I would change the function names to something a bit shorter as well!)

Private Declare Function WritePrivateProfileStringA Lib _
    "Kernel32" (ByVal strSection As String, _
    ByVal strKey As String, ByVal strString As String, _
    ByVal strFileNameName As String) As Long
 
Private Function WritePrivateProfileString32(ByVal strFileName As String, _
    ByVal strSection As String, ByVal strKey As String, _
    ByVal strValue As String) As Boolean
Dim lngValid As Long
    On Error Resume Next
    lngValid = WritePrivateProfileStringA(strSection, strKey, _
        strValue, strFileName)
    If lngValid > 0 Then WritePrivateProfileString32 = True
    On Error GoTo 0
End Function

Open in new window

Avatar of Zoodiaq

ASKER

Thx works, and I can move on with my script. Fantastic thx. I ended up using chris_bottomleys script however bluntTony's script works as well. I'm gonna split the points:

chris_bottomley 300
bluntTony 200

Thx. again.