Page 1 of 1

[Solved] New doc from template with loadComponentFromURL()

PostPosted: Tue Sep 09, 2014 4:07 pm
by Mat
Hello

I'm new to OO and just getting my hands dirty a little bit to see if we could use is professionally. We think of producing reports with OpenOffice.
What I want to do is the following: Cause an application to launch OpenOffice and make a new document from a OpenOffice-template. I have OpenOffice 4.1.0 on Windows 7 and I have the template file ready. Currently I try to do that in Excel VBA editor. I also tried it with a python interpreter with no luck either.
Herer's the code (VBA) that doesn't work:

Code: Select all   Expand viewCollapse view
' Compute Template path
templatePath = "c:\Users\mat\Desktop\Report_OoTest.ots"
templatePath = Replace(templatePath, "\", "/")
templatePath = Replace(templatePath, ":", "|")
templatePath = "file:///" & templatePath

' Create service manager
Set oServiceManager = CreateObject("com.sun.star.ServiceManager")

' Do some magic
Set oDesktop = oServiceManager.createInstance("com.sun.star.frame.Desktop")

set oFileProperties(0) = oServiceManager.Bridge_GetStruct("com.sun.star.beans.NamedValue")
oFileProperties(0).Name = "AsTemplate"
oFileProperties(0).Value = True

msgbox oFileProperties(0).Name & ", " & oFileProperties(0).Value

' Make new file from templatefile
Set oDoc = oDesktop.loadComponentFromURL(templatePath, "_blank", 0, oFileProperties)


The messagebox shows "AsTemplate, True", as one would expect. But on the last line, where I loadComponentFromURL it throws this error:
[automation bridge]com.sun.star.script.CannotConvertException in UnoConversionUtilities<T>::variantToAny! Message: value is not of same or derived type!

I suspect, that there is something wrong with the datatype of oFileProperties, but I just can't fix it :crazy:

Any ideas anyone? I keep trying with python so long

Re: New document from template with loadComponentFromURL()

PostPosted: Tue Sep 09, 2014 6:17 pm
by FJCC
Try
Code: Select all   Expand viewCollapse view
set oFileProperties(0) = oServiceManager.Bridge_GetStruct("com.sun.star.beans.PropertyValue")

In the help I see
loadComponentFromURL
::com::sun::star::lang::XComponent
loadComponentFromURL( [in] string URL,
[in] string TargetFrameName,
[in] long SearchFlags,
[in] sequence< ::com::sun::star::beans::PropertyValue > Arguments )

Re: New document from template with loadComponentFromURL()

PostPosted: Wed Sep 10, 2014 4:51 pm
by Mat
Thanks for the quick reply FJCC!
Using a PropertyValue instead of a NamedValue did the trick.
Combining code snippets without much understanding can provide a jump start, but also has it's disadvantages...
Thanks and greetings!