日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 操作 Word 修改word的高级属性中的自定义属性

發布時間:2023/12/10 C# 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 操作 Word 修改word的高级属性中的自定义属性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為 Microsoft Word 創建自動化客戶端

  • 啟動 Visual Studio .NET。
  • 文件菜單上,單擊新建,然后單擊項目。從 Visual C# 項目類型中選擇?Windows 應用程序。默認情況下會創建 Form1。
  • 添加對?Microsoft Word 對象庫的引用。為此,請按照下列步驟操作:
  • 項目菜單上,單擊添加引用
  • 在?COM?選項卡上,找到?Microsoft Word 對象庫,然后單擊選擇

    注意:Microsoft Office 2003 包含主 Interop 程序集 (PIA)。 Microsoft Office XP 不包含 PIA,但您可以下載 PIA。 有關 Office XP PIA 的其他信息,請單擊下面的文章編號,以查看 Microsoft 知識庫中相應的文章: 328912?INFO:Microsoft Office XP PIA 可供下載
  • 添加引用對話框中單擊確定以接受您的選擇。如果系統提示您為選定的庫生成包裝,請單擊
  • 視圖菜單上,選擇工具箱以顯示工具箱,然后向 Form1 添加一個按鈕。
  • 雙擊?Button1。出現該窗體的代碼窗口。
  • 在代碼窗口中,將以下代碼 private void button1_Click(object sender, System.EventArgs e) { } 替換為: private void button1_Click(object sender, System.EventArgs e) {Word.Application oWord;Word._Document oDoc;object oMissing = Missing.Value;object oDocBuiltInProps;object oDocCustomProps;//Create an instance of Microsoft Word and make it visible.oWord = new Word.Application();oWord.Visible = true;//Create a new Document and get the BuiltInDocumentProperties collection.oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);oDocBuiltInProps = oDoc.BuiltInDocumentProperties;Type typeDocBuiltInProps = oDocBuiltInProps.GetType();//Get the Author property and display it.string strIndex = "Author";string strValue;object oDocAuthorProp = typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null,oDocBuiltInProps, new object[] {strIndex} );Type typeDocAuthorProp = oDocAuthorProp.GetType();strValue = typeDocAuthorProp.InvokeMember("Value", BindingFlags.Default |BindingFlags.GetProperty,null,oDocAuthorProp,new object[] {} ).ToString();MessageBox.Show( "The Author is: " + strValue,"Author" );//Set the Subject property.strIndex = "Subject";strValue = "The Subject";typeDocAuthorProp.InvokeMember("Item", BindingFlags.Default | BindingFlags.SetProperty, null,oDocBuiltInProps, new object[] {strIndex,strValue} );//Add a property/value pair to the CustomDocumentProperties collection.oDocCustomProps = oDoc.CustomDocumentProperties;Type typeDocCustomProps = oDocCustomProps.GetType();strIndex = "Knowledge Base Article";strValue = "Q303296";object[] oArgs = {strIndex,false,MsoDocProperties.msoPropertyTypeString,strValue};typeDocCustomProps.InvokeMember("Add",BindingFlags.Default | BindingFlags.InvokeMethod, null, oDocCustomProps, oArgs );MessageBox.Show("Select \"Properties\" from the File menu "+ "to view the changes.\nSelect the Summary tab to view "+ "the Subject property and the Custom tab to view the Knowledge" + "Base Article property.", "Check File Properties",MessageBoxButtons.OK,MessageBoxIcon.Information); }
  • 滾動到代碼窗口頂部,然后將以下行添加到?using?指令列表的末尾: using Microsoft.Office.Core; using Word = Microsoft.Office.Interop.Word; using System.Reflection;
  • 按 F5 鍵運行該應用程序。
  • 注意DocumentProperties?和?DocumentProperty?接口是晚期綁定接口。若要使用這些接口,必須像對待?IDispatch?接口那樣對待它們。

    總結

    以上是生活随笔為你收集整理的C# 操作 Word 修改word的高级属性中的自定义属性的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。