sharky12345
Well-known Member
- Joined
- Aug 5, 2010
- Messages
- 3,422
- Office Version
- 2016
- Platform
- 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:
Can anyone spot the issue?
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?