Can't protect word document before closing

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,340
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to open a word document, populate it, password protect it then save it but I can't get it to work. Everything works fine except the saved document isn't password protected. This is what I have so far:

VBA Code:
'Open template
    Set WordApp = CreateObject("Word.Application")
    WordApp.Visible = True
    WordApp.Documents.Add (ThisWorkbook.Path & "\System Files\Blank.docx")
    WordApp.Application.Activate
    Set WordDoc = WordApp.ActiveDocument

    'Populate document code

    FilePath = vbNullString
    Do While FilePath = vbNullString
        FilePath = Application.GetSaveAsFilename(InitialFileName:="Status Record", FileFilter:="Word Files (*.docx), *.docx", Title:="Select Folder to save")
        If FilePath = "False" Then

            Select Case MsgBox("Do you want to cancel creating this record?", vbYesNo Or vbExclamation Or vbDefaultButton1, "File not created")
                Case vbYes
    'Tidy up
                    WordDoc.Close SaveChanges:=False
                    WordApp.Visible = True
                    If Not WordApp Is Nothing Then
                        WordApp.Quit
                    End If
                    Exit Sub

                Case vbNo
                    FilePath = vbNullString
            End Select
        End If
    Loop

    If FilePath <> "False" Then
        Application.ScreenUpdating = False

        WordDoc.Protect Type:=wdAllowOnlyReading, NoReset:=True, Password:="password"

        WordDoc.SaveAs FilePath
        WordDoc.Close SaveChanges:=False
        WordApp.Visible = True

    End If

    'Tidy up
    If Not WordApp Is Nothing Then
        WordApp.Quit
    End If

Can anyone spot the issue?
 

Some videos you may like

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
First, do you have a reference set to the word object library? Second, what sort of password are you expecting?
 

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,340
Office Version
  1. 2016
Platform
  1. Windows
Hi, yes, there is a reference to the word object. I was hoping to set the password to a numeric value but when that didn't work I tried just the text 'password', but that doesn't work either.

To be fair I could set the password to anything at all, as long as it works.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
You didn't answer my other question - what kind of password are you expecting? If you want a password to open the document, rather than a password to unprotect and edit after it's opened, then you are using the wrong code.
 

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,340
Office Version
  1. 2016
Platform
  1. Windows

ADVERTISEMENT

Ahh, sorry, I misunderstood - a password to prevent editing, opening should be unrestricted.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Your code works for me as long as the reference is set, or you use the literal value for wdAllowOnlyReading (3)
 

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,340
Office Version
  1. 2016
Platform
  1. Windows
Thanks Rory - I double checked the reference and it wasn't set, not sure why because it certainly was but anyway, issue resolved now.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,067
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Glad it's working. :)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,583
Messages
5,597,021
Members
414,116
Latest member
sfullnet

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top