0

Is there a way to make a paragraph in a Microsoft Word 2003 document readonly using VBA?

3 Answers 3

1

I think you can only do it by selecting the paragraphs that will not be protected.

Below is a sample macro that is selecting a piece of text, enabling it for editing, and then locking the rest of the document. You can either use IRM or password protection, the macro below is using the latter. You should replace the method of selection below by something more elegant

Selection.MoveLeft Unit:=wdCharacter, Count:=11, Extend:=wdExtend 
Selection.Editors.Add wdEditorEveryone
ActiveDocument.protect Password:="password", NoReset:=False, Type:= _
wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False
0

You might be able to do it by throwing the to-be-protected text into a text form and protecting that. Obviously a little gross.

0

The answer above will leave a yellow highlighting of the editable area.

One alternative is adding this AutoOpen code http://answers.microsoft.com/en-us/office/forum/office_2010-word/remove-highlighting-in-editable-areas-of-protected/bfe22585-c5d3-4c19-997f-092fc4aaaa7a

Sub AutoOpen()
   ActiveWindow.View.ShadeEditableRanges = False
End Sub

In Word 2007(tested), a more direct way is to create a rich text content control and set the properties. This will easily make a paragraph read-only without making any highlights.

Sub LockContent()

Dim objCC As ContentControl
Set objCC = ActiveDocument.ContentControls.Add(Type:=wdContentControlRichText)

With objCC
    .Range.Text = "123123" 'Your text
    .LockContentControl = True
    .LockContents = True
End With

End Sub

More ContentControl.LockContentControl Property on https://msdn.microsoft.com/en-us/library/office/ff835775.aspx

Hope this help anyone having the same headache! :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.