繁体   English   中英

“Kill”/“FileCopy”导致权限被拒绝

[英]“Kill” / “FileCopy” leads to permission denied

我的 Access VBA 宏出现问题,由于某种原因,在那些行中出现错误“权限被拒绝 [运行时错误 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

直接“指出”的是“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 内存中的某个位置打开,因此无法删除该文件。

感谢所有贡献的家伙。 你一如既往的棒!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM