Question

I'm using the SWT OLE api to edit a Word document in an Eclipse RCP. I read articles about how to read properties from the active document but now I'm facing a problem with collections like sections.

I would like to retrieve only the body section of my document but I don't know what to do with my sections object which is an IDispatch object. I read that the item method should be used but I don't understand how.

Was it helpful?

Solution

I found the solution so I'll share it with you :)

Here is a sample code to list all paragraphs of the active document of the word editor :

    OleAutomation active = activeDocument.getAutomation();
    if(active!=null){
    int[] paragraphsId = getId(active, "Paragraphs");
    if(paragraphsId.length > 0) {
        Variant vParagraphs = active.getProperty(paragraphsId[0]);
        if(vParagraphs != null){
            OleAutomation paragraphs = vParagraphs.getAutomation();
            if(paragraphs!=null){
                int[] countId = getId(paragraphs, "Count");
                if(countId.length > 0) {
                    Variant count = paragraphs.getProperty(countId[0]);
                    if(count!=null){
                        int numberOfParagraphs = count.getInt();
                        for(int i = 1 ; i <= numberOfParagraphs ; i++) {
                            Variant paragraph = paragraphs.invoke(0, new Variant[]{new Variant(i)});
                            if(paragraph!=null){
                                System.out.println("paragraph " + i + " added to list!");
                                listOfParagraphs.add(paragraph);
                            }
                        }
                        return listOfParagraphs;
                    }
                }
            }
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top