将文件 ini 复制到 txt:权限被拒绝

copyfile ini to txt: permission-denied

我只想将 ini 文件的内容复制到 txt 文件中。但它告诉我,权限被拒绝了。

这是代码

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

在此先感谢您的帮助

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

你的代码不完整(它没有 End)所以我不能肯定地说,但我敢打赌你的问题是同样的常见错误 [imho] 是Excel 崩溃 几乎所有投诉的罪魁祸首都是由 VBA 代码引起的...

就像父母总是告诉他们children:

在您 .Close 之前文件一直处于打开状态(并锁定并占用内存)。

Objects 已打开的需要关闭并清除。

尝试将这 3 行添加到代码的末尾(或者您使用 objects 完成的地方):

oFile.Close
Set oFile = Nothing
Set fso = Nothing

...然后保存您的工作,重新启动,然后重试。


更多信息:


编辑:"Copy & Rename"

如果您只需要复制文件(同时重命名副本),请使用:

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

更多更多信息: