r/excel•
InHaUse

VBA code to open protected Word doc with known password isn't working

unsolved

Hey guys,

This should be pretty simple but for whatever reason my code to open a Word document with a known password isn't working so any help would be appreciated. I'm using the code below from within an Excel file.

Code:

Dim MyWord as object

Dim MyDoc as document

Dim MyPass as string

MyPass = "something"

Set MyWord = createobject("Word.application")

Set MyDoc = MyWord.documents.open(FileName:=whatever, PasswordDocument:=MyPass)

The document opens, but it stays stuck at the window requesting the password and if I type it in manually it works and the rest of the code can continue.

Posted Apr 26, 2023, 9:31 PM UTC · edited

15
AutoModeratorMOD•

u/InHaUse - Your post was submitted successfully.

Failing to follow these steps may result in your post being removed without warning.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1
fanpages32•

This statement:

Set MyDoc = MyWord.documents.open(FileName:=whatever, PasswordDocument:="MyPass")

Try changing it to:

Set MyDoc = MyWord.documents.open(FileName:=whatever, PasswordDocument:=MyPass)

That is, remove the quotes from around MyPass.

1
InHaUseOP•

Sorry that's not it. I updated my original post to reflect the correct code. I know that it shouldn't have quotes.

1
fanpages32•

I suggest you change the opening post in your other thread (in r/VBA) too:

[ https://old.reddit.com/r/vba/comments/12zzlv3/word_vba_code_to_open_protected_word_doc_with/ ]


I would also advise copy'n'pasting the exact code, rather than re-typing it as you have done.

Thanks.

1
InHaUseOP•

Yep I did that thanks. I can't just copy it because it's on my work laptop and can't access Reddit from there unfortunately.

1