从QTP删除Excel文件

时间:2013-01-15 19:41:47

标签: qtp

我在QTP中有以下代码:

Dim objexcel
Set objexcel = createobject("Excel.application")

objexcel.Visible = True

objexcel.Workbooks.Add
objexcel.Cells(1,1).Value =  "ABCDE"
objexcel.ActiveWorkBook.SaveAs("J:\Test.xls")

如何关闭文件并将其删除,我尝试了很多方法,但最终我遇到了对象错误。

感谢advence

1 个答案:

答案 0 :(得分:1)

您需要创建类Filesystemobject的对象以删除任何文件,因此代码应该是这样的

Dim objExcel,objFSO
Set objExcel = createobject("Excel.application")
Set objFSO = CreateObject("Scripting.Filesystemobject")
objExcel.Visible = True
objExcel.Workbooks.Add
objExcel.Cells(1,1).Value =  "ABCDE"
objExcel.ActiveWorkBook.SaveAs("J:\Test.xls")
objExcel.Workbooks.Close
objExcel.Quit
set objExcel = nothing
objFSO.DeleteFile("J:\Test.xls")
Set objFSO =  nothing