Link to home
Start Free TrialLog in
Avatar of DrTribos
DrTribosFlag for Australia

asked on

MS Word Write to .ini file

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 ...
SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DrTribos

ASKER

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
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
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
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
Chris & Terry - Thank you! I will spend some time on this today.

Cheers, Steve
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
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?
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
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
Good troubleshooting, confidence restored!