名为" XX \ Data \ AccNum.xls'的文件已存在于此位置。你想替换它吗?

时间:2015-09-24 09:36:00

标签: qtp hp-uft

有人可以在这里说清楚。以下代码绝对有效,但它一直困扰着我:

  

名为" XX \ Data \ AccNum.xls"的文件已存在于此位置。你想替换它吗?

适用于所有行。

多次,我必须单击“是”以执行其余代码。我已尝试displayAlert=Falseapplicationalerts=false。没运气。

For Iterator = 1 To AccNoRow Step 1

    Set AccNoTB=browser("title:=.*").page("title:=.*").webtable("column names:=;Account No;Account Name;Billing City;Website;Phone;Assigned To;Action","cols:=8")

    'open excel & write to it

    Set Fso=createobject("Excel.application")
    Set file =fso.workbooks.open("XX\Data\AccNum.xls")
    file.worksheets("sheet1").cells(Iterator,3).value=AccNoTB.GetCellData(Iterator,3)
    fso.ActiveWorkbook.saveAs "XX\Data\AccNum.xls"
    fso.quit
Next

Set fso=nothing
Set file=nothing

1 个答案:

答案 0 :(得分:1)

您“认为”的问题的解决方案是使用ActiveWorkbook.save而不是ActiveWorkbook.saveas,但您的代码的真正问题是,您为每一行创建一个新的Excel对象,这绝对没有必要。只需创建对象ONCE并在添加所有行后将其保存为ONCE:为您节省大量开销,加快代码速度,并且只是正确的方法。

'open excel & write to it

Set Fso=createobject("Excel.application")
Set file =fso.workbooks.open("XX\Data\AccNum.xls")

For Iterator = 1 To AccNoRow Step 1
    Set AccNoTB=browser("title:=.*").page("title:=.*").webtable("column names:=;Account No;Account Name;Billing City;Website;Phone;Assigned To;Action","cols:=8")
    file.worksheets("sheet1").cells(Iterator,3).value=AccNoTB.GetCellData(Iterator,3)
Next

fso.ActiveWorkbook.save
fso.quit

Set fso=nothing
Set file=nothing