Hello, I am attempting to automate filling in fields in a Word 2003 Document from C#.
I try to follow examples I have researched, but I am getting the following error at runtime, even though the project compiles without error....
"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))."
This error occurs on the following line of code...
prps = (Office.Core.DocumentProperties)wordApp.ActiveDocument.CustomDocumentProperties;
Here is what is being compiled/executed ...
--------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office;
private void cmdfinish_Click(object sender, EventArgs e)
{
// Create filename object...
object fileName = @"C:\\Documents and Settings\\gstar\\Desktop\\1.doc";
// Declare and Instantiate Word Application...
Word.Application wordApp = new Word.Application();
// Open Word document...
wordApp.Documents.Open(ref fileName,
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, ref missing, ref missing);
// Declare prps object as DocumentProperties object...
Microsoft.Office.Core.DocumentProperties prps;
// Capture CustomDocumentProperties in a usable object...
prps = (Office.Core.DocumentProperties)wordApp.ActiveDocument.CustomDocumentProperties;
// Fill Field...
prps["CONTRACTSELLER"].Value = "Tim";
}
Can anyone help? I know there is more code to add for error handling, but this code should work, but I don't see why it doesn't.
Thank you in advance,
Gary
gwinn7
I try to follow examples I have researched, but I am getting the following error at runtime, even though the project compiles without error....
"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))."
This error occurs on the following line of code...
prps = (Office.Core.DocumentProperties)wordApp.ActiveDocument.CustomDocumentProperties;
Here is what is being compiled/executed ...
--------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office;
private void cmdfinish_Click(object sender, EventArgs e)
{
// Create filename object...
object fileName = @"C:\\Documents and Settings\\gstar\\Desktop\\1.doc";
// Declare and Instantiate Word Application...
Word.Application wordApp = new Word.Application();
// Open Word document...
wordApp.Documents.Open(ref fileName,
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, ref missing, ref missing);
// Declare prps object as DocumentProperties object...
Microsoft.Office.Core.DocumentProperties prps;
// Capture CustomDocumentProperties in a usable object...
prps = (Office.Core.DocumentProperties)wordApp.ActiveDocument.CustomDocumentProperties;
// Fill Field...
prps["CONTRACTSELLER"].Value = "Tim";
}
Can anyone help? I know there is more code to add for error handling, but this code should work, but I don't see why it doesn't.
Thank you in advance,
Gary
gwinn7