简体   繁体   English

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

[英]How to save a file with password in UFT

I am using UFT 12.5. 我正在使用UFT 12.5。 During run time it opens excel and word. 在运行时,它将打开excel和word。 Then it writes some data in the both files. 然后,它在两个文件中写入一些数据。 After that, I would like to save both files with a new name and then password protected. 之后,我想用新名称保存两个文件,然后用密码保护。 I need to be able to enter password manually to open it. 我需要能够手动输入密码才能打开它。 So far, I have written the below code and I getting an error at the last line. 到目前为止,我已经编写了以下代码,最后一行出现错误。

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"

Please advise on how I can save word and excel files with a password using UFT. 请告知我如何使用UFT使用密码保存word和excel文件。

Thanks. 谢谢。

You need to pass correct parameters with SaveAs method. 您需要使用SaveAs方法传递正确的参数。 Check this link for more info. 检查this链接以获取更多信息。
Here is the working code that you can try: 这是您可以尝试的工作代码:

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 UPDATE
Per comments from OP 根据OP的评论

If you want to save file with ReadOnly , you have to use WriteResPassword parameter this way: 如果要使用ReadOnly保存文件,则必须通过以下方式使用WriteResPassword参数:

ExcelFile.SaveAs newfile, , , "test"

Note that I've two empty parameters for FileFormat and Password respectively. 请注意,我分别为FileFormatPassword有两个空参数。

This way it will ask for password to open the file in write mode and if you won't provide the password, file will be opened in ReadOnly mode. 这样,它将要求输入密码以写模式打开文件,如果您不提供密码,则文件将以ReadOnly模式打开。

Check the link that I've mentioned. 检查我提到的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM