locked
Having problem programatically altering properties of word docs RRS feed

  • Question

  • User-92576484 posted

    I'm trying to alter the Title property of word docs on my server. The error msg I'm getting is telling me I'm not including a reference to an assembly, but I'm pretty positive I am and Google isn't helping me out with this. Any ideas what the issue is? Maybe word 2003 vs. word 2007 (2007 is what I have installed/am referencing)?

    My Code:

     

           
          static void openDoc() //will be passing in fileName eventually
          (
                Word.Application app = new Word.Application();
                app.Visible = true;
                object visible = true;
                object fileName = "c:\\temp\\MyTest.doc";
                object optional = System.Type.Missing;
                Word.Document document = app.Documents.Open(ref fileName, ref optional, ref
           optional, ref optional, ref optional, ref optional, ref optional, ref
           optional, ref optional, ref optional);
    
                string propertyName = "Title";
    
    
                GetWordDoucumentPropertyValue(document, propertyName);
            }
    
    
    
             object GetWordDocumentPropertyValue(Word.Document document, string propertyName)
             {
                object builtInProperties = document.BuiltInDocumentProperties;
                Type builtInPropertiesType = builtInProperties.GetType();
                object property = builtInPropertiesType.InvokeMember("Item", BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });
                Type propertyType = property.GetType();
                object propertyValue = propertyType.InvokeMember("Value", BindingFlags.GetProperty, null, property, new object[] { });
                return propertyValue;
             }
    
            void SetWordDocumentPropertyValue(Word.Document document, string propertyName, string propertyValue)
            {
                object builtInProperties = document.BuiltInDocumentProperties;
                Type builtInPropertiesType = builtInProperties.GetType();
                object property = builtInPropertiesType.InvokeMember("Item", System.Reflection.BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });
                Type propertyType = property.GetType();
                propertyType.InvokeMember("Value", BindingFlags.SetProperty, null, property, new object[] { propertyValue });
                document.UpdateSummaryProperties();
                document.Save();
            }
    
      

     

     

    Friday, May 8, 2009 3:53 PM

All replies

  • User1052258516 posted

    This might seem like a dumb question but is Word installed on your server?  If not is has to be.

    Friday, May 8, 2009 6:31 PM
  • User-92576484 posted

     Not a bad question at all, I'm actually thinking that's part of the issue... Word is installed, but it's Word 2007. I've added references to everything I can possibly think of (MS Word, MS Office). Everything I've found online is for Word 2003, even on MSDN.

     
     

     
     

    Friday, May 8, 2009 9:04 PM
  • User1052258516 posted

    Another issue with using Word like this is permissions to launch the OLE Server (word.exe).  You will need to configure DCOM and give the IUSR_machinename and or NETWORK SERVICES account launch permissions as well.

    Saturday, May 9, 2009 1:48 AM
  • User-92576484 posted

     OK, I took care of the permissions and configuration, and also figured out I just wasn't being specific enough when referencing MS Office.

    Now, I'm having an issue where I'm getting a COM exception on this line: document.UpdateSummaryProperties();

     I have no idea why I'm getting it, and Google isn't being too helpful. Here is my code:

     

           static void testWord()
            {
                object Empty = Type.Missing; 
                Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
                app.Visible = true;
                object visible = true;
                object fileName = @"C:\Temp\Test.DOC";
                Microsoft.Office.Interop.Word.Document document = app.Documents.Open(ref fileName, ref Empty, ref Empty, ref Empty, ref Empty, ref Empty, ref Empty, ref Empty,
                         ref Empty, ref Empty, ref Empty, ref Empty, ref Empty, ref Empty, ref Empty, ref Empty);
    
                string propertyName = "";
                string propertyValue = "";
                propertyName = "Title";
                propertyValue = "TestWorks";
    
                SetWordDocumentPropertyValue(document, propertyName, propertyValue);
            }
    
    
            static void SetWordDocumentPropertyValue(Microsoft.Office.Interop.Word.Document document, string propertyName, string propertyValue)
            {
                object builtInProperties = document.BuiltInDocumentProperties;
                Type builtInPropertiesType = builtInProperties.GetType();
                object property = builtInPropertiesType.InvokeMember("Item", System.Reflection.BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });
                Type propertyType = property.GetType();
                propertyType.InvokeMember("Value", System.Reflection.BindingFlags.SetProperty, null, property, new object[] { propertyValue });
                document.UpdateSummaryProperties();
                document.Save();
            }
      
    Monday, May 11, 2009 1:40 PM
  • User1052258516 posted

    A couple of things with your code you should be destroying the COM objects like my sample below or you will end up with hundreds of WINWORD.EXE objects in memory which may be your issue.  Also you might post the COM exception as well.

    public static void TestWord()
    {
        object Empty = Type.Missing;
        Microsoft.Office.Interop.Word.Application app = null;
        Microsoft.Office.Interop.Word.Document document  = null;
        object visible = true;
        object saveChanges = false;
        object fileName = @"C:\Temp\temp.doc";
    
        try
        {
            app = new Microsoft.Office.Interop.Word.Application();
    
            document = app.Documents.Open(ref fileName, ref Empty, ref Empty, ref Empty, ref Empty, ref Empty, ref Empty, ref Empty,
                ref Empty, ref Empty, ref Empty, ref visible, ref Empty, ref Empty, ref Empty, ref Empty);
    
            string propertyName = "";
            string propertyValue = "";
            propertyName = "Title";
            propertyValue = "TestWorks";
    
            SetWordDocumentPropertyValue(document, propertyName, propertyValue);
            document.Close(ref saveChanges, ref Empty, ref Empty);
            app.Quit(ref saveChanges, ref Empty, ref Empty);
    
        }
        catch { }
        finally
        {
            if(app != null)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
    
            if (document != null)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(document);
        }
        
    }
    
    
    public static void SetWordDocumentPropertyValue(Microsoft.Office.Interop.Word.Document document, string propertyName, string propertyValue)
    {
        object builtInProperties = document.BuiltInDocumentProperties;
        Type builtInPropertiesType = builtInProperties.GetType();
        object property = builtInPropertiesType.InvokeMember("Item", System.Reflection.BindingFlags.GetProperty, null, builtInProperties, new object[] { propertyName });
        Type propertyType = property.GetType();
        propertyType.InvokeMember("Value", System.Reflection.BindingFlags.SetProperty, null, property, new object[] { propertyValue });
        document.UpdateSummaryProperties();
        document.Save();
    }
    Monday, May 11, 2009 7:53 PM