append to text file - i give up

append to text file - i give up

Anonymous
Not applicable
946 Views
13 Replies
Message 1 of 14

append to text file - i give up

Anonymous
Not applicable
arggggh! after wasting the better part of the last two days trying to do
this one simple thing I have to admit ignorance and ask for help

I've read and re-read the help files and searched archives and google and
can't fiind anything that has helped me understand what I'm doing wrong.
I've tried a hundred variations of getfile, createtextfile, open
statement[forWriting, ForReading, ForAppending], OpenAsTextStream,
OpenTextFile, writeline, readline etc and I cant seem to get a simple text
file and append to it.
I know this is pathetically lame that I can't figure this out on my own but
if there's any sympathy out there for a lamer...help!
my latest failed example is:

Public Sub AddToFile(filenm as string, msg As String)

'declare file locations
Dim FileLocation As String
FileLocation = "C:\temp\"

'declare file name variables
Dim FileName As String

'assign name to text file
FileName = FileLocation & filenm


'create a file object for the report file
Dim fso As Variant 'file scripting object
Set fso = CreateObject("Scripting.FileSystemObject")

Dim objFile As Variant 'actual file textstream object

'create actual file stream object
On Error Resume Next
Set objFile = fso.GetFile(FileName)
If Err Then

Set objFile = fso.CreateTextFile(FileName, True) 'allow overwrite
Err.Clear
End If

Dim objfile2 As Variant
'this is supposed to open the file for appending but it doesn't
'supposedly ForAppending etc are built in constants but they aren't
'you apparently have to declare them as constants yourself
'the help says append = 3 but the example uses 8 but I can't get either to
work
Set objfile2 = objFile.OpenAsTextStream(8) 'forappending - or (3) -
different help files

'write msg to file
'since the file was "supposedly" opened for append this should add a new
line each time it's run
'but it doesn't
objfile.Writeline msg
objfile.Close

Set objFile = Nothing
Set objfile2 = Nothing
Set fso = Nothing

End Sub
that would then be called with:
Sub testx()
AddToFile "test1.txt", "this is a test"
End Sub

I know somehow I'm making this much harder than it has to be.
This has to be the easiest thing to do and I cant figure it out....dumb dimb
dimmmerer
Thanks for any help
Mark
0 Likes
947 Views
13 Replies
Replies (13)
Message 2 of 14

Anonymous
Not applicable
Does this help

Option Explicit

Public Sub writeme()

Dim FileNumber As Integer
FileNumber = FreeFile

Dim FileName As String
FileName = "C:\Test.txt"
Dim i As Integer

For i = 1 To 4
Open FileName For Append As #FileNumber
Print #FileNumber, "Test" & i
Close #FileNumber
Next i


End Sub


--
Kent Keller
http://kwikmcad.topcities.com/

"MP" wrote in message
news:7C23C6AA78FEE52F445B8026E90463BD@in.WebX.maYIadrTaRb...
> arggggh! after wasting the better part of the last two days trying to do
> this one simple thing I have to admit ignorance and ask for help
0 Likes
Message 3 of 14

Anonymous
Not applicable
Kent Keller wrote in message
> Does this help

Hi Kent,
sure does!
can you explain the difference between all the different ways to get at a
file...I didn't know which to use for what purpose
eg:
getFile
OpenTextFile
OpenAsTextStream
CreateTextFile?
CreateTextStream?
Open statement
etc?
sometimes I can't even find references to them in the help cause i don't
know which program they belong to - autocad - vba - vb ???...like just now I
looking for CreateTextFile and it's not in the help but it was there before
now I don't know where to look for it??? mucho confuso

for example what is wrong with this approach?
Set f = fs.CreateTextFile("test1.txt", True) 'Create a file
Set ts = f.OpenAsTextStream(ForAppending, TristateUseDefault)'open to
append
I can't get it to work but from the sparce help I would think it would.
I just don't understand the basics of all this

I appreciate your solving my immediate dilema but I also want to understand
why...
Thanks again.
Mark
0 Likes
Message 4 of 14

Anonymous
Not applicable
Mark

No I can't, most of them I have never used, but looking at a couple of them they
look like they are part of scripting rather than the basic VB stuff. I just haven't
done anything with scripting. Unless you really need it, to avoid confusion I would
avoid examples that have something like Set fs =
CreateObject("Scripting.FileSystemObject") in it. It will likely add to your
confusion at this time. It is to my understanding a Light version of VB/VBA that
doesn't require a host program such as Autocad. It is they language a lot of the
email viruses are written in. I could be off base a bit, and I am sure we will get
straightened out if I am. 8^)

I think most (all?) of those methods you listed are part of VBA and Scripting....
not AutoCAD.

