Problems opening a txt file

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  !!!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.