0

I want to write a text file that contains all the data from within an object. I know how to create and open a text file using vba, also how to write from a range or string going through every line but I have not found a solution for doing the same with an object. Any help would be appreciated. The object in question is genereated by parsing a .json file into an object in vba and then selecting the necessary objects withing.

Public Sub exportobejct(obj)
    dim path as string
    dim filename as string
    dim filenumber as string

    path = "C:\Mypath\"
    filename = "myfilename"
    filenumber = Freefile
    Open filepath & filename for output as filenumber
'Here would be the code to write into the textfile, which I failed 
'What I tried:
    For r in obj.rows.cells.count-1
        for c in obj.columns.cells.count-1
            print filenumber, obj.cells(r,c)
'or
            print filenumber, obj.cells(1).offset(r,c).value
        next c
    next r
'sth else I tried:
    Print filenumber, obj
'or
    for each cc in obj.rows
        write filenumber, cc 
    next cc

    Close filenumber
End sub

Ive seen there is also an option creating files using FSO see Link: https://stackoverflow.com/questions/11503174/how-to-create-and-write-to-a-txt-file-using-vba

I still dont know how to write from an object however.

Some additional information: The object that I want to write from may contain several more objects > within, if that poses any issues.

  • Don't you want to write the object out as JSON to the file? In this manner, the object can be recreated to/from JSON. Am I missing something? – PeterT Jun 05 '23 at 13:04
  • @PeterT I am fairly new to vba, so maybe the question is super stupid. All I want is a textfile of all the things inside that object. I can open the json file with an editor and see whats inside but I do not know how I see whats inside the object variable that I imported parts of that json file. I dont know how to export it. – vba_enjoyer Jun 05 '23 at 13:39
  • What kind of object is your code producing? Is it a VBA class? There must be some kind of `Set obj = new ...` in the code: What is it? Where is it defined? – FunThomas Jun 05 '23 at 15:34
  • There is no built-in support for persisting a generic "object" to a file - you would need to provide a method which "serializes" the object into a form which can be written to a file (and from which you can then reconstitute the object at a later time). See https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/serialization/ (.NET but covers the basic concept) – Tim Williams Jun 05 '23 at 15:45
  • Because you have a custom object, it's up to you to define how (what format) to write that object to a file. In general, writing it out as text (CSV, JSON, INI, etc) is perfectly acceptable - it's really up to you. Since you're already working with JSON, you may want to write your object to a JSON file. [Here is a handy reference](https://stackoverflow.com/a/46245469/4717755) for how to map objects (or even compound objects) to JSON that might help. – PeterT Jun 05 '23 at 17:36

0 Answers0