HOW TO:使用內容控制項保護文件的部分

更新: 2008 年 7 月

適用於

本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。

專案類型

  • 文件層級專案

  • 應用程式層級專案

Microsoft Office 版本

  • Word 2007

如需詳細資訊,請參閱依應用程式和專案類型提供的功能

當您保護文件的某個部分時,使用者即無法變更或刪除文件中該部分的內容。有多種方法可以使用內容控制項來保護 Microsoft Office Word 2007 文件的某個部分:

  • 您可以保護內容控制項。

  • 您可以保護文件中不在內容控制項內的部分。

保護內容控制項

您可以透過文件層級專案,於設計階段或執行階段設定該控制項的屬性,防止使用者編輯或刪除內容控制項。

從 Visual Studio 2008 Service Pack 1 (SP1) 開始,您也可以使用應用程式層級專案,保護您在執行階段加入至文件的內容控制項。如需詳細資訊,請參閱 HOW TO:將內容控制項加入至 Word 文件

若要在設計階段保護內容控制項

  1. 在裝載 (Host) 於 Visual Studio 設計工具的文件中,選取想要保護的內容控制項。

  2. 在 [屬性] 視窗中,設定下列一個或兩個屬性:

    • 若要防止使用者編輯控制項,請將 [LockContents] 設定為 [True]。

    • 若要防止使用者刪除控制項,請將 [LockContentControl] 設定為 [True]。

  3. 按一下 [確定]。

若要透過文件層級專案在執行階段保護內容控制項

  • 將內容控制項的 LockContents 屬性設定為 true 防止使用者編輯控制項,並將 LockContentControl 屬性設定為 true 防止使用者刪除控制項。

    下列程式碼範例示範透過文件層級專案,使用兩個不同 RichTextContentControl 物件的 LockContentsLockContentControl 屬性。若要執行這個程式碼,請將程式碼加入至專案的 ThisDocument 類別 (Class) 中,並從 ThisDocument_Startup 事件處理常式呼叫 AddProtectedContentControls 方法。

    Dim deletableControl As Microsoft.Office.Tools.Word.RichTextContentControl
    Dim editableControl As Microsoft.Office.Tools.Word.RichTextContentControl
    
    Private Sub AddProtectedContentControls()
        Me.Paragraphs(1).Range.InsertParagraphBefore()
        Dim range1 As Word.Range = Me.Paragraphs(1).Range
    
        deletableControl = Me.Controls.AddRichTextContentControl(range1, _
            "deletableControl")
        deletableControl.PlaceholderText = "You can delete this control, " & _
            "but you cannot edit it"
        deletableControl.LockContents = True
    
        range1.InsertParagraphAfter()
        Dim range2 As Word.Range = Me.Paragraphs(2).Range
    
        editableControl = Me.Controls.AddRichTextContentControl(range2, _
            "editableControl")
        editableControl.PlaceholderText = "You can edit this control, " & _
            "but you cannot delete it"
        editableControl.LockContentControl = True
    End Sub
    
    private Microsoft.Office.Tools.Word.RichTextContentControl deletableControl;
    private Microsoft.Office.Tools.Word.RichTextContentControl editableControl;
    
    private void AddProtectedContentControls()
    {
        this.Paragraphs[1].Range.InsertParagraphBefore();
        Word.Range range1 = this.Paragraphs[1].Range;
    
        deletableControl = this.Controls.AddRichTextContentControl(range1,
            "deletableControl");
        deletableControl.PlaceholderText = "You can delete this control, " +
            "but you cannot edit it";
        deletableControl.LockContents = true;
    
        range1.InsertParagraphAfter();
        Word.Range range2 = this.Paragraphs[2].Range;
    
        editableControl = this.Controls.AddRichTextContentControl(range2,
            "editableControl");
        editableControl.PlaceholderText = "You can edit this control, " +
            "but you cannot delete it";
        editableControl.LockContentControl = true;
    }
    

