View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Cannot write the contents of an Object to file

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" wrote in message
...
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