I usually use the Search tab rather than the Index ...especially when I am not sure
about something. The easies way though is to type it in on the IDE, Highlight it,
and hit F1. That should take you right to the proper spot in help

--
Kent Keller
http://kwikmcad.topcities.com/

"MP" wrote in message
news:B70C04D7E0F11533EBDAA92CB62F105E@in.WebX.maYIadrTaRb...
>
> can you explain the difference between all the different ways to get at a
> file...I didn't know which to use for what purpose
> eg:
> getFile
> sometimes I can't even find references to them in the help cause i don't
> know which program they belong to - autocad - vba - vb ???...like just now I
> looking for CreateTextFile and it's not in the help but it was there before
> now I don't know where to look for it??? mucho confuso
0 Likes
Message 5 of 14

Anonymous
Not applicable
Kent Keller wrote in message
> No I can't, most of them I have never used, but looking at a couple of
them they
> look like they are part of scripting rather than the basic VB stuff.
yeah, I still don't understand the fact that so many different progs can
work together.
I see some posts that have c++ looking lines in them but they're appearing
in a vb context.
very confusing to me. And all the API stuff...more very confusing

> It will likely add to your confusion at this time.
Yeah, and like I really need that added to!!!!!

> The easies way though is to type it in on the IDE, Highlight it,
> and hit F1. That should take you right to the proper spot in help
I do that a lot but sometimes get the keyword not found...but on another
machine sometimes I can get the reference so some other progs must be loaded
that have other help files or something????

Any way, thanks again for your help...that saved me another two days of
frustrating ignorant flailing about!
Mark
0 Likes
Message 6 of 14

Anonymous
Not applicable
for example what is wrong with this approach?
Set f = fs.CreateTextFile("test1.txt", True) 'Create a file
Set ts = f.OpenAsTextStream(ForAppending, TristateUseDefault)'open to
append

The CreateTextFile method returns a TextStream object.

The OpenAsTextStream method is part of the FileSystemObject and not a method
of the TextStream object, which is what you are doing wrong in the above
code.

In the first statement f is set to an open TextStream.
It is not neccesary to open it again.

If you want to open an existing text file. Use the OpenAsTextStream method
with an FileSystemObject.

Try the example below:

Option Explicit
'Reference: 'Microsoft Scripting Runtime' library

Sub AppendTextToFileEx()
'this will create and oopen a text file
'add 7 lines and close file
'then reopen for appending
'and add 3 more lines then close

Dim objFileSystem As FileSystemObject
Dim objTextFile As TextStream
Dim i As Integer

'create file system object
Set objFileSystem = CreateObject("Scripting.FileSystemObject")

'create and open a new text file, overwrite set to true
Set objTextFile = objFileSystem.CreateTextFile("C:\MyTextFile.txt",
True)

'add 7 numbered lines to text file
For i = 1 To 7
objTextFile.WriteLine i
Next i

'close text file
objTextFile.Close


'open text file for appending
Set objTextFile = objFileSystem.OpenTextFile("C:\MyTextFile.txt",
ForAppending)

'add 3 more line to text file
For i = 8 To 10
objTextFile.WriteLine i
Next i

'close text file
objTextFile.Close

End Sub
0 Likes
Message 7 of 14

Anonymous
Not applicable
W,
Thanks for that explanation.
I was flopping around all over the place - just couldn't hit the target.
Now it's much clearer.
So, given there are different ways to access a text file, what
considerations determine which method to use?
'per Kent's post
Open FileName For Append As #FileNumber
that seems very clean and simple
and now with your explanation I could as easily use the TextStream obj.
So, is there a preference and why?
And what specifically are the Scripting Runtime options designed for as
opposed to stock vb or vba? I've heard of vbScript and have some idea that
its' something for web pages???
Appreciate any clarification of the background concepts that you could
provide.

Thanks again for your response.
Mark

"W Massey" wrote in message
> The CreateTextFile method returns a TextStream object.

ah ha!
> 'Reference: 'Microsoft Scripting Runtime' library
0 Likes
Message 8 of 14

Anonymous
Not applicable
Hi Mark,

Having seen the problems you've had I'd forget the Scripting approach. The
standard VB file open with Read/Write/Append options works simply and
reliably. Why add confusion with scripting stuff.

--




Laurie Comerford
CADApps
www.cadapps.com.au

