java - 如何使用 Word Ole Automation 列出集合中的项目

标签 java ms-word ole

我正在使用 SWT OLE api 在 Eclipse RCP 中编辑 Word 文档。我阅读了有关如何从 Activity 文档中读取属性的文章,但现在我面临着像部分这样的集合的问题。

我只想检索文档的正文部分,但我不知道如何处理我的sections对象,它是一个IDispatch对象。我读到应该使用 item 方法,但我不明白如何使用。

最佳答案

我找到了解决方案,所以我会与您分享:)

这里是一个示例代码,用于列出单词编辑器的 Activity 文档的所有段落:

    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;
                    }
                }
            }
        }
    }

关于java - 如何使用 Word Ole Automation 列出集合中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17145902/

相关文章:

powershell - 如何使用 PowerShell 访问第三方 OLE DLL 的方法和属性?

delphi - 从函数返回 OleVariant 会导致对所包含的 IDispatch 对象产生无法释放的额外引用

java - Jar文件显示的屏幕尺寸与IntelliJ不同

java - Eclipse 未找到现有 Web 项目

ms-word - 在 Office-js 中获取范围周围的文本

VBA 将文档从 .docm 保存(并转换)为 .docx

visual-c++ - 在 OLE Word 自动化中搜索和替换 - 如何覆盖页眉和页脚?

Java - 绘制动态形状轮廓

java - Aspose cells java,如何从表/ListObject创建图像

c# - 将图像从 xml 中的字节转换为 word 文档