使用 VBA 将文本段落设为只读 (Microsoft Word 2003)

发布于 2024-07-11 10:13:29 字数 52 浏览 5 评论 0原文

有没有办法使用 VBA 将 Microsoft Word 2003 文档中的段落设为只读?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回眸一笑 2024-07-18 10:13:29

我认为您只能通过选择不受保护的段落来做到这一点。

下面是一个示例宏,它选择一段文本,使其能够编辑,然后锁定文档的其余部分。 您可以使用 IRM 或密码保护,下面的宏使用后者。 您应该用更优雅的方法替换下面的选择方法

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

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
叹倦 2024-07-18 10:13:29

您可以通过将要保护的文本放入文本表单并对其进行保护来做到这一点。 显然有点恶心。

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.

沙与沫 2024-07-18 10:13:29

上面的答案将留下黄色突出显示的可编辑区域。

一种替代方法是添加此 AutoOpen 代码

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

在Word 2007(已测试)中,更直接的方法是创建富文本内容控件并设置属性。 这将很容易使段落只读而不做任何突出显示。

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

更多 ContentControl.LockContentControl 属性
https://msdn.microsoft.com/en-us/library/office /ff835775.aspx

希望这可以帮助有同样头痛的人! :D

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文