Author | Message | Time |
---|---|---|
Sorc_Polgara | I want to be able to return a TextStream object... I want to put it in a module and I want it to be like a Public Function. I'm using a TextStream object frequently and repeating the same code over and I thought that I could make it function to save some time. I've tried making a Public Function but I can't get it to work. In order to make the TextStream object (tst) that I want, a FileSystemObject (fso) must be set. Here is my code in the module [code] Public Function CreateTST(ByVal FileName As String, ByVal IOMode As Long, ByVal Create As Boolean) As TextStream Dim fso As FileSystemObject Dim tst As TextStream Set fso = New FileSystemObject Set tst = fso.OpenTextFile(FileName, IOMode, Create) CreateTST = tst End Function [/code] It works fine when I insert the code into the Sub that I want it to be used in. This is the call I make [code] Dim tst As TextStream tst = CreateTST(strFileName, 1, False) [/code] the argument "1" in the function call is the numerical representation for constant ForReading which must be stated inorder to use OpenTextFile. I think it should work! but all I get is the "Run-time error '5': Invalid Procedure call or argument" Please help! How can I return a TextStream/FileSystemObject![code][/code] | June 29, 2004, 1:22 AM |
Stealth | Try returning it as a Variant into a TextStream object: [code] Dim objTS as TextStream objTS = CreateTST(myFilename, 1, True) '// ... Public Function CreateTST(ByVal FileName As String, ByVal IOMode As Long, ByVal Create As Boolean) As Variant[/code] | June 29, 2004, 1:34 PM |
Sorc_Polgara | [quote author=Stealth link=board=31;threadid=7487;start=0#msg67743 date=1088516078] Try returning it as a Variant into a TextStream object: [code] Dim objTS as TextStream objTS = CreateTST(myFilename, 1, True) '// ... Public Function CreateTST(ByVal FileName As String, ByVal IOMode As Long, ByVal Create As Boolean) As Variant[/code] [/quote] I changed it, but I get a different run-time error, "Run-time error '438': Object doesn't support this property or method" Here is the code calling to the function... [code] Dim tst1 As TextStream tst1 = CreateTST(strFileName, 1, False) [/code] ... the function in the module [code] Public Function CreateTST(ByVal FileName As String, ByVal IOMode As Long, ByVal Create As Boolean) As Variant Dim fso As FileSystemObject Dim tst As TextStream Set fso = New FileSystemObject Set tst = fso.OpenTextFile(FileName, IOMode, Create) CreateTST = tst End Function [/code] When I debug it the following is highlighted in the function above, [code] CreateTST = tst [/code] Is there another way to return an object like a FileSystemObject? | June 29, 2004, 6:26 PM |
Stealth | Perhaps create it ahead of time and pass it ByRef? | June 29, 2004, 10:58 PM |
Adron | You could also try adding "Set" like this: [code] Set tst = CreateTST(strFileName, 1, False) [/code] Your assignments without "Set" may be trying to assign to the default property. | June 30, 2004, 10:37 PM |