0

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

2

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:

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
  • oh... and what [@Erik](https://stackoverflow.com/questions/48947714/copyfile-ini-to-txt-permission-denied/48948824#comment84903350_48947714) said too.... – ashleedawg Feb 23 '18 at 13:21
  • Thank you! I didn't know, that I have to close oFile, too. – mark_da_rat Feb 23 '18 at 13:44
  • It's a very common mistake. ...and if Excel ever crashes on you while running VBA, check for "un-closed" objects first -- I have yet to see or hear of Excel crashing (while running VBA), that isn't somehow related to a ***[memory leak](https://en.wikipedia.org/wiki/Memory_leak)*** caused by mishandling objects. – ashleedawg Feb 23 '18 at 13:58
  • Oh, and here's an excellent reference on [Copying & Moving Files with VBA](https://www.rondebruin.nl/win/s3/win026.htm). – ashleedawg Feb 23 '18 at 14:00
  • Note that your entire sub can be minimized to one line: `CreateObject("Scripting.FileSystemObject").copyFile "c:\sourcePath\sourceFile.ini", "c:\destinationPath\destFile.txt"`, without affecting functionality. Since `fso` is never declared, it doesn't need to be destroyed. – Erik A Feb 23 '18 at 14:08
  • >! ***Shh!** That's second-week curriculum!* :) – ashleedawg Feb 23 '18 at 14:41