首页 文章

如何在UFT中使用密码保存文件

提问于
浏览
0

我正在使用UFT 12.5 . 在运行时,它打开excel和word . 然后它在两个文件中写入一些数据 . 之后,我想用新名称保存这两个文件,然后密码保护 . 我需要手动输入密码才能打开它 . 到目前为止,我已经编写了下面的代码,并在最后一行收到错误 .

Set ExcelObj = createobject("excel.application")
ExcelObj.Visible = true

Set ExcelFile = ExcelObj.Workbooks.Open (file)
Set ScripSheet = ExcelFile.Worksheets("Scripts")
ScripSheet.Cells(1,1) = "Passed"
ExcelFile.SaveAs mm1, "ttt"

请告知我如何使用UFT使用密码保存word和excel文件 .

谢谢 .

1 回答

  • 1

    您需要使用 SaveAs 方法传递正确的参数 . 查看this链接了解更多信息 .
    以下是您可以尝试的工作代码:

    file = "File path with file name"
    newfile = "File path with new file name"
    
    Set ExcelObj = createobject("excel.application")
    ExcelObj.Visible = true
    
    Set ExcelFile = ExcelObj.Workbooks.Open (file)
    Set ScripSheet = ExcelFile.Worksheets("Scripts")
    ScripSheet.Cells(1,1) = "Passed"
    ExcelFile.SaveAs newfile, , "test"
    ExcelFile.Close
    ExcelObj.Quit
    

    UPDATE
    根据OP的评论

    如果要使用 ReadOnly 保存文件,则必须以这种方式使用 WriteResPassword 参数:

    ExcelFile.SaveAs newfile, , , "test"
    

    请注意,我分别为FileFormat和Password提供了两个空参数 . 这样它将要求密码以写入模式打开文件,如果您不提供密码,文件将以ReadOnly模式打开 .

    检查我提到的链接 .

相关问题