VBA 执行 .bat 但覆盖 .bat 中的 cd

VBA executes a .bat but overwrites a cd in the .bat

嘿嘿,我 运行 遇到了一个 st运行ge 问题。

我在 VBA:

中执行了这样一行
Shell "path_to_my_bat\batname.bat"

bat的内容也很简单:

cd c:\some_path\
copy *csv newfiles.txt

它所做的只是将目录中的所有 csvs 合并到一个 .txt 中,我在宏中进一步使用它。

问题是,因为我玩过一些 vbs 脚本,所以我似乎对 shell 命令的工作方式做了一些更改。

"cd"命令好像被跳过了,或者被什么东西覆盖了,实际bat的第二部分是在我的Workbook所在的目录下执行的。 幸运的是我有一个 .csv 放在那里,否则我不会注意到...

如果我不 运行 从 VBA.

,蝙蝠本身就可以正常工作

我玩过的 vbs 脚本看起来像这样(它应该打开一个文件并在其中执行一个宏):

我想我覆盖了某种我不应该修改的默认设置... 除此之外,据我所知,我没有做任何改变的事情。宏今天早上还好好的,现在因为bat报错没用了

' 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 位 获得完整的管理员权限等

如果您的当前目录在 D: 驱动器上,则 cd c:\some_path\ 将为 C: 设置目录,但您当前的驱动器将保留在 D: 上的某个位置。您需要告诉 cmd.exe 实际更改您正在使用的驱动器。

只需将命令更改为

cd /d c:\some_path\

pushd c:\some_path\