1*cdf0e10cSrcweir'************************************************************************* 2*cdf0e10cSrcweir' 3*cdf0e10cSrcweir' DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir' 5*cdf0e10cSrcweir' Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir' 7*cdf0e10cSrcweir' OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir' 9*cdf0e10cSrcweir' This file is part of OpenOffice.org. 10*cdf0e10cSrcweir' 11*cdf0e10cSrcweir' OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir' it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir' only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir' 15*cdf0e10cSrcweir' OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir' but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir' GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir' (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir' 21*cdf0e10cSrcweir' You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir' version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir' <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir' for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir' 26*cdf0e10cSrcweir'************************************************************************* 27*cdf0e10cSrcweir 28*cdf0e10cSrcweirOption Explicit On 29*cdf0e10cSrcweirOption Strict On 30*cdf0e10cSrcweir 31*cdf0e10cSrcweirimports System 32*cdf0e10cSrcweirimports System.Collections 33*cdf0e10cSrcweirimports Microsoft.VisualBasic 34*cdf0e10cSrcweirimports unoidl.com.sun.star.lang 35*cdf0e10cSrcweirimports unoidl.com.sun.star.uno 36*cdf0e10cSrcweirimports unoidl.com.sun.star.bridge 37*cdf0e10cSrcweirimports uno.util 38*cdf0e10cSrcweir 39*cdf0e10cSrcweirModule WriterDemo 40*cdf0e10cSrcweir 41*cdf0e10cSrcweirSub Main( ByVal args() As String) 42*cdf0e10cSrcweir' If args.Length <> 2 Then 43*cdf0e10cSrcweir' Console.WriteLine("WriterDemo takes two arguments. A file url to the office" & _ 44*cdf0e10cSrcweir' "program directory and a connection string.") 45*cdf0e10cSrcweir' End If 46*cdf0e10cSrcweir'Connect to a running office 47*cdf0e10cSrcweir'-------------------------------------------------- 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir'Create a service manager of the remote office 50*cdf0e10cSrcweir'Dim ht As Hashtable = New Hashtable() 51*cdf0e10cSrcweir'ht.Add("SYSBINDIR", args(0)) 52*cdf0e10cSrcweirDim xContext As XComponentContext 53*cdf0e10cSrcweir'xLocalContext = Bootstrap.defaultBootstrap_InitialComponentContext( _ 54*cdf0e10cSrcweir' args(0) & "/uno.ini", ht.GetEnumerator()) 55*cdf0e10cSrcweir 56*cdf0e10cSrcweirxContext = Bootstrap.bootstrap() 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir'Dim xURLResolver As XUnoUrlResolver 59*cdf0e10cSrcweir'xURLResolver = DirectCast(xLocalContext.getServiceManager(). _ 60*cdf0e10cSrcweir' createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", _ 61*cdf0e10cSrcweir' xLocalContext), XUnoUrlResolver) 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir'Dim xRemoteContext As XComponentContext 64*cdf0e10cSrcweir'xRemoteContext = DirectCast(xURLResolver.resolve( _ 65*cdf0e10cSrcweir' "uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext"), _ 66*cdf0e10cSrcweir' XComponentContext) 67*cdf0e10cSrcweir 68*cdf0e10cSrcweirDim xFactory As XMultiServiceFactory 69*cdf0e10cSrcweirxFactory = DirectCast(xContext.getServiceManager(), _ 70*cdf0e10cSrcweir XMultiServiceFactory) 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir'Create the Desktop 73*cdf0e10cSrcweirDim xDesktop As unoidl.com.sun.star.frame.XDesktop 74*cdf0e10cSrcweirxDesktop = DirectCast(xFactory.createInstance("com.sun.star.frame.Desktop"), _ 75*cdf0e10cSrcweir unoidl.com.sun.star.frame.XDesktop) 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir'Open a new empty writer document 78*cdf0e10cSrcweirDim xComponentLoader As unoidl.com.sun.star.frame.XComponentLoader 79*cdf0e10cSrcweirxComponentLoader = DirectCast(xDesktop, unoidl.com.sun.star.frame.XComponentLoader) 80*cdf0e10cSrcweirDim arProps() As unoidl.com.sun.star.beans.PropertyValue = _ 81*cdf0e10cSrcweir New unoidl.com.sun.star.beans.PropertyValue(){} 82*cdf0e10cSrcweirDim xComponent As unoidl.com.sun.star.lang.XComponent 83*cdf0e10cSrcweir xComponent = xComponentLoader.loadComponentFromURL( _ 84*cdf0e10cSrcweir "private:factory/swriter", "_blank", 0, arProps) 85*cdf0e10cSrcweirDim xTextDocument As unoidl.com.sun.star.text.XTextDocument 86*cdf0e10cSrcweirxTextDocument = DirectCast(xComponent, unoidl.com.sun.star.text.XTextDocument) 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir'Create a text object 89*cdf0e10cSrcweirDim xText As unoidl.com.sun.star.text.XText 90*cdf0e10cSrcweirxText = xTextDocument.getText() 91*cdf0e10cSrcweir 92*cdf0e10cSrcweirDim xSimpleText As unoidl.com.sun.star.text.XSimpleText 93*cdf0e10cSrcweirxSimpleText = DirectCast(xText, unoidl.com.sun.star.text.XSimpleText) 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir'Create a cursor object 96*cdf0e10cSrcweirDim xCursor As unoidl.com.sun.star.text.XTextCursor 97*cdf0e10cSrcweirxCursor = xSimpleText.createTextCursor() 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir'Inserting some Text 100*cdf0e10cSrcweirxText.insertString(xCursor, "The first line in the newly created text document." _ 101*cdf0e10cSrcweir & vbLf, false) 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir'Create instance of a text table with 4 columns and 4 rows 104*cdf0e10cSrcweirDim objTextTable As Object 105*cdf0e10cSrcweirobjTextTable= DirectCast(xTextDocument, unoidl.com.sun.star.lang.XMultiServiceFactory). _ 106*cdf0e10cSrcweir createInstance("com.sun.star.text.TextTable") 107*cdf0e10cSrcweirDim xTextTable As unoidl.com.sun.star.text.XTextTable 108*cdf0e10cSrcweirxTextTable = DirectCast(objTextTable, unoidl.com.sun.star.text.XTextTable) 109*cdf0e10cSrcweirxTextTable.initialize(4, 4) 110*cdf0e10cSrcweirxText.insertTextContent(xCursor, xTextTable, false) 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir'Set the table background color 113*cdf0e10cSrcweirDim xPropertySetTable As unoidl.com.sun.star.beans.XPropertySet 114*cdf0e10cSrcweirxPropertySetTable = DirectCast(objTextTable, unoidl.com.sun.star.beans.XPropertySet) 115*cdf0e10cSrcweirxPropertySetTable.setPropertyValue("BackTransparent", New uno.Any(False)) 116*cdf0e10cSrcweirxPropertySetTable.setPropertyValue("BackColor", New uno.Any(&Hccccff)) 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir'Get first row 119*cdf0e10cSrcweirDim xTableRows As unoidl.com.sun.star.table.XTableRows 120*cdf0e10cSrcweirxTableRows = xTextTable.getRows() 121*cdf0e10cSrcweirDim anyRow As uno.Any 122*cdf0e10cSrcweiranyRow = DirectCast(xTableRows, unoidl.com.sun.star.container.XIndexAccess).getByIndex( 0) 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir'Set a different background color for the first row 125*cdf0e10cSrcweirDim xPropertySetFirstRow As unoidl.com.sun.star.beans.XPropertySet 126*cdf0e10cSrcweirxPropertySetFirstRow = DirectCast(anyRow.Value, unoidl.com.sun.star.beans.XPropertySet) 127*cdf0e10cSrcweirxPropertySetFirstRow.setPropertyValue("BackTransparent", New uno.Any(False)) 128*cdf0e10cSrcweirxPropertySetFirstRow.setPropertyValue("BackColor", New uno.Any(&H6666AA)) 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir'Fill the first table row 131*cdf0e10cSrcweirinsertIntoCell("A1","FirstColumn", xTextTable) 132*cdf0e10cSrcweirinsertIntoCell("B1","SecondColumn", xTextTable) 133*cdf0e10cSrcweirinsertIntoCell("C1","ThirdColumn", xTextTable) 134*cdf0e10cSrcweirinsertIntoCell("D1","SUM", xTextTable) 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir'Fill the remaining rows 137*cdf0e10cSrcweirxTextTable.getCellByName("A2").setValue(22.5) 138*cdf0e10cSrcweirxTextTable.getCellByName("B2").setValue(5615.3) 139*cdf0e10cSrcweirxTextTable.getCellByName("C2").setValue(-2315.7) 140*cdf0e10cSrcweirxTextTable.getCellByName("D2").setFormula("sum <A2:C2>") 141*cdf0e10cSrcweir 142*cdf0e10cSrcweirxTextTable.getCellByName("A3").setValue(21.5) 143*cdf0e10cSrcweirxTextTable.getCellByName("B3").setValue (615.3) 144*cdf0e10cSrcweirxTextTable.getCellByName("C3").setValue( -315.7) 145*cdf0e10cSrcweirxTextTable.getCellByName("D3").setFormula( "sum <A3:C3>") 146*cdf0e10cSrcweir 147*cdf0e10cSrcweirxTextTable.getCellByName("A4").setValue( 121.5) 148*cdf0e10cSrcweirxTextTable.getCellByName("B4").setValue( -615.3) 149*cdf0e10cSrcweirxTextTable.getCellByName("C4").setValue( 415.7) 150*cdf0e10cSrcweirxTextTable.getCellByName("D4").setFormula( "sum <A4:C4>") 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir'Change the CharColor and add a Shadow 153*cdf0e10cSrcweirDim xPropertySetCursor As unoidl.com.sun.star.beans.XPropertySet 154*cdf0e10cSrcweirxPropertySetCursor = DirectCast(xCursor, unoidl.com.sun.star.beans.XPropertySet) 155*cdf0e10cSrcweirxPropertySetCursor.setPropertyValue("CharColor", New uno.Any(255)) 156*cdf0e10cSrcweirxPropertySetCursor.setPropertyValue("CharShadowed", New uno.Any(true)) 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir'Create a paragraph break 159*cdf0e10cSrcweirxSimpleText.insertControlCharacter(xCursor, _ 160*cdf0e10cSrcweir unoidl.com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False) 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir'Inserting colored Text. 163*cdf0e10cSrcweirxSimpleText.insertString(xCursor," This is a colored Text - blue with shadow" & vbLf, _ 164*cdf0e10cSrcweir False) 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir'Create a paragraph break 167*cdf0e10cSrcweirxSimpleText.insertControlCharacter(xCursor, _ 168*cdf0e10cSrcweir unoidl.com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False) 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir'Create a TextFrame. 171*cdf0e10cSrcweirDim objTextFrame As Object 172*cdf0e10cSrcweirobjTextFrame = DirectCast(xTextDocument, unoidl.com.sun.star.lang.XMultiServiceFactory). _ 173*cdf0e10cSrcweir createInstance("com.sun.star.text.TextFrame") 174*cdf0e10cSrcweir 175*cdf0e10cSrcweirDim xTextFrame As unoidl.com.sun.star.text.XTextFrame = _ 176*cdf0e10cSrcweir DirectCast(objTextFrame, unoidl.com.sun.star.text.XTextFrame) 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir'Set the size of the frame 179*cdf0e10cSrcweirDim aSize As unoidl.com.sun.star.awt.Size = _ 180*cdf0e10cSrcweir New unoidl.com.sun.star.awt.Size(15000, 400) 181*cdf0e10cSrcweirDirectCast(xTextFrame, unoidl.com.sun.star.drawing.XShape).setSize(aSize) 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir'Set anchortype 184*cdf0e10cSrcweirDim xPropertySetFrame As unoidl.com.sun.star.beans.XPropertySet 185*cdf0e10cSrcweirxPropertySetFrame = DirectCast(xTextFrame, unoidl.com.sun.star.beans.XPropertySet) 186*cdf0e10cSrcweirxPropertySetFrame.setPropertyValue("AnchorType", New uno.Any( _ 187*cdf0e10cSrcweir GetType(unoidl.com.sun.star.text.TextContentAnchorType), _ 188*cdf0e10cSrcweir unoidl.com.sun.star.text.TextContentAnchorType.AS_CHARACTER)) 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir'insert the frame 191*cdf0e10cSrcweirxText.insertTextContent(xCursor, xTextFrame, False) 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir'Get the text object of the frame 194*cdf0e10cSrcweir 195*cdf0e10cSrcweirDim xFrameText As unoidl.com.sun.star.text.XText 196*cdf0e10cSrcweirxFrameText = xTextFrame.getText() 197*cdf0e10cSrcweir 198*cdf0e10cSrcweirDim xFrameSimpleText As unoidl.com.sun.star.text.XSimpleText 199*cdf0e10cSrcweirxFrameSimpleText = DirectCast(xFrameText, unoidl.com.sun.star.text.XSimpleText) 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir'Create a cursor object 202*cdf0e10cSrcweirDim xFrameCursor As unoidl.com.sun.star.text.XTextCursor 203*cdf0e10cSrcweirxFrameCursor = xFrameSimpleText.createTextCursor() 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir'Inserting some Text 206*cdf0e10cSrcweirxFrameSimpleText.insertString(xFrameCursor, _ 207*cdf0e10cSrcweir "The first line in the newly created text frame.", False) 208*cdf0e10cSrcweirxFrameSimpleText.insertString(xFrameCursor, _ 209*cdf0e10cSrcweir vbLf & "With this second line the height of the frame raises.", False) 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir'Create a paragraph break 212*cdf0e10cSrcweirxSimpleText.insertControlCharacter(xFrameCursor, _ 213*cdf0e10cSrcweir unoidl.com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False) 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir'Change the CharColor and add a Shadow 216*cdf0e10cSrcweirxPropertySetCursor.setPropertyValue("CharColor", New uno.Any(65536)) 217*cdf0e10cSrcweirxPropertySetCursor.setPropertyValue("CharShadowed", New uno.Any(False)) 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir'Insert another string 220*cdf0e10cSrcweirxText.insertString(xCursor, vbLf + " That's all for now !!", False) 221*cdf0e10cSrcweir 222*cdf0e10cSrcweirEnd Sub 223*cdf0e10cSrcweir 224*cdf0e10cSrcweirSub insertIntoCell(sCellName As String,sText As String, _ 225*cdf0e10cSrcweir xTable As unoidl.com.sun.star.text.XTextTable) 226*cdf0e10cSrcweir Dim xCell As unoidl.com.sun.star.table.XCell 227*cdf0e10cSrcweir xCell = xTable.getCellByName(sCellName) 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir Dim xSimpleTextCell As unoidl.com.sun.star.text.XSimpleText 230*cdf0e10cSrcweir xSimpleTextCell = DirectCast(xCell, unoidl.com.sun.star.text.XSimpleText) 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir Dim xCursor As unoidl.com.sun.star.text.XTextCursor 233*cdf0e10cSrcweir xCursor = xSimpleTextCell.createTextCursor() 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir Dim xPropertySetCursor As unoidl.com.sun.star.beans.XPropertySet 236*cdf0e10cSrcweir xPropertySetCursor = DirectCast(xCursor, unoidl.com.sun.star.beans.XPropertySet) 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir xPropertySetCursor.setPropertyValue("CharColor", New uno.Any(&Hffffff)) 239*cdf0e10cSrcweir xSimpleTextCell.insertString(xCursor, sText, False) 240*cdf0e10cSrcweirEnd Sub 241*cdf0e10cSrcweir 242*cdf0e10cSrcweirEnd Module 243