Forum Discussion

Nikhil1615's avatar
Nikhil1615
Copper Contributor
Jan 06, 2024

Excel file with Macros enabled cannot Genarate JSON with error bad file name

When I tried to edit my Macro- enabled file to Generate JSON. I am getting this error Run time 52 bad file name or number. Please help me fully address this issue in order for me to successfully save this Macro enabled file.

 

1 Reply

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    Nikhil1615 

    The "Run-time error '52': Bad file name or number" is a common error in VBA (Visual Basic for Applications) that typically occurs when there is an issue with the file name you are trying to use. This error can happen when attempting to save or open a file using VBA code.

    Here are some steps to help you address this issue:

    1. Check File Path and Name:

    • Ensure that the file path and name you are using in your VBA code are correct.
    • Check for any invalid characters, spaces, or special characters in the file name that might cause issues.

    2. Avoid Reserved Characters:

    • Ensure that the file name does not contain reserved characters such as \ / : * ? " < > |.

    3. Verify File Existence:

    • Check if the file already exists at the specified location. If it does, ensure that it is not open in another program, as that could prevent the code from overwriting it.

    4. Use Full File Paths:

    • Instead of relying on the current directory, provide the full file path to avoid any ambiguity. Use something like ThisWorkbook.Path & "\YourFileName.xlsx".

    5. Escape Backslashes:

    • If you are using backslashes in your file path, make sure to escape them with an additional backslash. For example: C:\Folder\YourFile.xlsx should be written as C:\\Folder\\YourFile.xlsx in VBA.

    6. Debug the Code:

    • Use the VBA debugger to step through your code and identify the line where the error occurs. This can help you pinpoint the issue.

    Here's a basic example of how you might save a file in VBA:

    Vba code is untested, please backup your file before you use this code.

     

    Sub SaveFile()
        Dim FilePath As String
        Dim FileName As String
        
        ' Set the file path and name
        FilePath = ThisWorkbook.Path
        FileName = "YourFileName.xlsx"
        
        ' Save the file
        ThisWorkbook.SaveAs FilePath & "\" & FileName
    End Sub

     

     

    Make sure you adapt the file path and name to your specific requirements.

    If the issue persists, please provide the relevant part of your VBA code that is causing the error, and I can help you identify and address the specific problem.

    The text and steps were edited with the help of AI.

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

    Was the answer useful? Mark as best response and Like it!

    This will help all forum participants.

Resources