Solved

MS Word Write to .ini file

Posted on 2013-01-02
12
1,330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

739 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