Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-15-2022, 09:56 AM
dyb dyb is offline Unprotect sections in a word document using VBA Windows 11 Unprotect sections in a word document using VBA Office 2019
Novice
Unprotect sections in a word document using VBA
 
Join Date: Feb 2022
Posts: 7
dyb is on a distinguished road
Question 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.
Reply With Quote
  #2  
Old 02-15-2022, 10:06 PM
gmayor's Avatar
gmayor gmayor is offline Unprotect sections in a word document using VBA Windows 10 Unprotect sections in a word document using VBA Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 02-16-2022, 01:56 AM
dyb dyb is offline Unprotect sections in a word document using VBA Windows 11 Unprotect sections in a word document using VBA Office 2019
Novice
Unprotect sections in a word document using VBA
 
Join Date: Feb 2022
Posts: 7
dyb is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 02-16-2022, 04:52 AM
gmayor's Avatar
gmayor gmayor is offline Unprotect sections in a word document using VBA Windows 10 Unprotect sections in a word document using VBA Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
The following will write your userform image to a rich text content control
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
The following will protect all of your document except for the content controls listed - here "Title 1" and " Title 2".
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
Or if you simply want to protect sections - here sections 2 & 3 then
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
Reply With Quote
  #5  
Old 02-16-2022, 07:49 AM
dyb dyb is offline Unprotect sections in a word document using VBA Windows 11 Unprotect sections in a word document using VBA Office 2019
Novice
Unprotect sections in a word document using VBA
 
Join Date: Feb 2022
Posts: 7
dyb is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 02-17-2022, 01:43 AM
gmayor's Avatar
gmayor gmayor is offline Unprotect sections in a word document using VBA Windows 10 Unprotect sections in a word document using VBA Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
Reply



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
Unprotect sections in a word document using VBA 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

Other Forums: Access Forums

All times are GMT -7. The time now is 10:39 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft