我的代码无法将数据保存到现有的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
答案 0 :(得分:1)
我认为这是因为您使用的是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
让我知道它是否有效。