Execute OpenOffice Calc macro using C#

Java, C++, C#, Delphi, ??? - Using the UNO bridges

Execute OpenOffice Calc macro using C#

Postby Subramaniyan » Thu Jun 21, 2018 12:04 pm

How to execute Open office calc macro using C#
oPENoFFICE 4.1.2
Subramaniyan
 
Posts: 2
Joined: Thu Jun 21, 2018 11:59 am

Re: Execute OpenOffice Calc macro using C#

Postby Zizi64 » Thu Jun 21, 2018 5:29 pm

Please give us some more details about your task.
Tibor Kovacs, Hungary; LO4.4.7, LO6.1.1 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.1.2 and AOO4.1.5
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.
User avatar
Zizi64
Volunteer
 
Posts: 7481
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Execute OpenOffice Calc macro using C#

Postby Subramaniyan » Fri Jun 22, 2018 5:18 am

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
Subramaniyan
 
Posts: 2
Joined: Thu Jun 21, 2018 11:59 am

Re: Execute OpenOffice Calc macro using C#

Postby Zizi64 » Fri Jun 22, 2018 6:24 am

.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; LO4.4.7, LO6.1.1 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.1.2 and AOO4.1.5
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.
User avatar
Zizi64
Volunteer
 
Posts: 7481
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Execute OpenOffice Calc macro using C#

Postby ms777 » Sun Jul 15, 2018 4:23 pm

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   Expand viewCollapse view
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. https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=4321#p19932

Good luck,

ms777
ms777
Volunteer
 
Posts: 131
Joined: Mon Oct 08, 2007 1:33 am


Return to External Programs

Who is online

Users browsing this forum: No registered users and 2 guests