VBA: removing password from word documents with VBA does not work

Anonymous
2020-05-04T12:33:41+00:00

I have a lot of word documents that are protected with a password. I want to remove the password from all those files with a vba script.

I found on this site a piece of code to do this. See the code below. The code runs without compilation error.

The files are saved in the C:\Test\Processed directory, however the password protection is still in it.

I am a beginner with VBA, so maybe I overlook something.

Dim myFile As String

Dim PathToUse As String

Dim myDoc As Document

Dim strPassword As String

strPassword = InputBox("Enter the password to open the documents", "Password")

PathToUse = "C:\Test"

MkDir "C:\Test\Processed"

SavePath = "C:\Test\Processed"

'Close all open documents before beginning

Documents.Close SaveChanges:=wdPromptToSaveChanges

myFile = Dir$(PathToUse & "*.doc")

While myFile <> ""

    'Open document

    Set myDoc = Documents.Open(filename:=PathToUse & myFile, PasswordDocument:=strPassword)

    myDoc.SaveAs SavePath & myFile, Password = ""

    myDoc.Close

    'Next file in folder

    myFile = Dir$()

Wend

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323.5K Reputation points MVP Volunteer Moderator
    2020-05-05T02:29:08+00:00

    Assuming that the documents are Encrypted, Use

    While myFile <> ""

        'Open document

        Set myDoc = Documents.Open(filename:=PathToUse & myFile, PasswordDocument:=strPassword)

        myDoc.EncryptionProvider = ""    myDoc.SaveAs SavePath & myFile

        myDoc.Close

        'Next file in folder

        myFile = Dir$()

    Wend

    However, if the protection was applied by using the Restrict Editing facility, then use

    While myFile <> ""

        'Open document

        Set myDoc = Documents.Open(filename:=PathToUse & myFile, PasswordDocument:=strPassword)

        myDoc.ProtectionType = wdNoProtection    myDoc.SaveAs SavePath & myFile

        myDoc.Close

        'Next file in folder

        myFile = Dir$()

    Wend

    Was this answer helpful?

    4 people found this answer helpful.
    0 comments No comments
  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Charles Kenyon 172K Reputation points Volunteer Moderator
    2020-05-05T03:15:07+00:00

    I assume that Doug's response was correct.

    Thought you might like to know that Graham Mayor and Greg Maxey have a Batch Processor Add-In that has removing or adding a password as one of its options.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323.5K Reputation points MVP Volunteer Moderator
    2020-05-05T07:59:43+00:00

    thanks, Paul

    I have edited my response, but I blame the forum software for the linebreaks going AWOL.

    Was this answer helpful?

    0 comments No comments
  5. Paul Edstein 82,496 Reputation points Volunteer Moderator
    2020-05-05T03:24:44+00:00

    Hi Doug,

    I think your code has some line wrap issues...

    Was this answer helpful?

    0 comments No comments