Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading and writing to an ini file VBA Word 1

Status
Not open for further replies.

kalle82

Technical User
Apr 2, 2009
163
SE
Hi!

I'm about to start expand an program i built at work to include the storing of text's for later use.

Here's what I want to do.

I have a textbox where the user can put the text they want to save for later, they also must name their saved text.

So question is how do i create an function that creates an .ini file in a specfic location like c:\inifile.ini

and a function that writes the text stored in my textboxes.

I know Ill have do divide as name and content will differ. So when creating the ini file ill have to write something to start from.

Anyone can pint to code that does this can't be the first time cheers!

//Carl
 
Let's say you want to make an INI file with the following content.

[FirstItem]
yadda = "first item first data"
blah = "first item second data"

[SecondItem]
whatever = "second item first data"
something = "second item second data"

Obviously the string values can be whatever you want. They could just as easily be:

[FirstItem]
yadda = TextBox2.Text

So....
Code:
Sub MakeMyINI()
Documents.Add
Selection.TypeText _
   "[FirstItem]" & vbCrLf & "yadda = " & Chr(34) & "first item first data" & Chr(34) & _
   vbCrLf & "blah = " & Chr(34) & "first item second data" & Chr(34) & _
   vbCrLf & _
   "[SecondItem]" & vbCrLf & "whatever = " & Chr(34) & "second item first data" & Chr(34) & _
   vbCrLf & "something = " & Chr(34) & "second item second data" & Chr(34)

ActiveDocument.SaveAs "c:\myINI.ini", Fileformat:=wdFormatText
End Sub
The code above makes a new document (Documents.Add), and then "types in" the text I want:
Code:
   "[FirstItem]" & vbCrLf & "yadda = " & Chr(34) & "first item first data" & Chr(34) & _
   vbCrLf & "blah = " & Chr(34) & "first item second data" & Chr(34) & _
   vbCrLf &
comes out as:
[FirstItem]
yadda = "first item first data"
blah = "first item second data"

It then save the new document as c:\myINI.ini in plain text format.

Done.

Now to test it...
Code:
Sub TryItAndSee()

MsgBox System _
   .PrivateProfileString("c:\myINI.ini", "FirstItem", "yadda")

End Sub

Ta-da! The messagebox displays "first item first data".

Gerry
 
Wow thank you guys!

It working really good!

You saved me a bunch of time and at the same time I learned something really useful.

I hope I'm soon able to repay the favor!

///Carl
 
Just remember the syntax:

System.PrivateProfileString(filename,Section, Key[i/])

with:

filename = valid path/filename, as STRING (either literal, or string variable)

Section = text between square brackets, as STRING (either literal, or string variable)

Key = the item name; the left side of the =

Note that you can also use this to get values from the Registry. If you do, the filename must be "".
Code:
aName = System.PrivateProfileString("", _
    "HKEY_CURRENT_USER\Software\Microsoft\" _
    & "Windows\CurrentVersion\Internet Settings", "EmailName")
MsgBox aName

Gerry
 
Of course, as the return value is, in fact, a string, technically, unless you need it, you do not have to use a variable....
Code:
Msgbox System.PrivateProfileString("", _
    "HKEY_CURRENT_USER\Software\Microsoft\" _
    & "Windows\CurrentVersion\Internet Settings", "EmailName")

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top