Solved

MS Word Write to .ini file

Posted on 2013-01-02
12
1,266 Views
Last Modified: 2013-01-02
Hi All

I want to use an ini file (or similar, also looking at an xml file) for configuring the way macros run.

I found out how to read from an ini file and populate a textbox on a userform (this was surprisingly simple):

TextBox1.Text = System.PrivateProfileString("C:\path\my.ini", "GroupLabel", "DataLabel")

But updating an ini file with user preferences etc. still eludes me ...
0
Comment
Question by:DrTribos
  • 5
  • 4
  • 3
12 Comments
 
LVL 59

Assisted Solution

by:Chris Bottomley
Chris Bottomley earned 250 total points
ID: 38735855
I use:

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

Public Function GetINIString(ByVal sApp As String, ByVal sKey As String, Optional CONFIG_FILE As String) As String
   'getinistring("outlook", "fred")
   If CONFIG_FILE = "" Then CONFIG_FILE = Environ("temp") & "\CRB.ini"
   Dim sBuf As String * 32768
   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, _
Optional CONFIG_FILE As String) As Boolean
   'WriteINIString("outlook", "fred", "Right Said Fred")
   If CONFIG_FILE = "" Then CONFIG_FILE = Environ("temp") & "\CRB.ini"
   WritePrivateProfileString sApp, sKey, sValue, CONFIG_FILE
End Function
 
   'getinistring("outlook", "fred")
   'WriteINIString("outlook", "fred", "Right Said Fred")

Open in new window


Chris
0
 
LVL 16

Accepted Solution

by:
terencino earned 250 total points
ID: 38735913
Updating preferences is much the same, just use
System.PrivateProfileString("C:\path\my.ini", "GroupLabel", "DataLabel") = TextBox1.Text

Open in new window

...Terry
0
 
LVL 15

Author Comment

by:DrTribos
ID: 38736353
Hi Chris... I keep finding ppl using these large blocks of code... I am yet to try Terry's approach but is there a reason for all the code?
Cheers, S
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 38736372
I don't know exactly when the systemprivateprofilestring was introduced but I suspect it was post 2003.  That being so the small methods may be fine for users with later installs but they can be fraught for users of older systems.

As provided I know it works 2003 - 2012.

Chris
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 38736452
The other observation is on a quick test, (haven't used the System.PrivateProfileString method myself) I note it works for Word but not excel at issue 2010.  Often you will find quick solutions with limited application but often recommendations will be to use a more generic approach as it is less likely to trip you up in the future.

On my quick analysis of the options I would say that I prefer the old approach - but if the System.PrivateProfileString method appeals to you then it is certainly more streamlined for the specific application.


Chris
0
 
LVL 16

Expert Comment

by:terencino
ID: 38738150
Yes this is definitely a legacy Word feature, like the WordBasic.SortArray function, they are not directly available in other Office applications. However, you can get it to work in Excel without using the Windows API.

Private Profile Strings using Words System.PrivateProfileString using VBA in Microsoft Excel

VBA is a cutback version of VB, a very powerful programming language. For functions that are not available natively in VBA, it is useful to have a library of Windows API functions such as the one Chris has provided. It is also important to note that Microsoft tends to support legacy application-specific code, and some shortcuts are available as long as you are aware of the limitations!
Happy programming
...Terry
0
 
LVL 15

Author Comment

by:DrTribos
ID: 38738221
Chris & Terry - Thank you! I will spend some time on this today.

Cheers, Steve
0
 
LVL 15

Author Comment

by:DrTribos
ID: 38738311
OK... so I can write to an ini file using the line from Terry... BUT if I have existing data and try to add additional data then all the data in that group disappears.  I did see an article somewhere about writing a temporary .ini file and then replacing the original and also looping through the original to rewrite the data...

Oddly (thankfully) other [Groups] were left unchanged when I wrote to the target group...

Cheers, S
0
 
LVL 16

Expert Comment

by:terencino
ID: 38738388
That's strange Steve, I can write new groups and values to an existing ini file no problems. Can you post the code that is causing the problem?
0
 
LVL 15

Author Closing Comment

by:DrTribos
ID: 38738640
Hi Terry & Chris

Um... for reasons unknown I am unable to reproduce the effect that I described above... I have tested over and over and all works perfectly...

Thank you both for your help, I am hoping to not support W2003 with this but that might be wishful thinking...

Cheers S
0
 
LVL 15

Author Comment

by:DrTribos
ID: 38738674
FWIW - It happened again, but I think I might have had the .ini file open in an editor so saving got messed up...

Cheers, S
0
 
LVL 16

Expert Comment

by:terencino
ID: 38738678
Good troubleshooting, confidence restored!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

776 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