无法在qtp中保存webtable内容到excel文件

我的代码无法将数据保存到现有的Excel文件。 我可以从本地窗口看到,它是从一个webtable复制到excel工作表的数据,但不能保存的细节。请你纠正,如果我在这里错过任何错误

path = "D:\Demo\TestData\Shopping_Cart.xls" set xl= CreateObject("excel.application") xl.workbooks.open(path) set nsheet=xl.sheets.item(1) Set BrwsrCheckOut= Browser("name:=Checkout","title:=Checkout - Internet Explorer").page("title:=Checkout","name:=.*") Set DesPrdChcKOut = Description.Create DesPrdChcKOut("html tag").value = "TABLE" DesPrdChcKOut("column names").value = "Product Name;Model;Quantity;Price;Total" For IteratorRow = 1 To 2 'BrwsrCheckOut.WebTable(DesPrdChcKOut).RowCount Step 1 For IteratorCol = 1 To 3 'BrwsrCheckOut.WebTable(DesPrdChcKOut).ColumnCount(1) Step 1 val = BrwsrCheckOut.WebTable(DesPrdChcKOut).GetCellData(IteratorRow, IteratorCol) Next Next 'xl.Activeworkbook.saveAs "D:\Demo\TestData\Shopping_Cart.xls" xl.Activeworkbook.save nsheet.SaveAs("D:\Demo\TestData\Shopping_Cart.xls") xl.ActiveWorkbook.Close Set xl = nothing Set nsheet = nothing 

我认为这是因为您正在使用ActiveWorkbook方法。 当您使用活动工作簿方法时,必须非常小心,没有其他工作簿打开/活动状态。

为了避免所有这些,以下可能是更好的方法。

  path = "D:\Demo\TestData\Shopping_Cart.xls" Set oxl = CreateObject("Excel.Application") Set wbk = oxl.workbooks.open(path) sheetname = "Output" ' You can also refer the sheet by index Set sheet = wbk.sheets(sheetname) 'Logic to retreive the data from webtable for i=1 to number_of_rows ' update the data in excel next 'Save the workbook wbk.save wbk.close oxl.quit 

让我知道如果它的作品。