Heyho,我遇到了一個奇怪的問題。VBA執行一個.bat文件,但覆蓋.bat文件中的一個cd
我在VBA執行這樣一行:
Shell "path_to_my_bat\batname.bat"
蝙蝠的內容很簡單,以及:
cd c:\some_path\
copy *csv newfiles.txt
它的作用是簡單地採取所有的CSV目錄並將它們合併爲一個.txt文件,我將其用在宏的更下面。
問題是,因爲我玩過一些vbs腳本,所以似乎我已經改變了shell命令的工作方式。
「cd」命令似乎被跳過或被某些東西覆蓋,蝙蝠的實際第二部分在放入我的工作簿的目錄中執行。 幸運的是,我有一個.csv躺在那裏,否則我會沒有注意到...
蝙蝠本身工作正常,如果我不從VBA運行它。
的vbs腳本我看起來像這樣打(它應該打開一個文件,並在那裏執行宏):
我想我重寫了某種默認設置的,我不應該有修修補補... 除此之外,我沒有做任何事情,我知道要改變一些事情。這個宏今天早上工作正常,但現在由於蝙蝠錯誤,它是沒用的。
' Create a WshShell to get the current directory
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
' Create an Excel instance
Dim myExcelWorker
Set myExcelWorker = CreateObject("Excel.Application")
' Disable Excel UI elements
myExcelWorker.DisplayAlerts = False
myExcelWorker.AskToUpdateLinks = False
myExcelWorker.AlertBeforeOverwriting = False
myExcelWorker.FeatureInstall = msoFeatureInstallNone
' Tell Excel what the current working directory is
' (otherwise it can't find the files)
Dim strSaveDefaultPath
Dim strPath
strSaveDefaultPath = myExcelWorker.DefaultFilePath
strPath = WshShell.CurrentDirectory
myExcelWorker.DefaultFilePath = strPath
' Open the Workbook specified on the command-line
Dim oWorkBook
Dim strWorkerWB
strWorkerWB = strPath & "\YourWorkbook.xls"
Set oWorkBook = myExcelWorker.Workbooks.Open(strWorkerWB)
' Build the macro name with the full path to the workbook
Dim strMacroName
strMacroName = "'" & strPath & "\YourWorkbook" &
"!Sheet1.YourMacro"
on error resume next
' Run the calculation macro
myExcelWorker.Run strMacroName
if err.number <> 0 Then
' Error occurred - just close it down.
End If
err.clear
on error goto 0
oWorkBook.Save
myExcelWorker.DefaultFilePath = strSaveDefaultPath
' Clean up and shut down
Set oWorkBook = Nothing
' Don’t Quit() Excel if there are other Excel instances
' running, Quit() will
shut those down also
if myExcelWorker.Workbooks.Count = 0 Then
myExcelWorker.Quit
End If
Set myExcelWorker = Nothing
Set WshShell = Nothing
如果有幫助,我運行Windows 8.1的64位與Excel 2010 64位 得到了完整的管理存取權限等
你是從不同的驅動器上運行? – SLaks