Can't protect word document before closing

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
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?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
First, do you have a reference set to the word object library? Second, what sort of password are you expecting?
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
Ahh, sorry, I misunderstood - a password to prevent editing, opening should be unrestricted.
 
Upvote 0
Your code works for me as long as the reference is set, or you use the literal value for wdAllowOnlyReading (3)
 
Upvote 0
Thanks Rory - I double checked the reference and it wasn't set, not sure why because it certainly was but anyway, issue resolved now.
 
Upvote 0

Forum statistics

Threads
1,223,646
Messages
6,173,531
Members
452,520
Latest member
Pingaware

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
Back
Top