在VBA

2016-05-31 40 views
1

打開一個密碼保護的Word文檔,我需要打開VBA 密碼保護的Word文檔時它被要求輸入密碼,如何通過代碼打開 代碼:在VBA

Dim DPDoc 
Dim DPApp 
Dim DPPath 
DPPath = "C:\MyFolder\PwdProtectdFile.docx" 
Set DPApp = CreateObject("word.Application") 
' It is asking for the password here 
DPDoc = DPApp.Documents.Open(DPPath) 

回答

3

只需添加一個參數:

Dim YourOwnPassword As String 
YourOwnPassword = "TestPWD" 
DPDoc = DPApp.Documents.Open(DPPath, PasswordDocument:=YourOwnPassword) 

源:https://msdn.microsoft.com/en-en/library/office/ff835182.aspx

+0

我得到運行時錯誤 '448' 「命名找不到參數」 因此,我更改爲 DPDoc = DPApp.Documents.Open(DPPath,Password = YourOwnPassword) 現在它仍在提示輸入密碼。似乎沒有考慮到這一點。任何人都可以請幫忙 –

+0

@udhayakumar:我的不好,因爲它似乎應該是'PasswordDocument'而不僅僅是'Password'!查看編輯 – R3uK