无法将 webtable 内容保存到 qtp 中的 excel 文件
Cannot save webtable contents to excel file in qtp
我的代码无法将数据保存到现有 excel 文件。我可以从本地 window 看到它正在将数据从 webtable 复制到 excel sheet 但无法保存 details.Could 如果我遗漏任何错误请更正这里
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
如果有效请告诉我。
我的代码无法将数据保存到现有 excel 文件。我可以从本地 window 看到它正在将数据从 webtable 复制到 excel sheet 但无法保存 details.Could 如果我遗漏任何错误请更正这里
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
如果有效请告诉我。