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