Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TextStream.WriteLine problem

Status
Not open for further replies.

hyarmion

Technical User
Dec 23, 2008
20
AU
I am having trouble using the WriteLine method to write out to a textstream:

The code is as follows:

Global gFSO as New FileSystemObject
Global gtsLog as TextStream

In a different module:

Private Sub A()
...
Set gtsLog = gFSO.OpenTextFile(<path>, ForAppending, True, TristateFalse)
gtsLog.WriteLine ("some text") '<== No problem here
...
End Sub

Private Sub B()
...
gtsLog.WriteLine ("more text") '<== Error message
...
End Sub

The error message reads:
Object variable or With Block not set.

This has happened twice in two unrelated programs I have been working on over the last week, where am I going wrong?
Any ideas?

Steve.
 

Do you close gtsLog anywhere between Sub A and Sub B?
Do you [tt]Set gtsLog = Nothing[/tt] anywhere?

Have fun.

---- Andy
 
No, I dont close gtsLog nor set it to Nothing.
Sub A and Sub B are as above, one directly after the other, no code in between.

Steve.
 
>>> Sub A and Sub B are as above, one directly after the other, no code in between. <<<<

That doesn't matter at all, what matters is the order that the subs get called. Either one of the following is happening:

1. You are calling Sub B before Sub A
2. In between calling Sub A and Sub B, you are setting gtsLog to Nothing
 
Set a Watch on gtsLog configured to "Break when Value Changes" and you should be able to trap where and when it gets set to Nothing (and it will probably confirm JoeAtWork's comment)
 
Found the problem!

It was, I think what is technically called an 'ID10T error', a stuff up on my part (I had failed to notice that in Sub A() there was an Exit Sub statement before gtsLog was set to the textstream, so gtsLog was never actually set, and the initial WriteLine statement, the one that was not giving any problem, was not actually being executed!)

But thanks to strongm for suggesting I set a watch, as this helped locate the problem.

Steve.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top