首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Office 開發實戰:使用文件資訊面板管理文件屬性與自動化業務流程

     編輯:關於.NET

企業面臨資訊分散、難以搜尋與合規的挑戰。Office 2007 引入的文件資訊面板 (Document Information Panel, DIP),結合 InfoPath 表單、內容控件、Quick Parts 與 OpenXML 格式,提供了一套完整的文件級中繼資料管理解決方案。本文深入剖析 DIP 架構、程式碼操作、與檢查清單管理應用的完整實作。

一、DIP 支援的四類文件屬性

  1. 核心/標準屬性:作者、標題、主題、關鍵字、類別、狀態、註解等,所有 Office 文件共用。
  2. 自訂文件屬性:附加可信任的 InfoPath 表單範本 (.xsn) 作為自訂 DIP,擴充業務脈絡(如專案名稱、階段、團隊成員)。
  3. SharePoint 內容類型:文件庫文件自動對應站點內容類型欄位,離線/線上編輯同步回寫。
  4. 擴充與進階自訂屬性:透過「進階屬性」對話框的「摘要」與「自訂」索引標籤管理。

二、OpenXML 封裝結構與屬性存儲位置

Office 2007 文件本質是 ZIP 套件,解壓後可見:

  • _rels/.rels:關係檔,指向 docProps/core.xml(標準屬性)、docProps/app.xml(擴充屬性)、docProps/custom.xml(自訂屬性)、customXml/itemN.xml(InfoPath 表單資料)。
  • InfoPath 表單資料另存為 自訂 XML 組件,其關係檔 (itemPropsN.xml) 標註 schemaRef 指向 InfoPath 結構描述,並可宣告為「自訂文件資訊面板」(customDocumentInformationPanel)。

三、程式碼操作文件屬性(VSTO / Word 物件模型)

1. 標準屬性:BuiltInDocumentProperties 與 CustomXMLParts

' 透過 BuiltInDocumentProperties
Dim props As DocumentProperties = DirectCast(Globals.ThisDocument.BuiltInDocumentProperties, DocumentProperties)
Dim prop As DocumentProperty = props.Item("Category")
prop.Value = "Modified Checklist"

' 透過 CustomXMLParts (XPath 操作 core.xml)
Dim xmlParts As CustomXMLParts = Globals.ThisDocument.CustomXMLParts
xmlParts.Item(1).SelectSingleNode("//*[local-name()='category']").Text = "modified category"

2. 自訂屬性 (InfoPath 表單資料)

' 讀取 InfoPath 表單欄位
Dim ipXml As New XmlDocument()
ipXml.LoadXml(Globals.ThisDocument.CustomXMLParts.Item(4).XML)
Dim projName = ipXml.SelectSingleNode("//*[local-name()='ProjectName']").InnerText

' 寫入需配合 InfoPath 結構描述,建議透過 DIP UI 或表單邏輯維護

3. 進階自訂屬性 (CustomDocumentProperties)

' 讀取/寫入
Dim cProps = DirectCast(Globals.ThisDocument.CustomDocumentProperties, DocumentProperties)
Dim clientProp = cProps.Item("Client")
clientProp.Value = "Microsoft"

' 新增屬性
cProps.Add("ChecklistVersion", False, MsoDocProperties.msoPropertyTypeNumber, "1.1")

四、完整應用實作:檢查清單管理系統

1. 開發與發布 InfoPath 表單範本

  • 定義分類:Business (IT)、Personal (Hospital) 等,使用條件式格式化動態顯示對應欄位區段。
  • 欄位類型:多行文字、單行文字、日期選取器。
  • 安全性:設定「安全性與信任」為「完整信任」,使用自我簽署憑證發布至本機信任位置或 MOSS 表單庫。

2. Visual Studio 建立 Word 2007 文件專案

  • 設計階段附加 ChecklistMetadata.xsn 為自訂 DIP,預設檢視設為表單檢視。
  • 驗證:切換分類下拉選單,確認 DIP 顯示對應欄位區段。

3. 自訂功能區控制 DIP 顯示/隱藏

Private Sub ShowHideDIP_Click(sender As Object, e As RibbonControlEventArgs) Handles ShowHideDIP.Click
    Globals.ThisDocument.Application.DisplayDocumentInformationPanel = Not Globals.ThisDocument.Application.DisplayDocumentInformationPanel
End Sub

4. 依分類插入 Quick Part (建構組塊)

  • 建立範本 ChecklistsQuickPartsTemplate.dotm,將各分類檢查清單存為 Quick Part,分類名稱前綴「CL - 」。
  • 部署範本至用戶 AppData 目錄,文件專案設定 AttachedTemplate 指向該範本。
  • 程式碼邏輯:讀取 InfoPath 表單 ChCategory 欄位 → 篩選範本中對應分類的所有 Quick Part → 逐一插入為內容控件,每個換頁。

5. 內容控件繫結:雙向同步 DIP 與文件本文

' 繫結至 InfoPath 欄位 ProjectName (CustomXMLParts 第 4 項)
Globals.ThisDocument.ContentControls(1).XMLMapping.SetMapping("//*[local-name()='ProjectName']", , Globals.ThisDocument.CustomXMLParts.Item(4))

' 繫結至標準屬性 Category (CustomXMLParts 第 1 項 core.xml)
Globals.ThisDocument.ContentControls(2).XMLMapping.SetMapping("//*[local-name()='category']", , Globals.ThisDocument.CustomXMLParts.Item(1))

繫結後,DIP 中修改欄位值即時反映於文件內容控件,反之亦然,實現「單一資料源,多處呈現」。

6. 標準/擴充屬性作為 Quick Part 插入

功能區「插入」→「Quick Part」→「文件屬性」,可插入作者、標題等標準屬性,與 DIP 雙向同步。

五、版本相容性考量

  • 相容模式下:標準屬性完整保留,自訂 DIP 與繫結功能失效。
  • 早期版本 (97-2003) 開啟/儲存:自訂 DIP 脫離,內容控件轉為靜態文字,標準屬性仍可透過「檔案屬性」對話框存取。
  • 建議:若需跨版本協作,僅依賴標準屬性;企業內部標準化於 Office 2007+ 才啟用完整 DIP 功能集。

總結

DIP + InfoPath + 內容控件 + Quick Parts + OpenXML,構成了 Office 2007 文件中繼資料管理與內容自動化的完整生態。廣泛採用標準化結構化中繼資料,將極大提升企業搜尋、合規、流程自動化的效能。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved