如何使用Excel Interop(VB.net)获得CustomDocumentProperties?

时间:2019-03-29 06:48:56

标签: excel vb.net interop excel-interop

我有一个使用VB.net编写的Excel Interop创建的Excel文档。 Adapting information from this post,我能够使用以下方法成功创建工作簿并编写其CustomDocumentProperties:

Dim objNewApp As Excel.Application
Dim objNewBook As Excel._Workbook
Dim objNewBooks As Excel.Workbooks

objNewApp = New Excel.Application With {
    .DisplayAlerts = True,
    .Visible = True,
    .UserControl = True,
    .ScreenUpdating = True
    }

objNewBooks = objNewApp.Workbooks
objNewBook = objNewBooks.Add

Dim properties As Object = objNewBook.CustomDocumentProperties
Dim propertiesType As Type = properties.[GetType]()
Dim documentClient As Object() = {"Client", False, Core.MsoDocProperties.msoPropertyTypeString, "In-Progress"}
propertiesType.InvokeMember("Add", BindingFlags.InvokeMethod, Nothing, properties, documentClient)

但是,设置此属性后,我无法成功读取它。在进行了彻底搜索之后,关于SO以及MSDN上有关此主题的大多数文章都建议阅读this missing MSDN article,以了解有关如何执行此操作的更多信息,并指出通常在Interop中这样做是多么愚蠢。但是,我将在其余程序中使用Interop,因此我想找到特定于Interop的解决方案(而不是VSTO)。

Adapting from this post,我相信我的当前代码在正确的轨道上:

Dim ReadClient As String = "Client"
Dim ObjReadClient = propertiesType.InvokeMember("Item", BindingFlags.GetProperty, Nothing, properties, New Object() {ReadClient})
Dim TypeReadClient As Type = ObjReadClient.GetType
Dim ClientString As String

ClientString = TypeReadClient.InvokeMember("Value", BindingFlags.GetProperty, Nothing, properties, New Object() {})

但是,当我运行它时,我收到一个System.Runtime.InteropServices.COMException:

"Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))"   

进行更多研究,这似乎是由于最后一行代码中的“ Value”部分所致。我无法弄清楚从那里去哪里。

有人对这个难题的最后一部分有任何想法吗?如果需要的话,我很乐意澄清!

2 个答案:

答案 0 :(得分:0)

这是我特定问题的解决方案:

Dim ReadClientIndex As String = "Client"
Dim ReadClientValue As String
Dim ObjReadClient As Object = propertiesType.InvokeMember("Item", BindingFlags.GetProperty, Nothing, properties, New Object() {ReadClientIndex})
Dim TypeReadClient As Type = ObjReadClient.GetType()
ReadClientValue = TypeReadClient.InvokeMember("Value", BindingFlags.GetProperty, Nothing, ObjReadClient, New Object() {})

除了清理代码外,问题在于我应该在最后一行引用“ ObjReadClient”作为参数,而不是前面设置的“ properties”变量。

但是,由于VB.net或C#中缺少有关此主题的文档,因此以下一些资源可能对将来的用户有所帮助:

Link 1:最初在缺少的MSKB文章中提供了代码(使用C#,下面再次复制以保留。)

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);

Link 2:注意到在VB.net中更容易完成,但是C#将很快支持使用“动态”(十年前编写)进行后期绑定。我在某处找到了另一篇文章,解释了“动态”作为C#答案的重要性,但无法再次找到它链接。

Link 3:此信息是Excel特有的,但是我认为它可以帮助某些人专门寻找它。

Link 4:这是一个错误使用VTSO和Interop的示例,它可以帮助用户区分两者。

答案 1 :(得分:0)

使用c#获取自定义属性有点不同。请检查这些2。

不复制整个代码以进行重复操作。

How can I read excel custom document property using c# excel interop

How to get CustomDocumentProperties using Excel Interop?