Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
How to write to file the contents of a generic array/collection
Hi,
I'm trying to write a general subroutine to print the contents of an array/collection to a file . So far I've come up with the following subroutine: Function WriteDataToFile(FileName As String, Container As Variant) As Boolean 'Write each element contained in Container (either an array or a collection) to file FileName Dim FNumber As Long, Element As Variant 'Find free file number FNumber = FreeFile 'Create new file or overwrite existing one Open FileName For Output As #FNumber 'Write array to file For Each Element In Container Print #FNumber, Element Next 'Close file and exit Close #FNumber End Function Albeit simple, this function is able to handle perfectly multidimensional arrays and collection of standard and user-derived type. However, the Print # function doesn't work for an Object data type, so it doesn't work for arrays/collections which contain Objects. How can I write a function which is able to write to file the contents of an array/collection, irrespective of what type of variables it holds? My idea: add code into WriteDataToFile which, For Each Element In Container, determines whether Element is a variable of standard/user defined data type or an Object. In the first case I use the Print function, in the second I call a Print method which I will define in all of the class module I write. Two questions arise: 1. How do I determine if Element is of standard/user defined data type or Object data type? 2. How do I write the Print method for my Classes? I tried something like: ' Print Method Public Function Print(FileUnit As Long) 'here goes code which prints each Property to file, something like: Print #FileUnit, Name Print #FileUnit, Surname .. .. .. End Function but this didn't work because Print is a reserved keyword, so I renamed it PrintToFile (btw, is there any workaround which would still allow me to name the method Print?) Well, that's all for now, I'll appreciate if you can answer my questions, or suggest a completely different approach, in case you think mine is unfeasible. Thanks in advance, Best Regards deltaquattro |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Generic file path using vba | Excel Programming | |||
Array vs. Collection | Excel Programming | |||
check if file with generic name already open | Excel Programming | |||
write 1-dimensional array of integers to text file | Excel Programming | |||
Write to Array from other closed Excel file | Excel Programming |