若要透過應用程式層級專案在執行階段保護內容控制項

  • 將內容控制項的 LockContents 屬性設定為 true 防止使用者編輯控制項,並將 LockContentControl 屬性設定為 true 防止使用者刪除控制項。

    下列程式碼範例示範透過應用程式層級專案,使用兩個不同 RichTextContentControl 物件的 LockContentsLockContentControl 屬性。若要執行這段程式碼,請將程式碼加入至專案的 ThisAddIn 類別中,並從 ThisAddIn_Startup 事件處理常式呼叫 AddProtectedContentControls 方法。

    Dim deletableControl As Microsoft.Office.Tools.Word.RichTextContentControl
    Dim editableControl As Microsoft.Office.Tools.Word.RichTextContentControl
    
    Private Sub AddProtectedContentControls()
        Dim vstoDocument As Microsoft.Office.Tools.Word.Document = _
            Me.Application.ActiveDocument.GetVstoObject()
        vstoDocument.Paragraphs(1).Range.InsertParagraphBefore()
        Dim range1 As Word.Range = vstoDocument.Paragraphs(1).Range
    
        deletableControl = vstoDocument.Controls.AddRichTextContentControl(range1, _
            "deletableControl")
        deletableControl.PlaceholderText = "You can delete this control, " & _
            "but you cannot edit it"
        deletableControl.LockContents = True
    
        range1.InsertParagraphAfter()
        Dim range2 As Word.Range = vstoDocument.Paragraphs(2).Range
    
        editableControl = vstoDocument.Controls.AddRichTextContentControl(range2, _
            "editableControl")
        editableControl.PlaceholderText = "You can edit this control, " & _
            "but you cannot delete it"
        editableControl.LockContentControl = True
    End Sub
    
    private Microsoft.Office.Tools.Word.RichTextContentControl deletableControl;
    private Microsoft.Office.Tools.Word.RichTextContentControl editableControl;
    
    private void AddProtectedContentControls()
    {
        Microsoft.Office.Tools.Word.Document vstoDocument = 
            this.Application.ActiveDocument.GetVstoObject();
        vstoDocument.Paragraphs[1].Range.InsertParagraphBefore();
        Word.Range range1 = vstoDocument.Paragraphs[1].Range;
    
        deletableControl = vstoDocument.Controls.AddRichTextContentControl(range1,
            "deletableControl");
        deletableControl.PlaceholderText = "You can delete this control, " +
            "but you cannot edit it";
        deletableControl.LockContents = true;
    
        range1.InsertParagraphAfter();
        Word.Range range2 = vstoDocument.Paragraphs[2].Range;
    
        editableControl = vstoDocument.Controls.AddRichTextContentControl(range2,
            "editableControl");
        editableControl.PlaceholderText = "You can edit this control, " +
            "but you cannot delete it.";
        editableControl.LockContentControl = true;
    }
    

保護文件中不在內容控制項內的部分

您可以將文件區域放入 GroupContentControl 中,防止使用者變更該區域。這在下列情況下十分有用:

  • 您想要保護不含內容控制項的區域。

  • 您想要保護已包含內容控制項的區域,但是想要保護的文字或其他項目不在內容控制項內。

注意事項:

如果建立的 GroupContentControl 包含內嵌內容控制項,則不會自動保護這些內嵌內容控制項。若要防止使用者編輯內嵌內容控制項,請使用控制項的 LockContents 屬性。

若要在設計階段保護文件的區域

  1. 在裝載於 Visual Studio 設計工具的文件中,選取想要保護的區域。

  2. 按一下 [功能區] 上的 [開發人員] 索引標籤。

    注意事項:

    如果 [開發人員] 索引標籤沒有顯示,您必須先使其顯示。如需詳細資訊,請參閱 HOW TO:在功能區顯示開發人員索引標籤

  3. 按一下 [控制項] 群組中的 [群組] 下拉按鈕,然後按一下 [群組]。

    這樣會在專案的 ThisDocument 類別中,自動產生內含保護區域的 GroupContentControl。在設計階段可以看到代表群組控制項的框線,但是在執行階段則看不到框線。

