如何正确写入和保存新的 Excel 文件?

How to properly write and save a new Excel file?

一个应用程序(一个 SCADA 程序)有一个每天触发的事件和 运行 一个编写 Excel 文件(每天一个)的脚本。该文件可以包含多个工作表。

关于Windows7,安装了Office 2007,可以写,但是不能保存,也不能退出Excel.Application

Dim objExcel as Object
Set objExcel = CreateObject("Excel.Application")
objExcel.WorkBooks.Add  'I think I shouldn't do this, but if not it doesn't work

Set sheet = objExcel.ActiveWorkBook.Worksheets.Add
'writing to the actual sheet...

objExcel.ActiveWorkBook.SaveAs path$
objExcel.Workbooks.Close
objExcel.Quit   

如果我 运行 手动脚本(从脚本编辑器):

如果我尝试触发事件:

已解决。

Dim objExcel as Object
Dim sheet as Object
Set objExcel = CreateObject("Excel.Application")
objExcel.WorkBooks.Add

Set sheet = objExcel.ActiveWorkBook.Worksheets.Add
'writing to the actual sheet...

path$ = "path/must/use/slash/insteed/of/backslash"

objExcel.ActiveWorkBook.SaveAs path$
objExcel.ActiveWorkBook.Close
objExcel.Quit  
Set objExcel = Nothing    

所以问题出在 path$ 而我忘记输入 Set objExcel = Nothing。就这样。