Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Word, PowerPoint, Outlook, and Other Office Products > Writing or Saving String to Text File


Reply
 
Thread Tools Display Modes
  #1  
Old 02-06-2003, 03:23 AM
Cassie
Guest
 
Posts: n/a
Question Writing or Saving String to Text File


Hello! I'm having problem writing or saving string to a text file particularly into notepad. Here's my code:


DbaseString - variable for my string

Dim f, a
Dim inFileNumber As Integer
Dim strLine As String
Dim lngFileLength As Integer

CommonDialog1.CancelError = True
On Error GoTo ErrHandler
CommonDialog1.DialogTitle = "Select a file name"
CommonDialog1.Flags = cdlOFNHideReadOnly
CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files" & _
"(*.txt)|*.txt|Batch Files (*.bat)|*.bat"
CommonDialog1.FilterIndex = 2
CommonDialog1.ShowSave
MsgBox "Save to Path: " + CommonDialog1.FileName

intFileNumber = FreeFile
lngFileLength = FileLen(CommonDialog1.FileName)
Open CommonDialog1.FileName For Input As intFileNumber _
Len = lngFileLength
txtDocument.Text = Input$(lngFileLength, intFileNumber)
Close intFileNumber

Set f = fs.OpenTextFile(CommonDialog1.FileName, ForWriting, TristateFalse)
f.Write (DbaseString)
f.Close

Exit Sub

ErrHandler:
Exit Sub

---------

I'm not sure with the other codes. I just tried them and they work fine. I think what's lack in there is the saving or writing string into text file. Is this code,

f.Write (DbaseString)

suppose to write string to text file?

This codes is about locating the text file through commondialog box and then I want to save my string to that text file. Just overwrite any characters save in there if there is.

I will appreciate any of your help. Thank you in advance.
Reply With Quote
  #2  
Old 02-06-2003, 05:22 AM
gallicus gallicus is offline
Junior Contributor
 
Join Date: Dec 2001
Location: at work... probably :-(
Posts: 366
Default

to write to a text file you must open it for output or append

Open "Text.txt" For Output As #1

then use

print #1, "string"

to write to the file. Not really sure what you are meant to be doing with

Set f = fs.OpenTextFile(CommonDialog1.FileName, ForWriting, TristateFalse)
f.Write (DbaseString)
f.Close
__________________
(A)bort, (R)etry, (G)et a beer?
Reply With Quote
  #3  
Old 02-06-2003, 07:09 AM
NateBrei's Avatar
NateBrei NateBrei is offline
Contributor
 
Join Date: Jul 2002
Location: Omaha, NE
Posts: 571
Default

A couple of things I see:
1) The last section of code looks like it is using FileSystemObjects (set reference to it from Tools, References, Microsoft Scripting Runtime -- scrrun.dll) to write to your textfile. Make sure you have your reference set and that you have 'fs' declared as a FileSystemObject and 'f' declared as a File object.
Code:
Dim fs As FileSystemObject Dim f As File '****** Set fs = New FileSystemObject '******
2) In this section, you use DbaseString as the argument to the Write method, but in your code sample you never set the value of DbaseString. Therefore, it would/could be blank which would explain why it seems that nothing is being written to the text file.
Code:
Set f = fs.OpenTextFile(CommonDialog1.FileName, ForWriting, TristateFalse) f.Write (DbaseString) f.Close
3) At the top of your module, you should add the following statement before anything else in the module. It will require you to declare all variables before you can use them (otherwise you will get an undeclared variable error when you try to run). This will help you figure out what is happening with your code. I highly encourage this...
Code:
Option Explicit
Hope these suggestions help.

Nate
Reply With Quote
  #4  
Old 02-06-2003, 08:23 PM
Cassie
Guest
 
Posts: n/a
Default

Quote:
Originally posted by gallicus
to write to a text file you must open it for output or append
Open "Text.txt" For Output As #1

then use

print #1, "string"

to write to the file. Not really sure what you are meant to be doing with

Set f = fs.OpenTextFile(CommonDialog1.FileName, ForWriting, TristateFalse)
f.Write (DbaseString)
f.Close
Thanks gallicus! The open and print solved the problem.
Reply With Quote
  #5  
Old 02-06-2003, 08:27 PM
Cassie
Guest
 
Posts: n/a
Default

Quote:
Originally posted by NateBrei
A couple of things I see:
1) The last section of code looks like it is using FileSystemObjects (set reference to it from Tools, References, Microsoft Scripting Runtime -- scrrun.dll) to write to your textfile. Make sure you have your reference set and that you have 'fs' declared as a FileSystemObject and 'f' declared as a File object.
Code:
Dim fs As FileSystemObject Dim f As File '****** Set fs = New FileSystemObject '******
2) In this section, you use DbaseString as the argument to the Write method, but in your code sample you never set the value of DbaseString. Therefore, it would/could be blank which would explain why it seems that nothing is being written to the text file.
Code:
Set f = fs.OpenTextFile(CommonDialog1.FileName, ForWriting, TristateFalse) f.Write (DbaseString) f.Close
3) At the top of your module, you should add the following statement before anything else in the module. It will require you to declare all variables before you can use them (otherwise you will get an undeclared variable error when you try to run). This will help you figure out what is happening with your code. I highly encourage this...
Code:
Option Explicit
Hope these suggestions help.

Nate
Thanks Nate! I forgot to add the "option explicit" statement and declare my variable.

As for those FileSystemObject, thank you also for was I able to understand its function.

---

This problem is solved.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->