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

您可以與文件一起儲存文件屬性。Office 應用程式提供許多內建屬性,例如 Author、Title 和 Subject。本主題顯示如何在 Microsoft Office Excel 和 Microsoft Office Word 中設定文件屬性。

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

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

設定 Excel 中的文件屬性

若要使用 Excel 中的內建屬性,請使用下列屬性:

這些屬性會傳回 DocumentProperties 物件,它是 DocumentProperty 物件的集合。您可以按照名稱或集合內的索引,使用該集合的 Item 屬性擷取特定的屬性。

下列程式碼範例顯示如何變更文件層級專案中的內建 Revision Number 屬性。

若要變更 Excel 中的 Revision Number 屬性

  1. 將內建文件屬性指派給變數。

    Dim properties As Microsoft.Office.Core.DocumentProperties
    
    properties = DirectCast(Globals.ThisWorkbook.BuiltinDocumentProperties, _
        Microsoft.Office.Core.DocumentProperties)
    
    Dim prop As Microsoft.Office.Core.DocumentProperty
    prop = properties.Item("Revision Number")
    
    Microsoft.Office.Core.DocumentProperties properties;
    
    properties = (Microsoft.Office.Core.DocumentProperties)
        Globals.ThisWorkbook.BuiltinDocumentProperties; 
    
    Microsoft.Office.Core.DocumentProperty prop;
    prop = properties["Revision Number"]; 
    
  2. 將 Revision Number 屬性加一。

    If prop.Value Is Nothing Then
        prop.Value = 1
    Else
        Dim revision As Integer
        If Integer.TryParse(prop.Value.ToString(), revision) Then
            prop.Value = revision + 1
            MessageBox.Show("Revision Number = " & revision)
        Else
            MessageBox.Show("Revision Number = invalid value")
        End If
    End If
    
    if (prop.Value == null)
    {
        prop.Value = 1;
    }
    else
    {
        int revision;
        if (int.TryParse((string)prop.Value, out revision))
        {
            prop.Value = revision + 1;
            MessageBox.Show("Revision Number = " + revision);
        }
        else
        {
            MessageBox.Show("Revision Number = invalid value");
        }
    }
    

設定 Word 中的文件屬性

若要使用 Word 中的內建屬性,請使用下列屬性:

這些屬性會傳回 DocumentProperties 物件,它是 DocumentProperty 物件的集合。您可以按照名稱或集合內的索引,使用該集合的 Item 屬性擷取特定的屬性。

下列程式碼範例顯示如何變更文件層級專案中的內建 Subject 屬性。

若要變更 Subject 屬性

  1. 將內建文件屬性指派給變數。

    Dim properties As Microsoft.Office.Core.DocumentProperties
    
    properties = DirectCast(Globals.ThisDocument.BuiltInDocumentProperties, _
        Microsoft.Office.Core.DocumentProperties)
    
    Microsoft.Office.Core.DocumentProperties properties;
    
    properties = (Microsoft.Office.Core.DocumentProperties)
        Globals.ThisDocument.BuiltInDocumentProperties; 
    
  2. 將 Subject 屬性變更為 "Whitepaper"。

    ' Set the Subject property.
    properties.Item("Subject").Value = "Whitepaper"
    
    // Set the Subject property. 
    properties["Subject"].Value = "Whitepaper"; 
    

穩固程式設計

這些範例假設您已在 Excel 文件層級專案的 ThisWorkbook 類別以及 Word 文件層級專案的 ThisDocument 類別中撰寫程式碼。

雖然您使用的是 Word 和 Excel 及其物件,但是 Microsoft Office 也提供了可用內建文件屬性的清單。嘗試存取未定義的屬性會引發例外狀況。

請參閱

工作

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

其他資源

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

文件層級自訂程式設計