嘿,我遇到了一个奇怪的问题。
我在我的VBA中执行这样的行:
Shell "path_to_my_bat\batname.bat"蝙蝠的内容也很简单:
cd c:\some_path\
copy *csv newfiles.txt它所做的只是将目录中的所有csvs合并到一个.txt中,我将在宏中进一步使用它。
问题是,由于我使用了一些vbs脚本,看来我已经改变了shell命令的工作方式。
"cd“命令似乎被跳过或被某种东西覆盖,bat的实际第二部分将在我的Workbook放在的目录中执行。幸运的是,我有一个.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位获得充分的管理访问等。
发布于 2015-04-07 20:35:43
如果当前目录位于D:驱动器上,那么cd c:\some_path\将为C:设置目录,但当前驱动器将保留在D:上的某个位置。您需要告诉cmd.exe实际更改您正在工作的驱动器。
只需将命令更改为
cd /d c:\some_path\或
pushd c:\some_path\https://stackoverflow.com/questions/29497066
复制相似问题