DrTribos
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.PrivateProfileStrin g("C:\path \my.ini", "GroupLabel", "DataLabel")
But updating an ini file with user preferences etc. still eludes me ...
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.PrivateProfileStrin
But updating an ini file with user preferences etc. still eludes me ...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
As provided I know it works 2003 - 2012.
Chris
The other observation is on a quick test, (haven't used the System.PrivateProfileStrin g 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.PrivateProfileStrin g method appeals to you then it is certainly more streamlined for the specific application.
Chris
On my quick analysis of the options I would say that I prefer the old approach - but if the System.PrivateProfileStrin
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.PrivateProfileStrin g 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
Private Profile Strings using Words System.PrivateProfileStrin
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
ASKER
Chris & Terry - Thank you! I will spend some time on this today.
Cheers, Steve
Cheers, Steve
ASKER
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
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?
ASKER
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
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
ASKER
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
Cheers, S
Good troubleshooting, confidence restored!
ASKER
Cheers, S