Solved

MS Word Write to .ini file

Posted on 2013-01-02
12
1,223 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 14

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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 14

Author Comment

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

Cheers, Steve
0
 
LVL 14

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 14

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 14

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now