共用方式為


HOW TO:建立及修改自訂文件屬性

上面列出的 Microsoft Office 應用程式提供與文件一起儲存的內建屬性。此外,如果您有想要與文件一起儲存的其他資訊,則可以建立和修改自訂文件屬性。

**適用於:**本主題中的資訊適用於下列應用程式的文件層級專案和應用程式層級專案:Excel 2013 和 Excel 2010、PowerPoint 2013 和 PowerPoint 2010、Project 2013 和 Project 2010、Word 2013 和 Word 2010。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

請使用文件的 CustomDocumentProperties 屬性來處理自訂屬性。例如,在 Microsoft Office Excel 的文件層級專案中,請使用 ThisWorkbook 類別的 CustomDocumentProperties 屬性。在 Excel 的應用程式層級專案中,則請使用 Microsoft.Office.Interop.Excel.Workbook 物件的 CustomDocumentProperties 屬性。這些屬性會傳回 DocumentProperties 物件,它是 DocumentProperty 物件的集合。您可以按照名稱或集合內的索引,使用該集合的 Item 屬性擷取特定的屬性。

下列範例示範如何在 Excel 的文件層級自訂中加入自訂屬性,並指派該屬性的值。

視訊的連結 如需觀看相關示範影片,請參閱如何:存取和操作 Microsoft Word 中的自訂文件屬性?(英文)。

範例

Sub TestProperties()
    Dim properties As Microsoft.Office.Core.DocumentProperties
    properties = CType(Me.CustomDocumentProperties, Office.DocumentProperties)

    If ReadDocumentProperty("Project Name") <> Nothing Then
        properties("Project Name").Delete()
    End If

    properties.Add("Project Name", False, _
        Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString, _
        "White Papers")
End Sub

Private Function ReadDocumentProperty(ByVal propertyName As String) As String
    Dim properties As Office.DocumentProperties
    properties = CType(Me.CustomDocumentProperties, Office.DocumentProperties)

    Dim prop As Office.DocumentProperty

    For Each prop In properties
        If prop.Name = propertyName Then
            Return prop.Value.ToString()
        End If
    Next

    Return Nothing
End Function
void TestProperties()
{
    Microsoft.Office.Core.DocumentProperties properties;
    properties = (Office.DocumentProperties)this.CustomDocumentProperties;

    if (ReadDocumentProperty("Project Name") != null)
    {
        properties["Project Name"].Delete();
    }

    properties.Add("Project Name", false,
        Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString,
        "White Papers");
}

private string ReadDocumentProperty(string propertyName)
{
    Office.DocumentProperties properties;
    properties = (Office.DocumentProperties)this.CustomDocumentProperties;

    foreach (Office.DocumentProperty prop in properties)
    {
        if (prop.Name == propertyName)
        {
            return prop.Value.ToString();
        }
    }
    return null;
}

穩固程式設計

嘗試存取未定義之屬性的 Value 屬性將會引發例外狀況。

請參閱

工作

HOW TO:從文件屬性中讀取及寫入

其他資源

應用程式層級增益集程式設計

文件層級自訂程式設計