[solved] Open a calc document with macro ACTIVATED

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
MarcoG
Posts: 22
Joined: Thu Apr 12, 2012 5:08 pm
Location: Italia

[solved] Open a calc document with macro ACTIVATED

Post by MarcoG »

Ciao

I open a calc document in C# in this way:

Code: Select all

  XComponentContext oStrap = uno.util.Bootstrap.bootstrap();                                       
  XMultiServiceFactory oServMan = (XMultiServiceFactory)oStrap.getServiceManager();        
  XComponentLoader oDesk = (XComponentLoader)oServMan.createInstance("com.sun.star.frame.Desktop");
  string fileName = textBox1.Text;
  string fileNameUrl = "file:///" + fileName.Replace("\\", "/");
  PropertyValue[] propVals = new PropertyValue[0];
  XComponent oDoc = oDesk.loadComponentFromURL(fileNameUrl, "_default", 0, propVals);
  XSpreadsheets oSheets = ((XSpreadsheetDocument)oDoc).getSheets();
The file has some macro written in BASIC and linked to a button.
The macro doesn't work when I press the button, nothing happens. I can't run any macro.
May be is due to the way I've opened the file. Have you got any tip?

Grazie
Marco
Last edited by RoryOF on Fri May 11, 2012 10:13 am, edited 2 times in total.
Reason: Added green tick [RoryOF, Moderator]
LibreOffice 3.5.2 - Windows Vista SP2
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Open a calc document with macro ACTIVATED

Post by Villeroy »

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
MarcoG
Posts: 22
Joined: Thu Apr 12, 2012 5:08 pm
Location: Italia

Re: Open a calc document with macro ACTIVATED

Post by MarcoG »

Thanks.

I've done in C#:

Code: Select all

  PropertyValue[] propVals = new PropertyValue[1];
  propVals[0].Name = "MacroExecutionMode";
  propVals[0].Value = unoidl.com.sun.star.document.MacroExecMode.ALWAYS_EXECUTE;
but the third lines give me an error.
propvals[0].value wants a uno.any type and the "unoidl.com.sun.star.document.MacroExecMode.ALWAYS_EXECUTE" is a short...

If you want to give me another tip, thanks a lot!

ciao
Marco
LibreOffice 3.5.2 - Windows Vista SP2
User avatar
karolus
Volunteer
Posts: 1158
Joined: Sat Jul 02, 2011 9:47 am

Re: Open a calc document with macro ACTIVATED

Post by karolus »

Hi
try:

Code: Select all

propVals[0].Value = 2 ;
http://www.openoffice.org/api/docs/comm ... YS_EXECUTE


Karo
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
MarcoG
Posts: 22
Joined: Thu Apr 12, 2012 5:08 pm
Location: Italia

Re: Open a calc document with macro ACTIVATED

Post by MarcoG »

karolus wrote:Hi
try:

Code: Select all

propVals[0].Value = 2 ;
http://www.openoffice.org/api/docs/comm ... YS_EXECUTE

Karo
thanks, but it doesn't work. 2 is "int" and C# wants "uno.any".
I can't understand how to convert a "short" to a "uno.any" type.
I think this is the problem.
At pag. 76 of Developer's guide is written "If you need to set the value of such a PropertyValue struct, you must assign an any type... how this is done depends on your language"

ciao
Last edited by MarcoG on Thu May 10, 2012 5:31 pm, edited 1 time in total.
LibreOffice 3.5.2 - Windows Vista SP2
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Open a calc document with macro ACTIVATED

Post by Charlie Young »

MarcoG wrote:
karolus wrote:Hi
try:

Code: Select all

propVals[0].Value = 2 ;
http://www.openoffice.org/api/docs/comm ... YS_EXECUTE

Karo
thanks, but it doesn't work. 2 is "int" and C# wants "uno.any".
I can't understand why C# wants an "uno.any" type and not a "short".

ciao
I don't know how it works in c#, but in c++, you assign various types to an Any using an overloaded shift left assignment.

Code: Select all

propVals[0].Value <<=  (sal_Int16) com::sun::star::document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN;
Note also the cast from short to sal_int16. Some such may also be necessary in c#.
Apache OpenOffice 4.1.1
Windows XP
MarcoG
Posts: 22
Joined: Thu Apr 12, 2012 5:08 pm
Location: Italia

Re: Open a calc document with macro ACTIVATED

Post by MarcoG »

Charlie Young wrote:
MarcoG wrote:
karolus wrote:Hi

I don't know how it works in c#, but in c++, you assign various types to an Any using an overloaded shift left assignment.

Code: Select all

propVals[0].Value <<=  (sal_Int16) com::sun::star::document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN;
Note also the cast from short to sal_int16. Some such may also be necessary in c#.
thanks a lot, I still look for a solution in C#. Tips? :?
LibreOffice 3.5.2 - Windows Vista SP2
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Open a calc document with macro ACTIVATED

Post by Charlie Young »

I'm groping in the dark here, does this work?

Code: Select all

uno.Any a = new uno.Any(4);
propVals[0].Value = a;
Note that

com::sun::star::document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN = 4
Apache OpenOffice 4.1.1
Windows XP
MarcoG
Posts: 22
Joined: Thu Apr 12, 2012 5:08 pm
Location: Italia

Re: Open a calc document with macro ACTIVATED

Post by MarcoG »

Ciao e thanks for your help!

I've written:

Code: Select all

            PropertyValue[] propVals = new PropertyValue[1];   
            propVals[0] = new PropertyValue();                  
            propVals[0].Name = "MacroExecutionMode";
            propVals[0].Value = new uno.Any(4); // (unoidl.com.sun.star.document.MacroExecMode.ALWAYS_EXECUTE_NO_WARN); 
            XComponent oDoc = oDesk.loadComponentFromURL(fileNameUrl, "_default", 0, propVals);
            XSpreadsheets oSheets = ((XSpreadsheetDocument)oDoc).getSheets();
There are no errors but I can't run a macro in the opened file yet. I don't know why...

MArco
LibreOffice 3.5.2 - Windows Vista SP2
MarcoG
Posts: 22
Joined: Thu Apr 12, 2012 5:08 pm
Location: Italia

Re: Open a calc document with macro ACTIVATED

Post by MarcoG »

Now it works!!!!

"4" has to be short!

Code: Select all

propVals[0].Value = new uno.Any((short)4);
Thanks a lot!
MArco
LibreOffice 3.5.2 - Windows Vista SP2
Post Reply