Select annotation to edit

Purpose: Transfer Excel to LO calc
Task: Via button_click a macro creates a new annotation at the selected cell, inserts text like "OrderNo: " and selects this annotation with cursor at the end of the text, waiting for the number to be entered.

I’ve worked it out to create the annotation and insert the text, but could not find any documentation about selecting an annotation.
In Excel it’s the command: comment.Shape.Select
Where comment is the object of the annotation.

Is there something similar in LO Basic or does someone have an idea how to solve the task?

Try this:

' Edit current cell annotation (comment)
Sub EditAnnotation() 
  Dim oFrame as Object, oDisp as Object
  oFrame = ThisComponent.CurrentController.Frame
  oDisp = createUnoService("com.sun.star.frame.DispatchHelper") 
  oDisp.executeDispatch(oFrame, ".uno:EditAnnotation", "", 0, Array())  
End Sub

I guess you found this solution by recording a macro!?! That’s fine, and I don’t know why I didn’t come up with this solution myself.
There is still one open issue. After this command, the cursor is at the very beginning of the comment, but it should be at the end (if the comment already contains text).
I haven’t found a solution for this yet (not even with macro recording). Do you or anyone else have any ideas?

I agree with you and will join the enhancement request if you create one.
For now, I can only suggest this “dirty trick.”
The EditAnnotation2 macro should be run from Calc (not BasicIDE).

Option Explicit

Sub EditAnnotation2() 
  Dim oFrame as Object, oDisp as Object, oDoc As Object
  Dim oKeyEvent As New  com.sun.star.awt.KeyEvent
  
  oDoc = ThisComponent
  
  oFrame = oDoc.CurrentController.Frame
  oDisp = createUnoService("com.sun.star.frame.DispatchHelper") 
  oDisp.executeDispatch oFrame, ".uno:EditAnnotation", "", 0, Array()  
	 
  oKeyEvent.Modifiers = com.sun.star.awt.KeyModifier.MOD1  ' Ctrl 	  
  oKeyEvent.KeyCode=com.sun.star.awt.Key.END
  Simulate_KeyPress oDoc, oKeyEvent   ' send Ctrl + End to window
End Sub


' https://ask.libreoffice.org/t/create-a-macro-that-press-enter-automatic-once/27172/3?u=sokol92.
Sub Simulate_KeyPress(Byval oDoc As Object, Byval oKeyEvent as Object)
  Dim oWindow as object, oToolkit as Object
  
  If (Not (oKeyEvent Is Nothing)) And (Not (oDoc Is Nothing)) Then
    oWindow = oDoc.CurrentController.Frame.ContainerWindow
    oKeyEvent.Source = oWindow
    oToolkit = oWindow.Toolkit
    With oToolkit
      .keyPress(oKeyEvent)	
      .keyRelease(oKeyEvent)
    End With
  End If
    
End Sub

Thank you very much for your support. I got it to run and so far it will do the job.

The suggestion to create an enhancement request sounds reasonable. I am still relatively new to LibreOffice basic and haven’t created one yet. However, I will look into it now.

1 Like

I made an enhancement request if you want to join.
id=169094

1 Like

The forum has a bug link format: tdf#169094

See attached example.

I don’t think there exist means to do so. To get a similar effect, you can place an InputBox near the annotation shape and append the entered and posibly checked to the default text (as a string, of course).
The example does not contain the positioning, but InputBox has the needeed parameters.

Despite the fact that @Sokol92 already provided a solution uising dispatcher commands, I also post my suggestion which only uses Basic and API.

createDefaultAnnotations_disask_128133.ods (31.6 KB)

In fact I thought about such a solution as a back up and I still do.
But there is the possibility,that the user wants to add not only that number.
Sometimes he wants to add also several lines of description in one step. I forgot to give that information (sorry), but because of it a simple InputBox will not do it and some kind of Dialog will be necessary.
But from the point of having easy and clean code, it’s a very good alternative.
Thank you for your post.