我正在使用 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/