It sounds like you've got the idea!
One thing though, if building a very large string, say by looping a large
array, size the string first, s=Space(nSize) then pad it with the Mid
function (don't forget to include 2*(loops-1) for the vbCrLF characters. Ask
if not sure how (but only if building a long string in many loops).
Regards,
Peter T
"deltaquattro" <(E-Mail Removed)> wrote in message
news:6c470e88-c340-47ef-aeac-(E-Mail Removed)...
Hi Peter,
I didn't do as you suggested last time, for two simple reasons:
1. I didn't get the point of returning all props as a single string,
but, seeing your example, I understand that, this way, I can use the
classic Print # statement. Clever!

2. I thought that GetProps() would return all properties on a single
line, but each CRecord must instead be written on file as
UBound(pLinea)=26 consecutive lines of text. This is a requirement of
the code which will then read the file, so I cannot alter this.
However, after reading your following example I understood that I can
write the contents of CRecord on separate lines thanks to VBCrLf
(didn't know about that!). I will try implementing your idea and see
if it works fine for me. Thnx!
Best Regards
deltaquattro
On 26 Apr, 16:42, "Peter T" <peter_t@discussions> wrote:
> I don't quite follow what's in what but why not do it the way I suggested
> last time. Give a method in each of your own objects to return a string to
> your print routine. See the example I suggested in your previous thread on
> this same subject.
>
> Note particularly GetProps() In the example it returned 3 tab separated
> values as a single string. But for your array you might want it to return
> something like this
>
> ' in the class module
> Public Function GetProps() As String
> Dim i As Long
> Dim s As String
>
> For i = LBound(pLinea) To UBound(pLinea)
> s = s & pLinea(i)
> If i < UBound(pLinea) Then
> s = s & vbCrLf
> End If
> Next
> GetProps = s
>
> ' could add any other value/string properties here on the end
> End Function
>
> and call it from your pint routine amended something like this
>
> 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 Integer, Element As Variant
>
> 'Find free file number
> On Error GoTo errH ' < new
> FNumber = FreeFile
>
> 'Create new file or overwrite existing one
> Open FileName For Output As #FNumber
>
> 'Write array to file
> For Each Element In Container
> If IsObject(Element) Then
> 'Element.PrintToFile FileUnit:=FNumber ' < comment
> Print #FNumber, Element.GetProps ' < new
> Else
> Print #FNumber, Element
> End If
> Next
>
> done: ' < new
> On Error Resume Next ' < new
> 'Close file and exit
> Close #FNumber
> Exit Function ' < new
> errH: ' < new
> Resume done ' < new
>
> End Function
>
> FWIW, personally I'd wouldn't want an all purpose print routine like this.
> Determine what "container" is before sending it to any print routine and
> cater accordingly
>
> Regards,
> Peter T
>
> "deltaquattro" <deltaquat...@gmail.com> wrote in message
>
> news:be2fa5e3-1aeb-4751-afa5-(E-Mail Removed)...
> Hi Peter,
>
> thanks for your answer and your suggestions. However, I still have the
> same error.
> I found out that when test() and WriteDataToFile() are located in the
> same Module1 inside the same workbook (for example Test.xls), and
> CRecord is in the same workbook, then no problem occurs.
> However, in my setup, is inside MyLibrary.xla, a collection of all
> modules which I reuse in different projects, while Module1 (test())
> and CRecord are inside Test.xls. MyLibrary.xla is added to the
> References of Test.xls. In this case, I get the error:
>
> "Run-time error 52:
> Bad file name or number"
>
> Should I give up on the idea of adding my more or less general data
> writing function in to MyLibrary.xla? Or is there a way to get things
> working? Thanks again,
>
> Best Regards,
>
> deltaquattro
>
> On 26 Apr, 13:21, "Peter T" <peter_t@discussions> wrote:
>
> > Works fine for me although I did have to change
> > For I = 1 To NLines
> > to
> > For I = 1 To Ubound(pLinea)
>
> > I can see all sorts of problems ahead unless you are very careful, eg
> > what
> > happens if/when
> > If IsObject(Element) Then
> > but Element doesn't have a method .PrintToFile
>
> > At the very least I'd suggest
>
> > on error goto errExit
> > FNumber = FreeFile
>
> > 'code
>
> > done:
> > on error resume next
> > Close #FNumber
>
> > exit function
> > errExit:
> > resume done
> > end function
>
> > Another thought, "foo.txt"? The code would fail if the curDir doesn't
> > have
> > access to write files (often a problem Vista / W7), why not qualify it,
> > eg
> > application.defaultfilepath & "\foo.txt"
>
> > Regards,
> > Peter T