Run-time error '75' : Path/File access error

akshay0880

New Member
Joined
Jul 11, 2021
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello.

I am trying the below code to generate a text file (with the extension .RG) but when running it says Path/File access error.

Please note that i am trying to save the generated text file on the same location as the workbook is saved.
I am not sure how to specify the path (if needed). Can someone please help me out?

Sub Card_RG()
Dim FileName As String
FileName = Range("Q1") & "CARD.RG"

Open FileName For Output As #1
Print #1, "B" & Range("Q1") & " YD"
Close #1
MsgBox ("CARD.RG Created")
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Kaper

Active Member
Joined
Mar 14, 2014
Messages
294
Office Version
  1. 365
Platform
  1. Windows
And what is the content of Q1?

According to Open statement (VBA)

the argument called filepath, so your
VBA Code:
Range("Q1") & "CARD.RG"

may (but
VBA Code:
not have to) include pathname

Try first
VBA Code:
FileName = activeworkbook.path & "\" & "CARD.RG"

and if it works - check what is wrong with Q1 (may be it does not contain trailing backslash?)
 
Upvote 0

akshay0880

New Member
Joined
Jul 11, 2021
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi, Thanks for replying.
Q1 contains a number (that changes every time) and the file naming that I want to generate, will have to be something like this:
1234CARD.RG
I also tried this line as suggested FileName = ActiveWorkbook.path & Range("Q1") & "CARD.RG" but I get the error Bad file name.
 
Upvote 0

Kaper

Active Member
Joined
Mar 14, 2014
Messages
294
Office Version
  1. 365
Platform
  1. Windows
you probably missed a backslash:
VBA Code:
FileName = ActiveWorkbook.path & "\" & Range("Q1") & "CARD.RG"
 
Upvote 0

akshay0880

New Member
Joined
Jul 11, 2021
Messages
14
Office Version
  1. 365
Platform
  1. Windows
you probably missed a backslash:
VBA Code:
FileName = ActiveWorkbook.path & "\" & Range("Q1") & "CARD.RG"
Yes. Now i am getting a '2 error.
Says Bad file name or number. Attached screenshot
 
Upvote 0

akshay0880

New Member
Joined
Jul 11, 2021
Messages
14
Office Version
  1. 365
Platform
  1. Windows
attached
 

Attachments

  • 2022-09-19 17_58_03-Powerpay Implementation - Akshay Nutbursing.png
    2022-09-19 17_58_03-Powerpay Implementation - Akshay Nutbursing.png
    37.7 KB · Views: 5
Upvote 0

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
20,822
Office Version
  1. 365
Platform
  1. Windows
By any chance, does the path returned by ActiveWorkbook.path contain any unicode characters? If so, the Open statement is unable to handle unicode characters, and so you'll get the error...

VBA Code:
Run-time error '52':

Bad file name or number

In this case, try using the FileSystemObject object instead of the Open statement. However, if in fact the path doesn't contain unicode characters, try running your code again. This time, though, when the error occurs, click on Debug, and then enter the following line in the Immediate Window (Ctrl+G), and press ENTER...

VBA Code:
? FileName

What does it return?
 
Upvote 0

Forum statistics

Threads
1,186,647
Messages
5,958,973
Members
438,387
Latest member
faxeur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top