Execute OpenOffice Calc macro using C#

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
Subramaniyan
Posts: 2
Joined: Thu Jun 21, 2018 11:59 am

Execute OpenOffice Calc macro using C#

Post by Subramaniyan »

How to execute Open office calc macro using C#
oPENoFFICE 4.1.2
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Execute OpenOffice Calc macro using C#

Post by Zizi64 »

Please give us some more details about your task.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Subramaniyan
Posts: 2
Joined: Thu Jun 21, 2018 11:59 am

Re: Execute OpenOffice Calc macro using C#

Post 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.
oPENoFFICE 4.1.2
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Execute OpenOffice Calc macro using C#

Post 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?
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: Execute OpenOffice Calc macro using C#

Post 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
Post Reply