George William Craig
George William Craig

Reputation: 11

Writing to a TXT file?

So i've got a Userform on VBA, and i'm making a Dungeons & Dragons style helper where you press buttons and each stat is decided for you, but i want to make it so that once you have rolled these stats, they can be saved in a text folder and opened again in the right way.

So, i have 3 different stats. Agility, skill and strength. the code currently works like this:

Private Sub agilitybutton_Click()
agility.Caption = Int(10 + ((Int((12 - 1 + 1) * Rnd + 1)) / (Int((4 - 1 + 1) * Rnd + 1))))
End Sub

Private Sub skillbutton_Click()
skill.Caption = Int(10 + ((Int((12 - 1 + 1) * Rnd + 1)) / (Int((4 - 1 + 1) * Rnd + 1))))
End Sub

Private Sub strengthbutton_Click()
strength.Caption = Int(10 + ((Int((12 - 1 + 1) * Rnd + 1)) / (Int((4 - 1 + 1) * Rnd + 1))))
End Sub

So each number is a division of two random integers and it is then rounded. The first number is between 1 and 12 and the second is between 1 and 4. The first is divided by the second and shows up in a caption next to the button.

I want to make it so that the captions of each skill. (strength.caption, skill.caption and Agility.caption) are all saved in a txt file which i can name at the push of one button.

how do i do this?

Upvotes: 1

Views: 5382

Answers (1)

Peter L.
Peter L.

Reputation: 7304

The most flexible way to operate with text files - is ADODB.Stream object. Here is the sample code for trivial text output:

TextOutput = "Hello World!"
FilePath = "C:\output.txt"
Set FileStream = CreateObject("ADODB.Stream")
FileStream.Open
FileStream.Type = 2 'Text
FileStream.Charset = "utf-8"
FileStream.WriteText TextOutput
FileStream.SaveToFile (FilePath)
FileStream.Close

Read more: ADO Stream Object and sample binary to text conversion: https://stackoverflow.com/a/14285131/1953175

Upvotes: 6

Related Questions