Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Problems opening a txt file

Posted on 2003-03-07
2
Medium Priority
?
132 Views
Last Modified: 2010-05-01
Hi everyone:

I am using the following code to create a directory if it does not already exists and a new file inside of it where I am intending to write the printers configuration of a computer. The problem is that once the blank txt file is created I am not able to open it so as a result I can't write on it. The following error shows up "Permission Denied". I would be interested to know also if there is any control or interface that comes already with VB and that will help with the printers configuration.


Private Function readPrinters()

Dim directoryLocationPrinters As String
Dim fileLocationPrinters As String
Dim fs As Variant
Dim a As Variant

directoryLocationPrinters = "c:\Printers\"
fileLocationPrinters = "c:\Printers\printers.txt"

If dirExists(directoryLocationPrinters) Then
   If fileExists(fileLocationPrinters) Then
   Else
       Set fs = CreateObject("Scripting.FileSystemObject")
       Set a = fs.CreateTextFile(fileLocationPrinters, True)
       Open fileLocationPrinters For Output Access Write As #2
            Print #2, "LPT1"
            Print #2, "LPT2"
       Close #1
   End If
Else
   MkDir directoryLocationPrinters
   Set fs = CreateObject("Scripting.FileSystemObject")
   Set a = fs.CreateTextFile(fileLocationPrinters, True)
   Open fileLocationPrinters For Output As #2
        Print #2, "LPT1"
        Print #2, "LPT2"
   Close #1
End If

End Function


Thank you in advance for any help  !!!


Rene
0
Comment
Question by:chrome2000
  • 2
2 Comments
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 200 total points
ID: 8091461
Private Sub readPrinters()
Dim dirLocPrint As String
Dim fileLocPrint As String
Dim ff As Integer
dirLocPrint = "c:\Printers"
fileLocPrint = "c:\Printers\printers.txt"

If Dir(dirLocPrint, vbDirectory) = "" Then MkDir dirLocPrint

ff = FreeFile
Open fileLocPrint For Output As #ff
           Print #ff, "LPT1"
           Print #ff, "LPT2"
      Close #ff
End Sub
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 8091487
I don't see a need to use FSO in your code.It adds unneccessary overhead to the project.When you open a file for output(or append) it will be created if it does not exist.Also you were opening as File #2,but were closing File #1. Also it doesn't appear your function is returning anything,so I changed it to a sub.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question