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

时间:2019-12-16 08:21:00

标签: vba powerpoint

我具有以下代码,该代码使用文本“占位符” 创建一个RoundedRectangle

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

我想锁定这种形状的文本,即使前端用户无法对其进行编辑

我该如何实现?

1 个答案:

答案 0 :(得分:1)

在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