Excel FileCopy:文件已存在-运行时错误';70';拒绝许可';

Excel FileCopy:文件已存在-运行时错误';70';拒绝许可';,excel,error-handling,file-copying,vba,Excel,Error Handling,File Copying,Vba,我正在尝试在这段代码上设置错误处理技术 Dim str1 As String Dim PlanDocTemplate As String Dim answer1 As String answer1 = MsgBox("Is this Workbook saved in the appropriate folder?", vbYesNo + vbQuestion) If answer1 = vbNo Then MsgBox ("Please save this Workbook in

我正在尝试在这段代码上设置错误处理技术

Dim str1 As String
Dim PlanDocTemplate As String
Dim answer1 As String

answer1 = MsgBox("Is this Workbook saved in the appropriate folder?", vbYesNo + vbQuestion)

If answer1 = vbNo Then
    MsgBox ("Please save this Workbook in the appropriate folder then run again.")
    Exit Sub
Else
    'do nothing
End If

str1 = "C:\user\desktop\document.docx"
PlanDocTemplate = Application.ActiveWorkbook.path & "\" & Range("A1").Value & ".docx"

Call FileCopy(str1, PlanDocTemplate)
现在,这可以正常工作,但是如果用户运行宏一次,然后再次运行宏而不删除/重命名从宏创建的文档,则会导致运行时错误70

想知道处理此错误的最佳方法是什么?我是否可以让vba询问用户是/否“是否要删除原始现有文档?”否将结束子文档,是将删除原始文档并继续我的其余代码

谢谢大家,
Rich

您可以按照任何方式捕获和处理错误,您建议的方式可能会很好。就我个人而言,我会在
FileCopy
sub中使用类似
If Dir(str1,vbDirectory)=“然后将该文件复制到msgbox中,或者在msgbox中查看该文件的处理方法。这应该首先防止抛出错误。如果出现问题,您仍然应该处理运行时错误70。嘿,泰勒,谢谢。最后我使用:如果Len(Dir(Application.ActiveWorkbook.Path&“\”&Range(“A1”).Value&“.docx”)=0,那么。。。其他的效果很好。谢谢你。我很高兴你能成功。仅供参考,使用Len(Dir())=0或Dir()=“”将实现相同的效果。