Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write VBA Dictionary to text file

Tags:

excel

vba

I have the twitter search api results stored as an Excel VBA Dictionary. I would like to write the dictionary to a text file. How do I do that?

like image 648
richie Avatar asked Jul 17 '26 04:07

richie


1 Answers

Assuming the keys and values in this dictionary are string, you could call a function like this and then use FileSystemObject or other method to create and write a textfile (example here).

Simply pass your dictionary object to the function in the FSO.WriteFile line like:

Dim fso as Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile as Object
Set oFile = FSO.CreateTextFile("C:\desktop\textfile.txt") 'Modify as needed
oFile.Write PrintDictionary(twitter_dictionary)           'modify as needed
set fso = Nothing: set oFile = Nothing

Here is the function:

Function PrintDictionary(dict as Object) As String

Dim k as Variant
Dim i as Long
Dim fullText as String

For each k in dict.Keys()
    fullText = fullText & k & vbTab & dict(k) & vbCrLF
Next

PrintDictionary = fullText

End Function
like image 196
David Zemens Avatar answered Jul 19 '26 03:07

David Zemens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!