Powerpoint VBA - 防止形状文本被编辑

Powerpoint VBA - prevent shape text from being edited

我有以下代码创建一个 RoundedRectangle 文本 "Placeholder":

Public Sub CreateShape(currentSlide As Long, boxName As String)

    Dim oShape As Shape
    Set oShape = ActivePresentation.Slides(currentSlide).Shapes.AddShape(msoShapeRoundedRectangle, 640, 465, 71, 27)

    With oShape
        .Fill.ForeColor.RGB = RGB(191, 191, 191)
        .Fill.Transparency = 0
        .Name = boxName

        With .TextFrame.TextRange
          .Text = "Placeholder"

        End With   ' TextFrame

    End With ' RoundedRectangle

End Sub

我想锁定此形状的文本,即让前端用户无法编辑它

如何实现?

这在 VBA 中不容易完成,您必须添加 类 来解压缩、编辑和重新压缩底层 XML。这是我关于通过手动编辑 XML: OOXML Hacking: Locking Graphics 来锁定形状的文章。您可能想要添加 NoTextEdit="1" 属性,该属性在 锁定图形:其他对象 部分中提到。这是未修改的文本框 XML 的样子:

<p:sp>
    <p:nvSpPr>
        <p:cNvPr id="4" name="TextBox 3">
            <a:extLst>
                <a:ext uri="{FF2B5EF4-FFF2-40B4-BE49-F238E27FC236}">
                    <a16:creationId xmlns:a16="http://schemas.microsoft.com/office/drawing/2014/main" id="{F9D1A779-1CA7-4FA6-AFA2-B0954FC9EA09}"/>
                </a:ext>
            </a:extLst>
        </p:cNvPr>
        <p:cNvSpPr txBox="1"/>
        <p:nvPr/>
    </p:nvSpPr>
</p:sp>

修改为文本框锁定:

<p:sp>
    <p:nvSpPr>
        <p:cNvPr id="4" name="TextBox 3">
            <a:extLst>
                <a:ext uri="{FF2B5EF4-FFF2-40B4-BE49-F238E27FC236}">
                    <a16:creationId xmlns:a16="http://schemas.microsoft.com/office/drawing/2014/main" id="{F9D1A779-1CA7-4FA6-AFA2-B0954FC9EA09}"/>
                </a:ext>
            </a:extLst>
        </p:cNvPr>
        <p:cNvSpPr txBox="1">
            <a:splocks noTextEdit="1"
        </p:cNvSpPr>
        <p:nvPr/>
    </p:nvSpPr>
</p:sp>

在 Visual Studio 中使用 VB 和 Open XML SDK 更容易做到。下面是针对这种情况的一些通用代码:

Imports DocumentFormat.OpenXml.Drawing
Imports DocumentFormat.OpenXml

Namespace GeneratedCode
    Public Class GeneratedClass
        Public Function GenerateShapeLocks() As ShapeLocks
            Dim shapeLocks1 As ShapeLocks = New ShapeLocks() With {
                .NoTextEdit = True
            }
            Return shapeLocks1
        End Function
    End Class
End Namespace