Change image mode in Calc macro

I am trying to toggle the mode of an image (which I have anchored to a cell) to/from default and grayscale. If I record a macro while doing this, I get:

sub mode_default
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$J$10"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())

rem ----------------------------------------------------------------------
rem dispatcher.executeDispatch(document, ".uno:GrafMode", "", 0, Array())

rem ----------------------------------------------------------------------
dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "ToPoint"
args3(0).Value = "$J$12"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args3())


end sub

It looks like what I need to do is uncomment the “.uno:GrafMode” line, but I can find no documentation for it. Can anyone help?

Thank you.

The LibreOffice is not a pixelgraphic editor.

*Which version of the LO are you using? *
What menu items was activated during the recording of the macro?

*I can not find color/grayscale switch in the menu of my LO 6.1.6 Calc application. Only the Writer has such function. *
If you once CONVERT an image to grayscale, you can not trestore the colors in the future.

Maybe you can achieve this task by usage two images: one colorized and an another one what is converted to grayscale. You can flip the visibility of the two images located in same position.

Edit: I see now: there is such function on the LO 6.1.6 Calc Toolbar - but in different position. (I never used it…)

I see now: there is such thing in the Help of the LO 7.4.0:
https://help.libreoffice.org/latest/ar/text/shared/02/24020000.html
And there is such function on the toolbar in my LO Portable 7.3.5.

Maybe the API not contain yet that function…
Or the Macro recorder can not record it.

It is better to WRITE your macros based on the existing API functions - instead of recording them. The macro recorder has a very limited capability.

And install one of the excellent object inspection tools: XrayTool or MRI. Then you will able to examine the existing properties and methods of the programming objects, like the embedded images.

Thanks for your input, @Zizi64. I was originally doing as you first suggested, and toggling colorized graphic version visibility, but it seemed tedious to have two sets of graphics and position each exactly. I discovered the graphics mode function in the toolbar and thought it a more elegant solution. I’ve installed MRI, and hopefully it will assist as soon as I figure out how to use it.

Thanks again.

I am using the XrayTool.

You need load it into memory, and then just call it together with one parameter: the reference name of the object what you want to examine.

If (Not GlobalScope.BasicLibraries.isLibraryLoaded("XrayTool")) Then
 GlobalScope.BasicLibraries.LoadLibrary("XrayTool")
End If

oDoc = Thiscomponent
Xray oDoc

oSheet = etc...

Where did you find it? I’ve looked in extension manager/online, and sourceforge…? I see that in the LO Conference last year they discussed including it out of the box.

Aha! I finally found it on Bernard Marcelly web site (but seems very old version). When I am released from some other domestic chores, I will explore it. Many thanks, @Zizi64. :grinning:

Yes, I am using the ‘6.0 En’ version from 2012. It works with the latest API objects too.

You can do it like this:

Option Explicit 
' Changes the color of 1-st graphic shape on the first sheet of the Calc document.
Sub test
 Dim oImage
 oImage=ThisComponent.Sheets(0).DrawPage.GetByIndex(0)
 oImage.GraphicColorMode=1    ' 0:STANDARD 1:GREYS 2:MONO 3:WATERMARK
                              ' https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1drawing.html#a0e887cb85702e0dccbf34d7dbe40443f 
End Sub

See also here.

1 Like

Perfect. Does exactly what I need. Thanks, @sokol92, and thanks again @Zizi64 for introducing me to a valuable tool.