"Mark Propst" wrote in message
news:B5D8BFE1EDD16C212A7532809A679C66@in.WebX.maYIadrTaRb...
> W,
> Thanks for that explanation.
> I was flopping around all over the place - just couldn't hit the target.
> Now it's much clearer.
> So, given there are different ways to access a text file, what
> considerations determine which method to use?
> 'per Kent's post
> Open FileName For Append As #FileNumber
> that seems very clean and simple
> and now with your explanation I could as easily use the TextStream obj.
> So, is there a preference and why?
> And what specifically are the Scripting Runtime options designed for as
> opposed to stock vb or vba? I've heard of vbScript and have some idea
that
> its' something for web pages???
> Appreciate any clarification of the background concepts that you could
> provide.
>
> Thanks again for your response.
> Mark
>
> "W Massey" wrote in message
> > The CreateTextFile method returns a TextStream object.
>
> ah ha!
> > 'Reference: 'Microsoft Scripting Runtime' library
>
>
>
0 Likes
Message 9 of 14

Anonymous
Not applicable
Laurie,
I totally agree with you, that I don't need any more confusion than I
already have(don't want to be greedy now 🙂
It's just that I like to learn about the options and the background to how
and why things work so hopefully in the future the confusion will start to
diminish.
I'm definitely going to use the simpler versions for now...just like to
understand!
Mark

"Laurie Comerford" wrote in message

> Having seen the problems you've had I'd forget the Scripting approach.
> Why add confusion with scripting stuff.
0 Likes
Message 10 of 14

Anonymous
Not applicable
Hi Mark,

The difficulty with a full understanding is that it's like Newton on the
seashore looking at one grain of sand. There is simply too much to ever be
able to understand it all - or even know what there is to know.
I think as non-professional programmers, we are better getting the job done
with the tools at hand than being at the cutting edge of technology.

--




Laurie Comerford
CADApps
www.cadapps.com.au

"Mark Propst" wrote in message
news:757681341AFC640B20F325E3B91E33C0@in.WebX.maYIadrTaRb...
> Laurie,
> I totally agree with you, that I don't need any more confusion than I
> already have(don't want to be greedy now 🙂
> It's just that I like to learn about the options and the background to how
> and why things work so hopefully in the future the confusion will start to
> diminish.
> I'm definitely going to use the simpler versions for now...just like to
> understand!
> Mark
>
> "Laurie Comerford" wrote in message
>
> > Having seen the problems you've had I'd forget the Scripting approach.
> > Why add confusion with scripting stuff.
>
>
>
0 Likes
Message 11 of 14

Anonymous
Not applicable
Laurie,
I'm not disaggreeing with you at all.
It's just that till I ask the questions I don't know what tool I do have at
hand, or which is easier to use!(since they were both in the help files at
the time I was trying to get that to work)
I'm all about trying to get the job done...that's why all my code is so
messy and weird...just get this **** thing working, eh mate?
hey, just sitting in front of a computer makes me feel on the cutting edge
:)
g'day!
Mark

"Laurie Comerford" wrote in message

> I think as non-professional programmers, we are better getting the job
done
> with the tools at hand than being at the cutting edge of technology.
0 Likes
Message 12 of 14

Anonymous
Not applicable
Mark,

The TextStream object is an object-oriented approach to handling text files.
As you saw with the other examples it is just as easy to do the same task
using stock vb commands. As far as I can tell, it doesn't offer any new
functionality. On the plus side, it wraps every thing up in a neat package
which helps with code readablilty. On the down side, it has a bit of
overhead that you won't get with stock vb commands.

If I had to say which way was better, I'd say the stock vb approach works
best for text files, because of the overhead factor. But, if you examine the
FileSystemObject you will find alot more functionality that can not be
easily accomplished using stock vb commands.

Wayne
0 Likes
Message 13 of 14

Anonymous
Not applicable
Wayne,
Thank you, always thirsty for knowledge.
Mark
0 Likes
Message 14 of 14

Anonymous
Not applicable
Mark:
I assume, if you are customizing AutoCad, you also use AutoCad. Learning to
drive cad in it self is no small task Having acomplished that, learning to
customize with all the great new tools available today, Dude YOU are cutting
edge buddy! 2 ghz PCs, megs & megs of ram, live it up, life is good
wahoo
Andy

Mark Propst wrote:

> Laurie,
> I'm not disaggreeing with you at all.
> It's just that till I ask the questions I don't know what tool I do have at
> hand, or which is easier to use!(since they were both in the help files at
> the time I was trying to get that to work)
> I'm all about trying to get the job done...that's why all my code is so
> messy and weird...just get this **** thing working, eh mate?
> hey, just sitting in front of a computer makes me feel on the cutting edge
> 🙂
> g'day!
> Mark
>
> "Laurie Comerford" wrote in message
>
> > I think as non-professional programmers, we are better getting the job
> done
> > with the tools at hand than being at the cutting edge of technology.
0 Likes