Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Merging cells in a Word table through OleAutomation
Merging cells in a Word table through OleAutomation [message #1060372] Fri, 24 May 2013 06:35
Binko Binev is currently offline Binko Binev
Messages: 20
Registered: June 2012
Junior Member
Hi all,

I have the following problem:

I use OleAutomation for generating reports. For this purpose some cells in tables are merged together. The report is produced as it should look.

But when I merge cells, the WinWord.exe process remains after the client site is closed. If I comment the merge line , the WinWord.exe process is closed as it should.

I have disposed all OleAutomations and Variants in my java code. But as it seems there is some internal stuff in the dll that does not remove the pointers created internally in the dll.

Here is the method I use to merge cells - it is a little bit bloated, as I have tried out different solution attempts:


/**
	 * Merges the range from cell1 in upper left corner to cell2 in lower right corner
	 * @param cell1Var - Variant of cell1
	 * @param cell2Var - Variant of cell2
	 * @return
	 * @throws Exception
	 */
	public static Variant mergeCells(Variant cell1Var, Variant cell2Var) throws Exception {
		Variant mergeCellsVar = null;
		Variant cell1RangeVar = OleWordTableCell.getRange(cell1Var);
		if (cell1RangeVar == null){
			return null;
		}
		int cell1RangeStart = OleWordRange.getStart(cell1RangeVar);
		Variant cell2RangeVar = OleWordTableCell.getRange(cell2Var);
		int cell2RangeEnd = OleWordRange.getEnd(cell2RangeVar);
		OleAutomation cell1Auto = cell1Var.getAutomation();
		Variant documentVar = cell1Auto.getProperty(1002/*Parent*/);
		Variant cellsToMergeRangeVar = OleWordDocument.setRange(documentVar,cell1RangeStart,cell2RangeEnd);
		OleAutomation cellsToMergeRangeAuto = cellsToMergeRangeVar.getAutomation();
		Variant selectionVar = OleWordRange.select(cellsToMergeRangeVar);
		Variant selectionCellsVar = OleWordSelection.getCells(selectionVar);
		OleAutomation cellsToMergeAuto = selectionCellsVar.getAutomation();
		int cellsToMergeCount = cellsToMergeAuto.getProperty(2/*Count*/).getInt();
		if (cellsToMergeCount > 1){
			try{
				[color=blue][/color]mergeCellsVar = cellsToMergeAuto.invoke(204/*Merge*/);
			}
			catch(Exception e){
				System.out.println();
				throw e;
			}
			finally{
				OleUtils.disposeVariant(selectionCellsVar);
				OleUtils.disposeVariant(selectionVar);
				OleUtils.disposeAutomation(cellsToMergeAuto);
				OleUtils.disposeAutomation(cellsToMergeRangeAuto);
				OleUtils.disposeVariant(cellsToMergeRangeVar);
				OleUtils.disposeVariant(mergeCellsVar);
				OleUtils.disposeVariant(cell1Var);
				OleUtils.disposeVariant(cell2Var);
				OleUtils.disposeAutomation(cell1Auto);
				OleUtils.disposeVariant(documentVar);
				OleUtils.disposeVariant(cell1RangeVar);
				OleUtils.disposeVariant(cell2RangeVar);
			}
		}
//		else if (cellsToMergeCount == 1){
//			// get item
//			Variant cellVar = cellsToMergeAuto.invoke(0, new Variant[]{new Variant(1)});
//			OleAutomation cellAuto = cellVar.getAutomation();
//			int rowIndex = cellAuto.getProperty(4).getInt();
//			int colIndex = cellAuto.getProperty(5).getInt();
//			cellAuto.setProperty(110/*FitText*/, new Variant[]{new Variant(true)});
//			
//			System.out.println("row: " + rowIndex + ",col: " + colIndex);
//			return null;
//		}
//		else{
//			return null;
//		}
		return mergeCellsVar;
	}


The problem code line is in blue. If it is commented out, the process closes, but cells are not merged.
If it is left, the cells are merged, but the process is not closed.

I want that both the cells are merged and the process is closed.

[Updated on: Fri, 24 May 2013 08:48]

Report message to a moderator

Previous Topic:GC operations not propagated to viewport on OS X
Next Topic:Detecting keys while showing a context menu
Goto Forum:
  


Current Time: Tue Jul 22 11:44:59 EDT 2014

Powered by FUDForum. Page generated in 0.01813 seconds