How to Set and Get Custom Properties to a word document

249 views
Skip to first unread message

venkky venkky

unread,
Mar 9, 2011, 8:01:12 AM3/9/11
to ComfyJ Forum
Hi,

I would like to add custom properties and read them to a MS Word
Document using ComfyJ generated APIs instead of manually opening File
--> Properties --> Custom and add properties.

Please kindly help me on this.

Regards,
Venkky

Sergei Piletsky

unread,
Mar 9, 2011, 1:55:40 PM3/9/11
to venkky venkky, ComfyJ Forum
Hi,

In Word API there is the ability to manage custom document properties. You
can access them via _Document.getBuiltinDocumentProperties() method:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._docum
ent.builtindocumentproperties.aspx

or _Document.getCustomDocumentProperties() method:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._docum
ent.customdocumentproperties.aspx

Sincerely,
Serge

venkky venkky

unread,
Mar 10, 2011, 2:20:29 AM3/10/11
to ComfyJ Forum
Hi,
Thanks for the reply.

I have written the following code to get the count of custom
properties,

OleFunctions.oleInitialize();

IUnknown activeWordApp =
ComFunctions.getActiveObject(Application.CLASS_ID);
_Application application =
Application.queryInterface(activeWordApp);

_Document doc = application.getActiveDocument();
IDispatch iprops = doc.getCustomDocumentProperties();
CustomPropertiesImpl cProps = new
CustomPropertiesImpl(iprops);
Int32 count = cProps.getCount();

OleFunctions.oleUninitialize();

I am getting the following exception when i am trying to execute this,

Exception c0000005, at 771311E0
Access violation: attempting to write memory at address 05D47A62
Native function stack data: 0,baf1c,
2e20368,2e2b978,30002,20c01a5,0,2e2ead0,20003,20801a7,0,0
com.jniwrapper.FunctionExecutionException: c0000005
at com.jniwrapper.Function.invokeVirtualFunc(Native Method)
at com.jniwrapper.FunctionCall.a(SourceFile:131)
at com.jniwrapper.FunctionCall.callVirtual(SourceFile:58)
at
com.jniwrapper.win32.com.impl.IUnknownImpl.invokeVirtualMethod(SourceFile:
660)
at
com.jniwrapper.win32.com.impl.IUnknownImpl.invokeStandardVirtualMethod(SourceFile:
739)
at
com.jniwrapper.win32.com.impl.IUnknownImpl.invokeStandardVirtualMethod(SourceFile:
722)
at
com.jniwrapper.win32.samples.wordex.word.impl.CustomPropertiesImpl.getCount(CustomPropertiesImpl.java:
114)
at
com.jniwrapper.samples.wordex.WordAutomationHeaderFooterSample.main(WordAutomationHeaderFooterSample.java:
77)
Exception in thread "main"

Please kindly help me to resolve this.

I am testing this on, MS Office 2003 installed on Win XP Service Pack
3, 32 bit.

Thanks & Regards,
Venkky

On Mar 9, 11:55 pm, Sergei Piletsky <sergei.pilet...@teamdev.com>
wrote:
> Hi,
>
> In Word API there is the ability to manage custom document properties. You
> can access them via _Document.getBuiltinDocumentProperties() method:http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word...
> ent.builtindocumentproperties.aspx
>
> or _Document.getCustomDocumentProperties() method:http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word...

Sergei Piletsky

unread,
Mar 10, 2011, 7:53:59 AM3/10/11
to venkky venkky, ComfyJ Forum
Hi Venkky,

According to this support article:
http://support.microsoft.com/kb/303296/en-us "DocumentProperties and the
DocumentProperty interfaces are late bound interfaces. To use these
interfaces, you must treat them like you would an IDispatch interface."

This probably explains why you were not able to invoke methods of that
object via appropriate interface, which in fact is early binding technique.

So, in this case I suppose you can use Automation to access fields and
method of that object, like it's demonstrated in the example below:

IDispatch properties = workbook.getBuiltinDocumentProperties();
// IDispatch properties = workbook.getCustomDocumentProperties();

boolean useCurrentThread = true;
Automation propertiesAutomation = new Automation(properties,
useCurrentThread);

Variant propertyCount = propertiesAutomation.getProperty("Count");
int count = (int) propertyCount.getLVal().getValue();
System.out.println("count = " + count);
for (int i = 1; i <= count; i++) {
Variant item = propertiesAutomation.getProperty("item", i);
Automation itemAutomation = new
Automation(item.getPdispVal(), useCurrentThread);
try {
Variant itemName =
itemAutomation.getProperty("Name");
System.out.println("itemName = " +
itemName.getValue());
Variant itemValue =
itemAutomation.getProperty("Value");
System.out.println("itemValue = " +
itemValue.getValue());
} catch (AutomationException e) {
ExcepInfo information = e.getExceptionInformation();
System.err.println("description: " +
information.getBstrDescription());
}
itemAutomation.release();
}
propertiesAutomation.release();

I also would like to notice that it's not necessary to use
OleFunctions.oleUninitialize(); at all, because it may interfere with
automatic memory management in ComfyJ.

Please let me know if you have another questions.

Sincerely,
Serge

> -----Original Message-----
> From: comfyj...@teamdev.com [mailto:comfyj...@teamdev.com] On
> Behalf Of venkky venkky
> Sent: Thursday, March 10, 2011 9:20 AM
> To: ComfyJ Forum
> Subject: Re: How to Set and Get Custom Properties to a word document
>
> Hi,

venkky venkky

unread,
Mar 12, 2011, 11:36:31 AM3/12/11
to ComfyJ Forum
Hi Serge,
Its working great. Thanks a lot for your reply.
Sorry to ask simple questions, i am getting it clear now.
Thanks once again.
Venkky

On Mar 10, 5:53 pm, Sergei Piletsky <sergei.pilet...@teamdev.com>
wrote:
Reply all
Reply to author
Forward
0 new messages