Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word Automation CustomDocumentProperties Help!

Status
Not open for further replies.

gwinn7

Programmer
Feb 10, 2001
1,004
US
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

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top