若要透過文件層級專案在執行階段保護文件的區域

  • 以程式設計方式選取想要保護的區域,然後呼叫 AddGroupContentControl 方法以建立 GroupContentControl

    下列程式碼範例會將文字加入至文件的第一段,然後選取第一段,再具現化 GroupContentControl。若要執行這個程式碼,請將程式碼加入至專案的 ThisDocument 類別中,並從 ThisDocument_Startup 事件處理常式呼叫 ProtectFirstParagraph 方法。

    Dim groupControl1 As Microsoft.Office.Tools.Word.GroupContentControl
    
    Private Sub ProtectFirstParagraph()
        Me.Paragraphs(1).Range.InsertParagraphBefore()
        Dim range1 As Word.Range = Me.Paragraphs(1).Range
        range1.Text = "You cannot edit or change the formatting of text " & _
                "in this paragraph, because this paragraph is in a GroupContentControl."
        range1.Select()
    
        groupControl1 = Me.Controls.AddGroupContentControl("groupControl1")
    End Sub
    
    private Microsoft.Office.Tools.Word.GroupContentControl groupControl1;
    
    private void ProtectFirstParagraph()
    {
        this.Paragraphs[1].Range.InsertParagraphBefore();
        Word.Range range1 = this.Paragraphs[1].Range;
    
        range1.Text = "You cannot edit or change the formatting of text " +
            "in this sentence, because this sentence is in a GroupContentControl.";
        range1.Select();
        groupControl1 = this.Controls.AddGroupContentControl("groupControl1");
    }
    

若要透過應用程式層級專案在執行階段保護文件的區域

  • 以程式設計方式選取想要保護的區域,然後呼叫 AddGroupContentControl 方法以建立 GroupContentControl

    下列程式碼範例會將文字加入至現用文件的第一個段落,然後選取此段落,再具現化 GroupContentControl。若要執行這段程式碼,請將程式碼加入至專案的 ThisAddIn 類別中,並從 ThisAddIn_Startup 事件處理常式呼叫 ProtectFirstParagraph 方法。

    Dim groupControl1 As Microsoft.Office.Tools.Word.GroupContentControl
    
    Private Sub ProtectFirstParagraph()
        Dim vstoDocument As Microsoft.Office.Tools.Word.Document = _
            Me.Application.ActiveDocument.GetVstoObject()
        vstoDocument.Paragraphs(1).Range.InsertParagraphBefore()
    
        Dim range1 As Word.Range = vstoDocument.Paragraphs(1).Range
        range1.Text = "You cannot edit or change the formatting of text " & _
                "in this paragraph, because this paragraph is in a GroupContentControl."
        range1.Select()
    
        groupControl1 = vstoDocument.Controls.AddGroupContentControl("groupControl1")
    End Sub
    
    private Microsoft.Office.Tools.Word.GroupContentControl groupControl1;
    
    private void ProtectFirstParagraph()
    {
        Microsoft.Office.Tools.Word.Document vstoDocument =
            this.Application.ActiveDocument.GetVstoObject();
        vstoDocument.Paragraphs[1].Range.InsertParagraphBefore();
    
        Word.Range range1 = vstoDocument.Paragraphs[1].Range;
        range1.Text = "You cannot edit or change the formatting of text " +
            "in this sentence, because this sentence is in a GroupContentControl.";
        range1.Select();
    
        groupControl1 = vstoDocument.Controls.AddGroupContentControl("groupControl1");
    }
    

請參閱

工作

HOW TO:將內容控制項加入至 Word 文件

概念

內容控制項

主項目和主控制項概觀

主項目和主控制項的程式設計限制

在執行階段將控制項加入至 Office 文件

主控制項的 Helper 方法

其他資源

Word 主控制項

變更記錄

日期

記錄

原因

2008 年 7 月

加入應用程式層級增益集的程序。

SP1 功能變更。