Document Properties Not Casting Correctly?

J

Jeffrey A. Voigt

I have the following word code in C#. After opening up a word document, I'm
trying to get all of the custom document properties, however the cast is not
working at all. Has anyone ran into this issue before?

Word.Document ExistingDocument;

object OpenFormat = Word.WdOpenFormat.wdOpenFormatAuto;

object oDocumentType = Word.WdDocumentType.wdTypeDocument;

object ExistingFile = txtFileLocation.Text;

try

{

ExistingDocument = oWord.Documents.Open(ref ExistingFile,ref
oMissing,ref oFalse,ref oFalse, ref oMissing,ref oMissing,ref oMissing,ref
oMissing,ref oMissing,ref OpenFormat,ref oMissing, ref oTrue,ref oFalse,ref
oMissing,ref oMissing);

}

catch(Exception ex)

{

//error retrieving the file

MessageBox.Show(ex.Message);

return;

}

try

{

//get the custom properties of the word document

Microsoft.Office.Core.DocumentProperties properties =

ExistingDocument.CustomDocumentProperties as
Microsoft.Office.Core.DocumentProperties;

}


Thanks,
- V
 
C

Cindy M -WordMVP-

Hi Jeffrey,

An end user newsgroup is not that best place to duplicate your question yet
again...

At least in this posting you provide more information. I can't see any error
in the way you query the document properties, and I ran an equivalent test in
the Word VBA environment, just to double check. My guess is that you may be
encountering a problem specific to the C# interface. Have you asked in a C#
interop group? And have you searched the MS Knowledge Base?

One thing that does occur to me is this:

Microsoft.Office.Core.DocumentProperties properties =

ExistingDocument.CustomDocumentProperties as
Microsoft.Office.Core.DocumentProperties;

"Obviously", Office.Core.DocumentProperties cannot be equivalent to
WORD.DocumentProperties. VBA lets us do this

Dim p As Office.DocumentProperty
Dim ps As Office.DocumentProperties

Set ps = ActiveDocument.CustomDocumentProperties
For Each p In ps
Debug.Print p.Name
Next

and basically what is happening is that an Office application "inherits" (or
whatever the correct term is) these from the Office class. (The collection
"shines through" into the Word class, but still uses the Office stuff)

But I suspect that C#, with its stronger typing, won't accept it the way VBA
will let you do it. What if you try using CAST or something to convert the
Word document properties to Office document properties? Or type properties as
an Object?
I have the following word code in C#. After opening up a word document, I'm
trying to get all of the custom document properties, however the cast is not
working at all. Has anyone ran into this issue before?

Word.Document ExistingDocument;

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top