Page 1 of 1

Execute OpenOffice Calc macro using C#

Posted: Thu Jun 21, 2018 12:04 pm
by Subramaniyan
How to execute Open office calc macro using C#

Re: Execute OpenOffice Calc macro using C#

Posted: Thu Jun 21, 2018 5:29 pm
by Zizi64
Please give us some more details about your task.

Re: Execute OpenOffice Calc macro using C#

Posted: Fri Jun 22, 2018 5:18 am
by Subramaniyan
I am working on some Open office automation project.I created one open office file with macro.In My c# project using process.Start("filename.ods").I will be open that calc spreadsheet. After that i don't have any idea to execute my macro.Kindly help me how to execute this macro using c#.Net code.

Re: Execute OpenOffice Calc macro using C#

Posted: Fri Jun 22, 2018 6:24 am
by Zizi64
.I created one open office file with macro.In My c# project using process.Start("filename.ods").I will be open that calc spreadsheet. After that i don't have any idea to execute my macro.Kindly help me how to execute this macro using c#.Net code.
You can assign your macro to an event of your ODF file. For example to the event " event Document loading finished"... The assigned macro will run, when the document load procedure finished.

Or you want launch more than one macros in your file depended on some conditions/user activity?

Re: Execute OpenOffice Calc macro using C#

Posted: Sun Jul 15, 2018 4:23 pm
by ms777
Hi,

this is some code to launch OO from PowerShell. It should be easy to translate to C#. Make sure that this is run in a 32 bit process.

Code: Select all

if ([Environment]::Is64BitProcess) {
    Write-Error "this must be run from a 32 bit powershell window"
    exit
}

[System.Reflection.Assembly]::LoadWithPartialName('cli_cppuhelper')
[System.Reflection.Assembly]::LoadWithPartialName('cli_oootypes')
[System.Reflection.Assembly]::LoadWithPartialName('cli_ure')
[System.Reflection.Assembly]::LoadWithPartialName('cli_uretypes')
$localContext = [uno.util.Bootstrap]::bootstrap()

$multiComponentFactory = [unoidl.com.sun.star.uno.XComponentContext].getMethod('getServiceManager').invoke($localContext, @())
$desktop = [unoidl.com.sun.star.lang.XMultiComponentFactory].getMethod('createInstanceWithContext').invoke($multiComponentFactory, @('com.sun.star.frame.Desktop', $localContext))
$calc = [unoidl.com.sun.star.frame.XComponentLoader].getMethod('loadComponentFromURL').invoke($desktop, @('private:factory/scalc', '_blank', 0, $null))
$sheets = [unoidl.com.sun.star.sheet.XSpreadsheetDocument].getMethod('getSheets').invoke($calc, @())
$sheet = [unoidl.com.sun.star.container.XIndexAccess].getMethod('getByIndex').invoke($sheets, @(0))
$cell = [unoidl.com.sun.star.table.XCellRange].getMethod('getCellByPosition').invoke($sheet.Value, @(0,0))
[unoidl.com.sun.star.table.XCell].getMethod('setFormula').invoke($cell, @('A value in cell A1.'))
You have to modify the loadComponentFromURL line when you want to load a AO doc rather than create a new one.

For calling a Macro stored in a document by code see e.g. viewtopic.php?f=45&t=4321#p19932

Good luck,

ms777