Working with Custom Document Properties in Microsoft Word 2003 |
|
There is a glitch in the type library of Microsoft Word 2003 which makes it confusing for Word developers to work with Custom Document Properties programmatically. If you browse through the Word type library (MSWORD.OLB) using Visual Basic's Object Browser and look for CustomProperties, you will see that the Add function has the following signature:
Function Add(Name As String, Value As String) As CustomProperty Member of Word.CustomProperties
Note that the function accepts two string parameters. Consequently, com2java will generate the corresponding Java method for this function as follows:
public word.CustomProperty add(String name, String value)
When you then call CustomProperties.Add() from Java passing it two valid String parameters, you will get an exception from your Java client that looks as follows:
java.lang.ClassCastException: A COM object reference via iid
00020400-0000-0000-c000-000000000046 does not support COM
interface b923fde1-f08c-11d3-91b0-00105a0a19fd
at com.linar.jintegra.Dispatch.b(Unknown Source)
at com.linar.jintegra.Dispatch.vtblInvoke(Unknown Source)
at word.CustomPropertiesProxy.add(CustomPropertiesProxy.java:212)
at CustomDocProperties.main(CustomDocProperties.java:37)
When to attempt to call the same function from VB passing it 2 string parameters as required by the function signature, you will get the following error:
Run-time error '450': Wrong number of arguments or invalid property assignment
The reason for this error is that the signature of the CustomProperties.Add function as specified in the Word type library is incorrect. Instead of requiring 2 string parameters, you actually need to pass at least 4 parameters for the function call to be successful. For more information on the CustomProperties.Add function, refer to this MSDN article: http://msdn.microsoft.com/library/en-us/vbaof10/html/ofmthAdd.asp
From VB, you would be able to call the CustomProperties.Add function properly passing it the four required parameters. This is possible, in spite of the fact that the function signature calls for only 2 parameters, due to late binding. Late binding does not perform type checking at compile time. J-Integra provides a way to mimic VBí»s late binding functionality in Java through a class called Dispatch. The Dispatch class has a method called invokeMethodByName() by which you can call methods of the Java proxy classes in a late-binding fashion. For more information, refer to KB artcle 34490.
The following Java program demonstrates how to work with Custom Document Properties in Microsoft Word 2003 using the Dispatch object provided by J-Integra.
import word.*;
// word is the package containing the Java proxies generated from MSWORD.OLB
import com.linar.jintegra.*;
public class CustomDocProperties {
public static void main(String[] args) {
try {
// Open a new Word document
Application app = new Application();
app.setVisible(true);
Document doc = app.getDocuments().add(null,null,null,null);
// Retrieve the document's custom properties
CustomProperties props = new CustomPropertiesProxy(
doc.getCustomDocumentProperties());
// How many custom properties does this document have?
Dispatch dispatch = new Dispatch(props);
Integer count = (Integer) dispatch.getPropertyByName("Count");
System.out.println("This new document has " + count + " custom properties.");
// Add a new custom property
System.out.println("Adding a new custom property . . . ");
// First, let's package the parameters into an array of Objects
Object name = "SomeName";
Boolean linkToContent = new Boolean(false);
Integer type = new Integer(4);
Object value = "SomeValue";
Object[] params = {name, linkToContent, type, value};
// the parameter 'type' assumes the following values:
// 0 for msoPropertyTypeBoolean
// 1 for msoPropertyTypeDate
// 2 for msoPropertyTypeFloat
// 3 for msoPropertyTypeNumber
// 4 for msoPropertyTypeString
// for more information, refer to:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaof10/html/ofmthAdd.asp
// then let's add this new custom property through the Dispatch call - invokeMethodByName()
dispatch.invokeMethodByName("Add", params);
// let's see if the new custom property was successfully added
count = (Integer) dispatch.getPropertyByName("Count");
System.out.println("This document has " + count + " new custom property.");
// Close the Word document without saving
app.quit(null,null,null);
}catch(Exception ex){
ex.printStackTrace();
}finally {
com.linar.jintegra.Cleaner.releaseAll();
}
}
}
|