使用VBScript将Excel工作簿保存在当前目录中

我有一个简单的vbscript,我想演示,它将最佳地创build一个Excel脚本运行的任何目录中的工作簿。 这是我迄今为止。

Set objExcel = CreateObject("Excel.Application") objExcel.Workbooks.Add Sheet = 1 Set objSheet = objExcel.ActiveWorkbook.Worksheets(Sheet) objSheet.Name = "EXAMPLE" strExcelPath = "C:\Users\EXAMPLE\EXAMPLE.xlsx" (data values) objExcel.ActiveWorkbook.SaveAs strExcelPath objExcel.ActiveWorkbook.Close objExcel.Application.Quit Set objSheet = Nothing Set objExcel = Nothing WScript.Echo "Done." 

我不能完全弄清楚如何将CurDir函数join到SaveAs中。 有小费吗?

使用下面的代码适当地使用VBScript获取当前目录。

 dim fso set fso = CreateObject("Scripting.FileSystemObject") dim CurrentDirectory CurrentDirectory = fso.GetAbsolutePathName(".") strExcelPath = fso.BuildPath(CurrentDirectory, "Example.xlsx") 

或者也可以使用VBScript中的WScript shell的CurrentDirectory属性。

 Dim WshShell, strCurDir Set WshShell = CreateObject("WScript.Shell") strCurDir = WshShell.CurrentDirectory strExcelPath = strCurDir & "\Example.xlsx"