Calc macro - selection by keyboard not recorded

Although reasonably experienced using calc, I haven't used macros much.

I am trying to write a macro to make the first character in a cell bold.
Enter "abc" into cell A1. Record the following keystrokes as a macro:
F2, home, shift->, control-B, enter.
This series of keystrokes does what I want, but running the resulting macro does nothing.
The code generated is:
sub djl1
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 ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SetInputMode", "", 0, Array())

rem ----------------------------------------------------------------------
dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "Bold"
args2(0).Value = true

dispatcher.executeDispatch(document, ".uno:Bold", "", 0, args2())

end sub

The keystrokes home and shift-> appear not to have been recorded: this appears to contradict the explicit advice in Help->Recording a Macro "Selections are recorded only if they are done by using the keyboard (cursor travelling), but not when the mouse is used"

What am I doing wrong please?

David Lynch

4.2.4.2 on Windows 8

Hi :slight_smile:
I only know that the best documentation appears to be Andrew Pitonyak's
guide;
https://wiki.documentfoundation.org/Documentation/Other_Documentation_and_Resources#Programmers

I am not sure if that will help for the specific issues you raise but it
might help you get to grips with macros faster or more easily or more
soundly or something.

Hopefully someone else on this list might be able to help with the specific
issues!
Good luck and regard from
Tom :slight_smile:

<snip>

Many thanks Andrew. I had simplified what I wanted: your code will, I think, enable me to complete the task.

David Lynch

Glad it helped!

david_lynch <david_lynch@blueyonder.co.uk> posted to users@global.libreoffice.org at 0:23 on Sun, 13 Jul 2014 about Calc macro - selection by keyboard not recorded:

Although reasonably experienced using calc, I haven't used macros much.

<snip>
What am I doing wrong please?

David Lynch

4.2.4.2 on Windows 8

You are doing nothing wrong.... The macro recorder fails to record
certain actions and should be reworked from the start, but, that is
not likely to happen (sadly). If you want to set the first character
to bold regardless of what you have selected, you could run something
like this (your welcome):

<snip>

What did Andrew suggest? As far as I can tell, the snipped suggestion never made it to this list.

[RANT]

This is a perfect example of why it is stupid to not post ordinary replies to this list!

[/RANT]

Many thanks Andrew. I had simplified what I wanted: your code will, I

think, enable me to complete the task.

Such rants are best not posted at all. Andrew did sent his replies to the list.

David Lynch

Hi )
Such rants are best sent to
postmaster@documentfoundation.org
and maybe the "discuss" mailing list.
Regards from
Tom :slight_smile:

Hi :slight_smile:
It wasn't really a rant. He just raised a valid point, that it is too easy
for answers to be taken off-list and that has a knock-on effect wrt the
usefulness of the archives.
Regards from
Tom :slight_smile:

I generally send only to the list. This is what you missed if it did not make it.

You are doing nothing wrong.... The macro recorder fails to record certain actions and should be reworked from the start, but, that is not likely to happen (sadly). If you want to set the first character to bold regardless of what you have selected, you could run something like this (your welcome):

Sub FirstCharBold
   Dim oOldSelection 'The original selection of cell ranges
   Dim oDoc
   Dim oCells
   Dim oCell
   Dim oEnum
   Dim iRows As Integer
   Dim iCols As Integer
   Dim iRow As Integer
   Dim iCol As Integer
   oDoc = ThisComponent

   REM store the current selection
   oOldSelection = oDoc.CurrentSelection
   If HasUnoInterfaces(oOldSelection, "com.sun.star.text.XText") Then
     'Inspect oOldSelection
      BoldFirstChar(oOldSelection.getText())
   ElseIf HasUnoInterfaces(oOldSelection, "com.sun.star.sheet.XSheetCellRanges") Then
     oCells = oOldSelection.getCells()
     oEnum = oCells.createEnumeration()
     Do While oEnum.hasMoreElements()
       oCell = oEnum.nextElement()
       BoldFirstChar(oCell.getText())
     Loop
   ElseIf HasUnoInterfaces(oOldSelection, "com.sun.star.sheet.XSheetCellRange") Then
     iCols = oOldSelection.getColumns().getCount() - 1
     iRows = oOldSelection.getRows().getCount() - 1
     For iRow = 0 To iRows
       For iCol = 0 To iCols
         oCell = oOldSelection.getCellByPosition(iCol, iRow)
         BoldFirstChar(oCell.getText())
       Next
     Next
   Else
     Print "Help, what is selected"
   End If
End Sub

Sub BoldFirstChar(oText)
   Dim oCurs
   oCurs = oText.createTextCursor()
   ' Goto start of text but do not select anything.
   oCurs.gotoStart(False)
   If oCurs.goRight(1, True) Then
     oCurs.CharWeight = com.sun.star.awt.FontWeight.BOLD
   End If
End Sub