Hi,
it is a misbelieve that you cannot access the methods of the interfaces where DirectCast or CType fails. Just go through the standard .NET reflection method. See the xray subroutine in the code below. XRay is very helpful when programming. Of course XRay must be installed prior to usage.
Image insertion (your point 2) is at the end of the main sub.
As I am allergic against VB I got lots of red spots on my skin when typing this. I need to recover now, so be patient when you need additional help …
Good luck,
ms777
'Option Explicit On
'Option Strict On
imports System
imports System.Collections
imports Microsoft.VisualBasic
imports unoidl.com.sun.star.lang
imports unoidl.com.sun.star.uno
imports unoidl.com.sun.star.bridge
imports uno.util
Module WriterDemo
Dim xContext As XComponentContext
Dim xServiceManager As XMultiComponentFactory 'this is XMultiServiceFactory in the OO example
Sub Main( ByVal args() As String)
System.Console.WriteLine("Start")
xContext = Bootstrap.bootstrap()
xServiceManager = DirectCast(xContext.getServiceManager(), XMultiComponentFactory)
'Create the Desktop
Dim xDesktop As unoidl.com.sun.star.frame.XDesktop = DirectCast(xServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xContext), unoidl.com.sun.star.frame.XDesktop)
'Open a new empty writer document
Dim xComponentLoader As unoidl.com.sun.star.frame.XComponentLoader = DirectCast(xDesktop, unoidl.com.sun.star.frame.XComponentLoader)
Dim arProps() As unoidl.com.sun.star.beans.PropertyValue = New unoidl.com.sun.star.beans.PropertyValue(){}
Dim xComponent As unoidl.com.sun.star.lang.XComponent = xComponentLoader.loadComponentFromURL( "private:factory/swriter", "_blank", 0, arProps)
Dim xTextDocument As unoidl.com.sun.star.text.XTextDocument = DirectCast(xComponent, unoidl.com.sun.star.text.XTextDocument)
'Create a text object
Dim xText As unoidl.com.sun.star.text.XText = xTextDocument.getText()
Dim xSimpleText As unoidl.com.sun.star.text.XSimpleText = DirectCast(xText, unoidl.com.sun.star.text.XSimpleText)
'Create a cursor object
Dim xCursor As unoidl.com.sun.star.text.XTextCursor = xSimpleText.createTextCursor()
'Inserting some Text
xText.insertString(xCursor, "The first line in the newly created text document." & vbLf, false)
' xray(xText)
' here starts the image insertion
Dim image = DirectCast(xComponent, XMultiServiceFactory).createInstance("com.sun.star.text.GraphicObject")
DirectCast(image, unoidl.com.sun.star.beans.XPropertySet).setPropertyValue("GraphicURL", New uno.Any("file:///C:/Users/Martin/Desktop/pic.png"))
DirectCast(image, unoidl.com.sun.star.beans.XPropertySet).setPropertyValue("AnchorType", New uno.Any(GetType(unoidl.com.sun.star.text.TextContentAnchorType), unoidl.com.sun.star.text.TextContentAnchorType.AS_CHARACTER))
DirectCast(image, unoidl.com.sun.star.beans.XPropertySet).setPropertyValue("Width", New uno.Any(2500))
DirectCast(image, unoidl.com.sun.star.beans.XPropertySet).setPropertyValue("Height", New uno.Any(2500))
xray(image)
xText.insertTextContent(xCursor, image, False)
End Sub
Sub xray(target)
Dim mspf = xServiceManager.createInstanceWithContext("com.sun.star.script.provider.MasterScriptProviderFactory", xContext)
Dim tSPF = GetType(unoidl.com.sun.star.script.provider.XScriptProviderFactory)
Dim mCreateScriptProvider = tSPF.GetMethod("createScriptProvider")
Dim scriptProvider = mCreateScriptProvider.invoke(mspf, New Object(){New uno.Any("")})
Dim tXScriptProvider = GetType(unoidl.com.sun.star.script.provider.XScriptProvider)
Dim mGetScript = tXScriptProvider.GetMethod("getScript")
Dim script = mGetScript.invoke(scriptProvider, New Object(){"vnd.sun.star.script:XrayTool._Main.Xray?language=Basic&location=application"})
Dim tXScript = GetType(unoidl.com.sun.star.script.provider.XScript)
Dim mInvoke = tXScript.GetMethod("invoke")
Dim args1 as uno.Any() = New uno.Any(){New uno.Any(GetType(Object), target)}
Dim args2 as Int16() = New Int16(){}
Dim args3 as uno.Any() = New uno.Any(){}
mInvoke.invoke(script, New Object() {args1, args2, args3})
End Sub
End Module