2 ' This file is part of the LibreOffice project.
4 ' This Source Code Form is subject to the terms of the Mozilla Public
5 ' License, v. 2.0. If a copy of the MPL was not distributed with this
6 ' file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 ' This file incorporates work covered by the following license notice:
10 ' Licensed to the Apache Software Foundation (ASF) under one or more
11 ' contributor license agreements. See the NOTICE file distributed
12 ' with this work for additional information regarding copyright
13 ' ownership. The ASF licenses this file to you under the Apache
14 ' License, Version 2.0 (the "License"); you may not use this file
15 ' except in compliance with the License. You may obtain a copy of
16 ' the License at http://www.apache.org/licenses/LICENSE-2.0 .
23 imports System
.Collections
24 imports Microsoft
.VisualBasic
25 imports unoidl
.com
.sun
.star
.lang
26 imports unoidl
.com
.sun
.star
.uno
27 imports unoidl
.com
.sun
.star
.bridge
32 Sub Main( ByVal args() As String)
33 ' If args.Length <> 2 Then
34 ' Console.WriteLine("WriterDemo takes two arguments. A file url to the office" & _
35 ' "program directory and a connection string.")
37 'Connect to a running office
38 '--------------------------------------------------
40 'Create a service manager of the remote office
41 'Dim ht As Hashtable = New Hashtable()
42 'ht.Add("SYSBINDIR", args(0))
43 Dim xContext
As XComponentContext
44 'xLocalContext = Bootstrap.defaultBootstrap_InitialComponentContext( _
45 ' args(0) & "/uno.ini", ht.GetEnumerator())
47 xContext
= Bootstrap
.bootstrap()
49 'Dim xURLResolver As XUnoUrlResolver
50 'xURLResolver = DirectCast(xLocalContext.getServiceManager(). _
51 ' createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", _
52 ' xLocalContext), XUnoUrlResolver)
54 'Dim xRemoteContext As XComponentContext
55 'xRemoteContext = DirectCast(xURLResolver.resolve( _
56 ' "uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext"), _
59 Dim xFactory
As XMultiServiceFactory
60 xFactory
= DirectCast(xContext
.getServiceManager(), _
64 Dim xDesktop
As unoidl
.com
.sun
.star
.frame
.XDesktop
65 xDesktop
= DirectCast(xFactory
.createInstance("com.sun.star.frame.Desktop"), _
66 unoidl
.com
.sun
.star
.frame
.XDesktop
)
68 'Open a new empty writer document
69 Dim xComponentLoader
As unoidl
.com
.sun
.star
.frame
.XComponentLoader
70 xComponentLoader
= DirectCast(xDesktop
, unoidl
.com
.sun
.star
.frame
.XComponentLoader
)
71 Dim arProps() As unoidl
.com
.sun
.star
.beans
.PropertyValue
= _
72 New unoidl
.com
.sun
.star
.beans
.PropertyValue(){}
73 Dim xComponent
As unoidl
.com
.sun
.star
.lang
.XComponent
74 xComponent
= xComponentLoader
.loadComponentFromURL( _
75 "private:factory/swriter", "_blank", 0, arProps
)
76 Dim xTextDocument
As unoidl
.com
.sun
.star
.text
.XTextDocument
77 xTextDocument
= DirectCast(xComponent
, unoidl
.com
.sun
.star
.text
.XTextDocument
)
80 Dim xText
As unoidl
.com
.sun
.star
.text
.XText
81 xText
= xTextDocument
.getText()
83 Dim xSimpleText
As unoidl
.com
.sun
.star
.text
.XSimpleText
84 xSimpleText
= DirectCast(xText
, unoidl
.com
.sun
.star
.text
.XSimpleText
)
86 'Create a cursor object
87 Dim xCursor
As unoidl
.com
.sun
.star
.text
.XTextCursor
88 xCursor
= xSimpleText
.createTextCursor()
91 xText
.insertString(xCursor
, "The first line in the newly created text document." _
94 'Create instance of a text table with 4 columns and 4 rows
95 Dim objTextTable
As Object
96 objTextTable
= DirectCast(xTextDocument
, unoidl
.com
.sun
.star
.lang
.XMultiServiceFactory
). _
97 createInstance("com.sun.star.text.TextTable")
98 Dim xTextTable
As unoidl
.com
.sun
.star
.text
.XTextTable
99 xTextTable
= DirectCast(objTextTable
, unoidl
.com
.sun
.star
.text
.XTextTable
)
100 xTextTable
.initialize(4, 4)
101 xText
.insertTextContent(xCursor
, xTextTable
, false)
103 'Set the table background color
104 Dim xPropertySetTable
As unoidl
.com
.sun
.star
.beans
.XPropertySet
105 xPropertySetTable
= DirectCast(objTextTable
, unoidl
.com
.sun
.star
.beans
.XPropertySet
)
106 xPropertySetTable
.setPropertyValue("BackTransparent", New uno
.Any(False))
107 xPropertySetTable
.setPropertyValue("BackColor", New uno
.Any(&Hccccff
))
110 Dim xTableRows
As unoidl
.com
.sun
.star
.table
.XTableRows
111 xTableRows
= xTextTable
.getRows()
112 Dim anyRow
As uno
.Any
113 anyRow
= DirectCast(xTableRows
, unoidl
.com
.sun
.star
.container
.XIndexAccess
).getByIndex( 0)
115 'Set a different background color for the first row
116 Dim xPropertySetFirstRow
As unoidl
.com
.sun
.star
.beans
.XPropertySet
117 xPropertySetFirstRow
= DirectCast(anyRow
.Value
, unoidl
.com
.sun
.star
.beans
.XPropertySet
)
118 xPropertySetFirstRow
.setPropertyValue("BackTransparent", New uno
.Any(False))
119 xPropertySetFirstRow
.setPropertyValue("BackColor", New uno
.Any(&H6666AA
))
121 'Fill the first table row
122 insertIntoCell("A1","FirstColumn", xTextTable
)
123 insertIntoCell("B1","SecondColumn", xTextTable
)
124 insertIntoCell("C1","ThirdColumn", xTextTable
)
125 insertIntoCell("D1","SUM", xTextTable
)
127 'Fill the remaining rows
128 xTextTable
.getCellByName("A2").setValue(22.5)
129 xTextTable
.getCellByName("B2").setValue(5615.3)
130 xTextTable
.getCellByName("C2").setValue(-2315.7)
131 xTextTable
.getCellByName("D2").setFormula("sum <A2:C2>")
133 xTextTable
.getCellByName("A3").setValue(21.5)
134 xTextTable
.getCellByName("B3").setValue (615.3)
135 xTextTable
.getCellByName("C3").setValue( -315.7)
136 xTextTable
.getCellByName("D3").setFormula( "sum <A3:C3>")
138 xTextTable
.getCellByName("A4").setValue( 121.5)
139 xTextTable
.getCellByName("B4").setValue( -615.3)
140 xTextTable
.getCellByName("C4").setValue( 415.7)
141 xTextTable
.getCellByName("D4").setFormula( "sum <A4:C4>")
143 'Change the CharColor and add a Shadow
144 Dim xPropertySetCursor
As unoidl
.com
.sun
.star
.beans
.XPropertySet
145 xPropertySetCursor
= DirectCast(xCursor
, unoidl
.com
.sun
.star
.beans
.XPropertySet
)
146 xPropertySetCursor
.setPropertyValue("CharColor", New uno
.Any(255))
147 xPropertySetCursor
.setPropertyValue("CharShadowed", New uno
.Any(true))
149 'Create a paragraph break
150 xSimpleText
.insertControlCharacter(xCursor
, _
151 unoidl
.com
.sun
.star
.text
.ControlCharacter
.PARAGRAPH_BREAK
, False)
153 'Inserting colored Text.
154 xSimpleText
.insertString(xCursor
," This is a colored Text - blue with shadow" & vbLf
, _
157 'Create a paragraph break
158 xSimpleText
.insertControlCharacter(xCursor
, _
159 unoidl
.com
.sun
.star
.text
.ControlCharacter
.PARAGRAPH_BREAK
, False)
162 Dim objTextFrame
As Object
163 objTextFrame
= DirectCast(xTextDocument
, unoidl
.com
.sun
.star
.lang
.XMultiServiceFactory
). _
164 createInstance("com.sun.star.text.TextFrame")
166 Dim xTextFrame
As unoidl
.com
.sun
.star
.text
.XTextFrame
= _
167 DirectCast(objTextFrame
, unoidl
.com
.sun
.star
.text
.XTextFrame
)
169 'Set the size of the frame
170 Dim aSize
As unoidl
.com
.sun
.star
.awt
.Size
= _
171 New unoidl
.com
.sun
.star
.awt
.Size(15000, 400)
172 DirectCast(xTextFrame
, unoidl
.com
.sun
.star
.drawing
.XShape
).setSize(aSize
)
175 Dim xPropertySetFrame
As unoidl
.com
.sun
.star
.beans
.XPropertySet
176 xPropertySetFrame
= DirectCast(xTextFrame
, unoidl
.com
.sun
.star
.beans
.XPropertySet
)
177 xPropertySetFrame
.setPropertyValue("AnchorType", New uno
.Any( _
178 GetType(unoidl
.com
.sun
.star
.text
.TextContentAnchorType
), _
179 unoidl
.com
.sun
.star
.text
.TextContentAnchorType
.AS_CHARACTER
))
182 xText
.insertTextContent(xCursor
, xTextFrame
, False)
184 'Get the text object of the frame
186 Dim xFrameText
As unoidl
.com
.sun
.star
.text
.XText
187 xFrameText
= xTextFrame
.getText()
189 Dim xFrameSimpleText
As unoidl
.com
.sun
.star
.text
.XSimpleText
190 xFrameSimpleText
= DirectCast(xFrameText
, unoidl
.com
.sun
.star
.text
.XSimpleText
)
192 'Create a cursor object
193 Dim xFrameCursor
As unoidl
.com
.sun
.star
.text
.XTextCursor
194 xFrameCursor
= xFrameSimpleText
.createTextCursor()
197 xFrameSimpleText
.insertString(xFrameCursor
, _
198 "The first line in the newly created text frame.", False)
199 xFrameSimpleText
.insertString(xFrameCursor
, _
200 vbLf
& "With this second line the height of the frame raises.", False)
202 'Create a paragraph break
203 xSimpleText
.insertControlCharacter(xFrameCursor
, _
204 unoidl
.com
.sun
.star
.text
.ControlCharacter
.PARAGRAPH_BREAK
, False)
206 'Change the CharColor and add a Shadow
207 xPropertySetCursor
.setPropertyValue("CharColor", New uno
.Any(65536))
208 xPropertySetCursor
.setPropertyValue("CharShadowed", New uno
.Any(False))
210 'Insert another string
211 xText
.insertString(xCursor
, vbLf
+ " That's all for now !!", False)
215 Sub insertIntoCell(sCellName
As String,sText
As String, _
216 xTable
As unoidl
.com
.sun
.star
.text
.XTextTable
)
217 Dim xCell
As unoidl
.com
.sun
.star
.table
.XCell
218 xCell
= xTable
.getCellByName(sCellName
)
220 Dim xSimpleTextCell
As unoidl
.com
.sun
.star
.text
.XSimpleText
221 xSimpleTextCell
= DirectCast(xCell
, unoidl
.com
.sun
.star
.text
.XSimpleText
)
223 Dim xCursor
As unoidl
.com
.sun
.star
.text
.XTextCursor
224 xCursor
= xSimpleTextCell
.createTextCursor()
226 Dim xPropertySetCursor
As unoidl
.com
.sun
.star
.beans
.XPropertySet
227 xPropertySetCursor
= DirectCast(xCursor
, unoidl
.com
.sun
.star
.beans
.XPropertySet
)
229 xPropertySetCursor
.setPropertyValue("CharColor", New uno
.Any(&Hffffff
))
230 xSimpleTextCell
.insertString(xCursor
, sText
, False)