[英]How to save a file with password in UFT
我正在使用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文件。
謝謝。
您需要使用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
模式打開。
檢查我提到的鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.