我有一个简单的vbscript,我想演示,它将在运行脚本的任何目录中最佳地创建一个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函数用于SaveAs。有什么提示吗?
答案 0 :(得分:0)
使用以下代码相应地使用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"