建立及修改自訂文件屬性

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

適用對象: 本主題資訊適用於以下應用程式的文件層級專案和 VSTO 增益集專案:Excel、PowerPoint、 Project、Word。 如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

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

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

範例

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 屬性將會引發例外狀況。