none
Unable to cast COM object of type 'System.__ComObject' RRS feed

  • 问题

  • My project is to take a group of word 2003 documents and change the properties on them.  I am doing this by storing the path and the new properties in a SQL Server table and accessing it through my C# code.  The code opens the document just fine, but when it gets to this line in the code it throws a COM error.

     

    properties = (Microsoft.Office.Core.DocumentProperties)oDoc.Application.ActiveDocument.BuiltInDocumentProperties;

     

    Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Core.DocumentProperties'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{2DF8D04D-5BFA-101B-BDE5-00AA0044DE52}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

     

    My developement machine is running Vista with VS 2005 Team.  I have Office 2007 installed as well as Word 2003.  I have added the Microsoft Office 11.0 Object Library and the Microsoft Word 11.0 Object Library refrences. 

     

    Here is the rest of the code I am trying.  I hope someone can help.  Thanks.

     

    Code Snippet

    using System;

    using System.Data;

    using System.Configuration;

    using System.Collections;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using System.IO.Packaging;

    using Word = Microsoft.Office.Interop.Word;

    using Office = Microsoft.Office.Core;

    using System.Reflection;

    .

    .

    .

     

    object fileName = strFileLocation;

    Word.Application oWord;

    Word.Document oDoc;

    object missing = Type.Missing;

    object falseValue = false;

    object trueValue = true;

    oWord = new Word.Application();

    oWord.Visible = true;

     

    oDoc = oWord.Documents.Open(ref fileName, ref missing, ref falseValue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

     

    Office.DocumentProperties properties;

     

    properties = (Microsoft.Office.Core.DocumentProperties)oDoc.Application.ActiveDocument.BuiltInDocumentProperties;

     

    properties["Creator"].Value = Convert.ToString(row["Author"]);

    properties["Title"].Value = Convert.ToString(row["Title"]);

    properties["Subject"].Value = Convert.ToString(row["Subject"]);

    properties["Description"].Value = Convert.ToString(row["Description"]);

    properties["ContentStatus"].Value = Convert.ToString(row["Status"]);

    properties["ContentType"].Value = Convert.ToString(row["DocType"]);

    oDoc.Close(ref oFalse, ref oMissing, ref oMissing);

      

    2007年5月24日 20:19

答案

全部回复

  • 2007年5月25日 10:57
    版主
  • That did the trick.  Not sure how I missed this article. Thank you.
    2007年5月25日 16:51
  •  

    thank u Cindy Meister ,

    This article helped me a lot . 

     

    thanks

    shashank

    2010年12月22日 12:53
  • Going thru the knowledge base article, I am trying to implement the same for properties like number of lines, words, characters. In the below code snippet:

    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;

    Seeing the way they have implemented the properties for Author & Subject,

    i am not sure how to achieve the same incase of lines/words. C

    an any one suggest me a code for the abpve mentioned property. I found wdWordCount

    which returns the number of words, but not sure if it is correct. Please suggest

    me the correct way of doing it.

    Thanks/Saurav.


    Suarav

    2014年3月7日 8:18
  • Referenced Knowledge Base Article link seems to have become invalid. MS site returns page not found.
    2018年10月3日 17:43
  • 10/9/2018 That URL now returns a 404 Page Not Found error.

    Gardoglee

    2018年10月9日 17:39
  • try https://web.archive.org/web/20130722205347/support.microsoft.com/kB/303296/en-us
    • 已编辑 bobatime 2019年3月11日 19:56
    2019年3月11日 19:55