"Kill" / "FileCopy" 导致权限被拒绝

"Kill" / "FileCopy" leads to permission denied

我的 Access VBA 宏出现问题,由于某种原因,在那些行上出现错误“权限被拒绝 [运行-time error 70]:

sOutputFile = CurrentProject.Path & "\Output Files\Recon\" & sDate & " " & sClient & " Cash Recon.xlsx"
sTemplateFile = CurrentProject.Path & "\Temp Files\Template_Listed.xlsx"
If Dir(sOutputFile) <> "" Then Kill sOutputFile
FileCopy sTemplateFile, sOutputFile

直接得到 "pointed out" 的是 "Kill sOutputFile" 短语。

值得一提的是,我没有打开任何文件,我可以完全访问目录,不久前(在声明 sOutputFile 和 sTemplateFile 之前)它们都被清除了。

非常感谢任何帮助,如果需要,我愿意分享更多我的代码。

编辑:另外,宏有时会转到下一行并在 FileCopy 处中断。

我认为逻辑不太对,请尝试以下(FileCopy 应该在 If...End If 内):

sOutputFile = CurrentProject.Path & "\Output Files\Recon\" & sDate & " " & 
sClient & " Cash Recon.xlsx"
sTemplateFile = CurrentProject.Path & "\Temp Files\Template_Listed.xlsx"
If FileExists(sOutputFile) Then 
  Kill sOutputFile
  FileCopy sTemplateFile, sOutputFile
End If

这是 FileExists 函数:

Public Function FileExists_1(sFileName As String) As Boolean
  Dim obj_fso As Object

  Set obj_fso = CreateObject("Scripting.FileSystemObject")
  FileExists_1 = obj_fso.fileExists(sFileName)

  Set obj_fso = Nothing
End Function

我的问题是什么,我以前使用过宏,但完整的 运行 失败了。经过几次调整(与我在此处发布的代码无关)后再次尝试 运行 后,出现了上述问题。都是因为我的 excel 内存中某处仍然打开着文件,因此无法删除文件。

感谢大家的贡献。你一如既往的棒棒哒!