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

  • Question

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

      

    Thursday, May 24, 2007 8:19 PM

Answers

All replies

  • Friday, May 25, 2007 10:57 AM
    Moderator
  • That did the trick.  Not sure how I missed this article. Thank you.
    Friday, May 25, 2007 4:51 PM
  •  

    thank u Cindy Meister ,

    This article helped me a lot . 

     

    thanks

    shashank

    Wednesday, December 22, 2010 12:53 PM
  • 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

    Friday, March 7, 2014 8:18 AM
  • Referenced Knowledge Base Article link seems to have become invalid. MS site returns page not found.
    Wednesday, October 3, 2018 5:43 PM
  • 10/9/2018 That URL now returns a 404 Page Not Found error.

    Gardoglee

    Tuesday, October 9, 2018 5:39 PM
  • try https://web.archive.org/web/20130722205347/support.microsoft.com/kB/303296/en-us
    • Edited by bobatime Monday, March 11, 2019 7:56 PM
    Monday, March 11, 2019 7:55 PM