Problems opening a txt file

Posted on 2003-03-07
Medium Priority
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
       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
   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  !!!

Question by:chrome2000
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 28

Accepted Solution

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
LVL 28

Expert Comment

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.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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
Course of the Month8 days, 20 hours left to enroll

764 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