Search code examples
vbams-accesspermission-deniedfile-copying

copyfile ini to txt: permission-denied


I just want to copy the Content of a ini-File into a txt-file. But it tells me, that permission is denied.

  • The source file is closed
  • the Ini-file "Aly_complete.ini" was previously executed in the code via "java -jar"
  • As you see, I already tried another file, which wasn't used by the code before

Here is the code

Sub Kopieren_Ini(strPathQuelle As String, strPathErg As String)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object    
Dim Quelle As String
Dim Ziel As String

If Sheets(1).TxtBoxIni.Text <> "" Then
    Quelle = Sheets(1).TxtBoxIni.Text
Else
    Quelle = strPathQuelle & "Aly_MitDatum.ini"
    'Quelle = strPathQuelle & "Aly_complete.ini"
End If

Set oFile = fso.CreateTextFile(strPathErg & "\" & "Config_Test.txt")
Ziel = strPathErg & "\" & "Config_Test.txt"

FileSystem.FileCopy Quelle, Ziel

Thanks in advance for your help


Solution

  • Sounds like the .ini is being used by another application or process. What else is running? Does this still occur after you reboot? ( Source: my comment ☺)

    Your code is incomplete (it doesn't End) so I can't say for sure, but I bet your issue is same common mistake that [imho] is the culprit in almost every complaint of Excel crashes caused by VBA code...

    It's just like parenta are always telling their children:

    "IF YOU OPEN IT, CLOSE IT!"

    The file is Open (and locked and taking up memory) until you .Close it.

    Objects that are opened need to be closed & cleared.

    Try adding these 3 lines to the end of your code (or where ever you're finished using the objects):

    oFile.Close
    Set oFile = Nothing
    Set fso = Nothing
    

    ...then save your work, reboot, and try it again.


    More Information:


    EDIT: "Copy & Rename"

    If you simply need to copy a file (and rename the copy at the same time), use this:

    Option Explicit
    
    Sub copyFile()
        Dim fso As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        fso.copyFile "c:\sourcePath\sourceFile.ini", "c:\destinationPath\destFile.txt"
        Set fso = Nothing
    End Sub
    

    More More Information: