Writing to a text file from different SUBrouteens

  • Thread starter Thread starter Julian Brotherton
  • Start date Start date
J

Julian Brotherton

I open a file using "job" as my file handler, but no other 'sub' can see the
file handler.
The debugger stops with the error "object required"


Public Sub make_job_file()
' create GEO_FILE
Dim fso, f1, job
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("geo_job.job")
Set f1 = fso.GetFile("geo_job.job")
Set job = f1.OpenAsTextStream(ForWriting, True)
End Sub

Sub jobname()
job_name = InputBox("input Job Name", "Job Name", job_name, 100, 1)
job.WriteLine "51=" + job_name
Job_date = InputBox("input correct date", "Job Date", Job_date, 100, 1)
job.WriteLine "51=" + Job_date
End Sub

Sub station()
job.WriteLine "2=" + Cells(row, B) ' AT STATION
job.WriteLine "3=" + Cells(row, H) ' INSTRUMENT HEIGHT
job.WriteLine "21=0.0000" ' DUMMY RO ANGLE NOT USED
End Sub

Can you spot where I have gone wrong.
Please don't tell me one sub routine can't see another subroutine's
variables.

Julian B
 
job is only defined in the make_job_file routine.

go to vba help and search for help on "scope"

Public Sub make_job_file()
' create GEO_FILE
Dim fso, f1, job
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("geo_job.job")
Set f1 = fso.GetFile("geo_job.job")
Set job = f1.OpenAsTextStream(ForWriting, True)
jobname job
station job

End Sub

Sub jobname(job)
job_name = InputBox("input Job Name", "Job Name", job_name, 100, 1)
job.WriteLine "51=" + job_name
Job_date = InputBox("input correct date", "Job Date", Job_date, 100, 1)
job.WriteLine "51=" + Job_date
End Sub

Sub station(job)
job.WriteLine "2=" + Cells(row, B) ' AT STATION
job.WriteLine "3=" + Cells(row, H) ' INSTRUMENT HEIGHT
job.WriteLine "21=0.0000" ' DUMMY RO ANGLE NOT USED
End Sub

allows the other routines to see job. (as an example. Probably doesn't do
what you want, but might give you some ideas about passing variables as
arguments).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top