Solved

MS Word Write to .ini file

Posted on 2013-01-02
12
1,309 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

685 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