View Single Post
 
Old 02-16-2022, 04:52 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
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 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