[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Reading data from ini-file using VBA i Outlook 2003

Posted on 2009-04-25
8
Medium Priority
?
1,468 Views
Last Modified: 2012-08-14
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?
0
Comment
Question by:Zoodiaq
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 27

Assisted Solution

by:bluntTony
bluntTony earned 800 total points
ID: 24232118
Your on the right tracks. Below is a basic example of using PrivateProfileString.
Assuming you're using the standard ini format, i.e.
[Section Header]
Data=Value
The arguments for the sub are 1. the ini file path, 2. the section header, 3. the value to return. For more info have a look here: http://exceltip.com/st/Private_Profile_Strings_using_INI-files_using_VBA_in_Microsoft_Excel/490.html

Private Declare Function GetPrivateProfileStringA Lib _
    "Kernel32" (ByVal strSection As String, _
    ByVal strKey As String, ByVal strDefault As String, _
    ByVal strReturnedString As String, _
    ByVal lngSize As Long, ByVal strFileNameName As String) As Long
 
Sub ReadIni()
    MsgBox (GetPrivateProfileString32("C:\tony.ini", "TEST", "String"))
End Sub
 
Private Function GetPrivateProfileString32(ByVal strFileName As String, _
    ByVal strSection As String, ByVal strKey As String, _
    Optional strDefault) As String
Dim strReturnString As String, lngSize As Long, lngValid As Long
    On Error Resume Next
    If IsMissing(strDefault) Then strDefault = ""
    strReturnString = Space(1024)
    lngSize = Len(strReturnString)
    lngValid = GetPrivateProfileStringA(strSection, strKey, _
        strDefault, strReturnString, lngSize, strFileName)
    GetPrivateProfileString32 = Left(strReturnString, lngValid)
    On Error GoTo 0
End Function

Open in new window

0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24232122
The link I provided is actually for Excel, but the concept is the same for Outlook VBA as well.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 24232174
Hi, Zoodiaq.

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

http://www.pc4u.com/plus/technotes/RegistryVB.html
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 1200 total points
ID: 24232264
Hello Zoodiaq,

I have been using teh following in a module to read/write ini strings

As you can see where I want to read or write data I use for example:
getinistring("outlook", "fred")
or
WriteINIString("outlook", "fred", "Right Said Fred")

Regards,
Chris
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault 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 lpString As String, ByVal lpFileName As String) As Long
  
  
Private Const CONFIG_FILE = "C:\Users\fred\ee\flintstone.ini"
  
  
Public Function GetINIString(ByVal sApp As String, ByVal sKey As String) As String
'getinistring("outlook", "fred")
Dim sBuf As String * 256
    Dim lBuf As Long
  
    lBuf = GetPrivateProfileString(sApp, sKey, "", sBuf, Len(sBuf), CONFIG_FILE)
    GetINIString = Left$(sBuf, lBuf)
End Function
  
  
Public Function WriteINIString(ByVal sApp As String, ByVal sKey As String, ByVal sValue As String) As Boolean
'WriteINIString("outlook", "fred", "Right Said Fred")
    WritePrivateProfileString sApp, sKey, sValue, CONFIG_FILE
End Function

Open in new window

0
 

Author Comment

by:Zoodiaq
ID: 24232569
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?
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 24232599
'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

0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24232605
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

0
 

Author Comment

by:Zoodiaq
ID: 24232767
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.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Take a look at these 6 Outlook Email management tools which can augment the working and performance of Microsoft Outlook to give you a more rewarding emailing experience.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question