Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » [Word Automation] How to get an item from a collection?(How to list items of collections like Sections or Paragraphs from a Word document)
icon7.gif  [Word Automation] How to get an item from a collection? [message #1064099] Mon, 17 June 2013 15:50 Go to next message
Gary Long is currently offline Gary LongFriend
Messages: 4
Registered: June 2013
Junior Member
Hi Smile

I'm using the SWT OLE api to manipulate a word document inside an Eclipse RCP. I'm trying to list all the sections of my document because I would like to get only the body section. I already wrote the following code:

                ...
		OleAutomation active = activeDocument.getAutomation();
		if(active!=null){
			int sectionsId = getId(active, "Sections");
			if(sectionsId > 0){
				Variant varSections = active.getProperty(sectionsId);
				if(varSections!=null){
					OleAutomation sections = varSections.getAutomation();
					if(sections!=null){
						int itemActionId = getId(sections, "Item");
						if(itemActionId > 0) {
							Variant sectionsList = sections.getProperty(itemActionId);
						}
					}
				}
			}
		}


itemActionId is equal to zero which I think means that the property or method does not exist for the specified OleAutomation.

According to the MSDN documentation, Sections is a Collection and I should be able to invoke the Item method to list items of this collection but I'm stuck at this point. I don't know how to correctly do it Sad

ps: sorry I can't provide a link to the MSDN page for Sections since I'm new to the forum (limit of 5 posts)
Re: [Word Automation] How to get an item from a collection? [message #1064284 is a reply to message #1064099] Tue, 18 June 2013 14:36 Go to previous message
Gary Long is currently offline Gary LongFriend
Messages: 4
Registered: June 2013
Junior Member
Hi again,

I found the solution so I'll share it with you. In fact, I was wrong when I thought zero was an incorrect id for a method. For collections, it corresponds to the Item method id. So to use the Item method of a collection (like Sections or Paragraphs) just do:

...
OleAutomation active = activeDocument.getAutomation();
if(active!=null){
	int[] paragraphsId = getId(activeDocument, "Paragraphs");
	if(paragraphsId != null) {
		Variant vParagraphs = active.getProperty(paragraphsId[0]);
		if(vParagraphs != null){
			OleAutomation paragraphs = vParagraphs.getAutomation();
			if(paragraphs!=null){
				int countId = getId(paragraphs, "Count");
				if(countId > 0) {
					Variant count = paragraphs.getProperty(countId);
					if(count!=null){
						int numberOfParagraphs = count.getInt();
						for(int i = 0 ; 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;
					}
				}
			}
		}
	}
}


The getId(OleAutomation auto, String name) method is just a wrapper for OleAutomation.getIDsOfNames(String[] names) Smile
Previous Topic:StyledText embed controls
Next Topic:Bolding selected TreeItem (answered)
Goto Forum:
  


Current Time: Thu Apr 25 05:32:05 GMT 2024

Powered by FUDForum. Page generated in 0.03110 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top