在VBA

2016-05-31 38 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