private static String getSelectionString (Object selection) {

		StringBuilder builder = new StringBuilder();

		try {

			XServiceInfo selectionServiceInfo = As.XServiceInfo (selection);

			if (selectionServiceInfo.supportsService ("com.sun.star.text.TextRanges")) {

				XIndexAccess indexAccess = As.XIndexAccess (selection);
				XTextRange textRange = null;

				for (int i = 0; i < indexAccess.getCount(); i++) {

					textRange = As.XTextRange (indexAccess.getByIndex (i));

					try {

						XEnumeration paragraphEnumeration = OfficeUtil.enumerationFor (textRange);

						while (paragraphEnumeration.hasMoreElements()) {

							XTextContent textContent = As.XTextContent (paragraphEnumeration.nextElement());
							XServiceInfo textContentServiceInfo = As.XServiceInfo (textContent);

							if (textContentServiceInfo.supportsService ("com.sun.star.text.Paragraph")) {

								XEnumeration portionEnumeration = OfficeUtil.enumerationFor (textContent);
								
								while (portionEnumeration.hasMoreElements()) {

									Object portion = portionEnumeration.nextElement();

									XPropertySet portionProperties = As.XPropertySet (portion);

									String textPortionType = (String)portionProperties.getPropertyValue ("TextPortionType");

									if (textPortionType.equals ("Text")) {
										XTextRange portionTextRange = As.XTextRange (portion);
										String content = portionTextRange.getString();
										builder.append (content);
										if (builder.length() > Commands.MAX_DICTIONARY_SEARCH_LENGTH) {
											return builder.substring (0, Commands.MAX_DICTIONARY_SEARCH_LENGTH);
										}
									}

								}

							}

						}

					} catch (NoSuchElementException e) {
						// TODO http://www.openoffice.org/issues/show_bug.cgi?id=74054
						// Iterating over text sections inside table cells has some issues.
					}

				}

			}

		} catch (Throwable t) {
			// Invalid range exceptions can occur due to the lack of synchronisation
			// with dispose events et al. Probably better simply to ignore the
			// exception, given the risk of deadlocks in trying to synchronise the
			// whole affair properly
		}

		return builder.toString();

	}