我在VBA中有一個Excel宏,我想將宏從其中執行的位置複製到另一個位置。VBA將自己複製到其他位置
我試圖像這樣
Call FileCopy(currentDir & "\" & Filename, _
otherDir & "\" & Filename)
但我得到一個Access restricted
例外,雖然我已經完全進入所涉及的所有目錄。是因爲我試圖「複製自己」嗎?這可能嗎?如果沒有,我可以建立一個解決方法嗎?
我在VBA中有一個Excel宏,我想將宏從其中執行的位置複製到另一個位置。VBA將自己複製到其他位置
我試圖像這樣
Call FileCopy(currentDir & "\" & Filename, _
otherDir & "\" & Filename)
但我得到一個Access restricted
例外,雖然我已經完全進入所涉及的所有目錄。是因爲我試圖「複製自己」嗎?這可能嗎?如果沒有,我可以建立一個解決方法嗎?
嘗試使用
ThisWorkbook.SaveCopyAs otherDir & "Test1"
或
ThisWorkbook.SaveAs otherDir & "Test2"
的ThisWorkbook是指包含正在運行的宏工作簿...
更新:這應該爲你工作,以創建一個文件夾...
Make確定你在Tools - > references下添加了「Microsoft Scripting Runtime」。
Dim fso As FileSystemObject
Set fso = New FileSystemObject
fso.CreateFolder ("C:\test\test2")
ThisWorkbook.SaveCopyAs "c:\test\test2\ttt.xlsm"
使用FileCopy沒有爲我工作,但使用FileFile從FileSystemObject似乎工作。
首先,你需要添加一個引用(菜單:工具 - >引用)對Microsoft腳本運行時,然後使用FileSystemObject
Dim fso As FileSystemObject
Set fso = New FileSystemObject
fso.CopyFile currentDir & "\" & Filename, otherDir & "\" & Filename, True
''the last parameter decides weather or not you want to overwrite existing files
Set fso = Nothing
替代方案:在新的目的地保存文檔並保存它背部。
ThisWorkbook.SaveAs otherDir & "\" & Filename
ThisWorkbook.SaveAs currentDir & "\" & Filename
我試過第二種方法,但現在我得到「訪問鎖定的目錄」xx「不可能」 - 是因爲我的宏創建這個目錄本身?也許它是因爲這個而被鎖定的?我創建文件夾後如何設置文件夾? – 2011-04-21 14:06:40
如何創建文件夾...請向我們顯示您的代碼... – 2011-04-21 14:11:07
SaveAS需要'otherDir'才能正常工作。 – marg 2011-04-21 14:13:44
您好,也有可能您無法讀取源文件。 – reporter 2011-04-21 14:04:49
如何創建文件夾...請告訴我們您的代碼... – 2011-04-21 14:11:26