#1
|
|||
|
|||
Unprotect sections in a word document using VBA
I have a word document template which is protected and want to unprotect some of the individual sections in the document so that the text can be edited. I found an old post on VBA express by Greg Maxey that almost does what I want but it is for unprotecting the sections to allow Form Field data entry.
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oFld As FormField ActiveDocument.Unprotect ActiveDocument.Protect wdAllowOnlyReading For Each oFld In ActiveDocument.Sections(2).Range.FormFields oFld.Range.Editors.Add wdEditorEveryone Next lbl_Exit: Exit Sub End Sub How would I adapt this to work for text in the sections as this is not a form so does not use form fields. Any help would be greatly appreciated. |
#2
|
||||
|
||||
If it is not protected for forms, how is the document protected?
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
The template is protected as read only and has bookmarks that populate the data required from a userform. The userform code unprotects the document to allow the userform data to be input but I need it to then protect it but leave two different sections available to edit.
|
#4
|
||||
|
||||
Frankly I would use content controls for this. You could swap the bookmarks for content controls ( Insert Content Control Add-In will do this) and leave the parts you want editable marked as editable. With regard to your other query you can insert a picture into a picture content control or a rich text content control and you can choose to lock the controls filled from your userform that you don't want editing.
The following code will write a text string to a content control. Code:
Public Sub FillCC(strCCTitle As String, strValue As String, bLock As Boolean) 'Graham Mayor - https://www.gmayor.com - Last updated - 03 Sep 2021 Dim oCC As ContentControl On Error GoTo lbl_Exit For Each oCC In ActiveDocument.ContentControls If oCC.Title = strCCTitle Then oCC.LockContents = False oCC.Range.Text = strValue oCC.LockContentControl = True If bLock = True Then oCC.LockContents = True Exit For End If Next oCC lbl_Exit: Set oCC = Nothing Exit Sub End Sub Code:
Private Sub UserFormImageToCC(strCCTitle As String, oImage As Object, bLock As Boolean) 'Graham Mayor - https://www.gmayor.com - Last updated - 16 Feb 2022 Dim oCC As ContentControl Dim oRng As Range Dim TempFile As String Dim FSO As Object Set FSO = CreateObject("Scripting.FileSystemObject") TempFile = Replace(FSO.GetTempName, "tmp", "bmp") SavePicture oImage, TempFile With ActiveDocument On Error GoTo lbl_Exit For Each oCC In ActiveDocument.ContentControls If oCC.Title = strCCTitle Then oCC.LockContents = False oCC.Range.Text = "" Set oRng = oCC.Range oRng.InlineShapes.AddPicture _ FileName:=TempFile, LinkToFile:=False, _ SaveWithDocument:=True oCC.LockContents = bLock Exit For End If Next oCC End With Kill TempFile lbl_Exit: Set FSO = Nothing Set oRng = Nothing Exit Sub End Sub Code:
Sub MakeReadOnly() Dim oCC As ContentControl Dim oRng As Range Const sPassword As String = "" If Not ActiveDocument.ProtectionType = wdNoProtection Then ActiveDocument.Unprotect Password:=sPassword End If For Each oCC In ActiveDocument.ContentControls Select Case oCC.Title Case Is = "Title 1", "Title 2" oCC.LockContents = False oCC.Range.Text = "" Set oRng = oCC.Range oRng.Editors.Add (wdEditorEveryone) Case Else End Select Next oCC ActiveDocument.Protect Type:=wdAllowOnlyReading, NoReset:=True, Password:=sPassword lbl_Exit: Set oCC = Nothing Set oRng = Nothing Exit Sub End Sub Code:
Sub MakeReadOnly2() Dim oSection As Section Dim oRng As Range Const sPassword As String = "" If Not ActiveDocument.ProtectionType = wdNoProtection Then ActiveDocument.Unprotect Password:=sPassword End If For Each oSection In ActiveDocument.Sections Select Case oSection.Index Case Is = "2", "3" Set oRng = oSection.Range oRng.Editors.Add (wdEditorEveryone) Case Else End Select Next oSection ActiveDocument.Protect Type:=wdAllowOnlyReading, NoReset:=True, Password:=sPassword lbl_Exit: Set oSection = Nothing Set oRng = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
Hi Graham
Thank you for those - you have been incredibly helpful and I am most appreciative. I have used the protect sections option as using content controls wouldn't work for this template. It is a legal contract for training where we do not want users to have the ability to add free text but there are a couple of clauses that may need to be deleted/amended if not applicable to a specific contract so just leaving those clauses unprotected was the best option for us. Many thanks for your help. |
#6
|
||||
|
||||
Users cannot add text to content controls that have their contents locked.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Word VBA. How best to insert sections of other documents into a main document. | mc1903 | Word VBA | 5 | 10-11-2021 01:18 AM |
Multiple approvers for different sections in a Word document | Tombi Voice | Word | 2 | 10-19-2020 05:42 PM |
Macro to Unprotect password protected document | pooklet | Word | 2 | 12-08-2014 01:32 AM |
Printing Sections of a Word Document | Andy Jenkinson | Word | 4 | 02-12-2010 02:53 AM |
Unprotect header in Word | Fran | Word | 0 | 12-23-2008 03:42 AM |