Hi Florian,
From the "Useful Macro Information For OpenOffice"
By Andrew Pitonyak
It's in Office Basic, but you can translate that to C++.
=========================================================
7.22.2 Setting the optimal column width
My book, OpenOffice.org Macros Explained, discusses text tables at some
length. It does not discuss how to set the optimal column width,
however. In a Calc document, you set the OptimalWidth column property to
True. There is no simple solution for a text table.
Tip: See pages 310 – 311 in my book for more information on text tables.
The GUI provides a method that can set the width of a column based on
the location of the text cursor, or the portion of the text table that
is selected. This method is available by using a dispatch. My book
explains how to select areas in a text table, so I will not repeat that
discussion here. The macro in Listing 7.54 selects an entire text table
and then sets the column width for the entire text table.
Listing 7.54: Set an entire table for optimal column width.
Sub SetTableOptimumWidth
Dim oDispHelper 'Dispatch helper
Dim oFrame 'Current window frame.
Dim oTable 'First table in the document.
Dim oVCursor 'The view cursor
oTable = ThisComponent.getTextTables().getByIndex(0)
ThisComponent.getCurrentController().select(oTable)
oVCursor = ThisComponent.getCurrentController().getViewCursor()
oVCursor.gotoEnd(True)
oVCursor.gotoEnd(True)
oFrame = ThisComponent.CurrentController.Frame
oDispHelper = createUnoService("com.sun.star.frame.DispatchHelper")
oDispHelper.executeDispatch(oFrame, ".uno:SetOptimalColumnWidth", "",
0, Array())
End Sub
=========================================================
Best Regards.
-------------------------------------------------------
André B. Derraik
Consultor
Tecgraf/PUC-Rio http://www.tecgraf.puc-rio.br
[EMAIL PROTECTED] Tel.: +55-21-2512-8428
Florian Limberger wrote:
Hi,
So is there another way to set the "OptimalWidth" property
programmatically ?
cheers
Flo
Joachim Lingner wrote:
Hi,
The API does not support columns. As I was told, the tables are
organized in rows and cells. That would explain, why the Any.Value
cannot be casted to a XPropertySet, because it is not implemented.
This does not work in Java either.
So the Developer's Guide is right, saying that getByIndex supporty
XInterface only.
--Joachim
Florian Limberger wrote:
Hi!
> Have you checked the type within the Any:
>
> Type t = Any.Type;
--
System.Type type = cols.getByIndex(i).Type;
Console.WriteLine(type.ToString());
--
Yields: System.Object
OOo2 did not install anything into the assembly cache on my pc. So
I'm using the assemblies in the ./OpenOffice.org
1.9.109/program/assembly directory. So far everything has worked
correctly (Document creation, text insertion, table insertion, etc..)
except setting properties of the XTableColumns elements.
Is it for sure, that it's possible to access the PropertySet of the
TextTable-Column?
(OO-Dev-Manual "Text Documents" under section "7.3.4 Tables", where
the Methods in XTabelColumns are described, says, that getByIndex()
supports the XInterface only. Does this mean, that I can not access
the XPopertySet?)
Here comes an example-code:
----------------------------------------------------------------------------
using System;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.text;
using unoidl.com.sun.star.table;
using unoidl.com.sun.star.beans;
namespace OpenOfficeWrapper
{
class Test
{
static void Main(string[] args)
{
unoidl.com.sun.star.lang.XMultiServiceFactory
ooMSFactory
=
(XMultiServiceFactory)uno.util.Bootstrap.bootstrap().getServiceManager();
XComponentLoader aLoader = (XComponentLoader)
ooMSFactory.createInstance(
"com.sun.star.frame.Desktop" );
XComponent xComponent = aLoader.loadComponentFromURL(
"private:factory/swriter", "_blank", 0,
new unoidl.com.sun.star.beans.PropertyValue[0] );
XTextDocument ooDoc = (XTextDocument)xComponent;
XMultiServiceFactory docFactory =
(XMultiServiceFactory)ooDoc;
XTextTable xTable = (XTextTable)
docFactory.createInstance("com.sun.star.text.TextTable");
XNamed name = (XNamed)xTable;
name.setName("TestTable");
xTable.initialize(2,2);
XText xText = ooDoc.getText();
xText.insertTextContent(xText.getStart(),xTable,false);
XText xCellText = (XText)xTable.getCellByName("A1");
xCellText.setString("A1");
xCellText = (XText)xTable.getCellByName("B1");
xCellText.setString("B1");
XTableColumns cols = xTable.getColumns();
for(int i=0;i<cols.getCount();i++)
{
XPropertySet props =
(XPropertySet)cols.getByIndex(i).Value;
props.setPropertyValue("OptimalWidth",new
uno.Any(true));
}
}
}
}
----------------------------------------------------------------------------
Joachim Lingner wrote:
Have you checked the type within the Any:
Type t = Any.Type;
If it is XPropertySet type, then there seems to be bug. Could you
then provide a stripped down (but still buildable) example that lets
me reproduce this problem?
BTW, I assume you are NOT using the assemblies which were once
provided through the udk.openoffice.org website. Instead one needs
to use the cli-uno bridge and assemblies which are automatically
installed with OOo2.
Joachim
Florian Limberger wrote:
Hi,
I'm using C# with cli-uno to control OO. I want to set the
column-property "OptimalWidth" of a TextTable to true.
Casting the column to a XPropertySet does not work (invalid cast).
Does anyone know, how to set the OptimalWidth of a whole TextTable ?
cheers
Flo
----------------------------------------------------------------
XTextTable table = null;
XMultiServiceFactory factory = (XMultiServiceFactory)doc;
table = (XTextTable)
factory.createInstance("com.sun.star.text.TextTable");
XTableColumns cols = table.getColumns();
int count = cols.getCount();
for(int i=0;i<cols.getCount();i++)
{
XPropertySet props = (XPropertySet)cols.getByIndex(i).Value;
props.setPropertyValue("OptimalWidth",new uno.Any(true));
}
----------------------------------------------------------------
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]