Macro to set shape in Writer in text edit mode

Shapes in Writer have an integrated text box in case the document was imported from Word. I want to remove the text box but keep the content as ordinary text of the shape. Such conversion is needed, if the text content needs to be rotated. I want a macro that does all needed steps.

Problem: I find no way to bring the selected shape into text edit mode using a macro. When you do this manually, you can select the shape and then press Enter or press F2.

The attached file contains the macro parts I have used so far. When you want to test the macro parts:

  1. Run macro "RemoveTextBox_KeepContent.
  2. Bring shape into text edit mode. That is the problem as macro!
  3. Run macro “PasteClipboardAsRTF”.
  4. Leave text edit mode but keep shape selected. Run macro “SetPropertiesToShape”.

Macro_inside_Remove_Textbox_but_keep_text.odt (22.7 KB)

Select text in Frame Text Box with textCursor()

Sub EditTextbox
	dim oDoc as object, oShape as object, oCur as object
	oDoc=ThisComponent
	for each oShape in oDoc.DrawPage 'get the shape by the name
		if oShape.Name="myShape" then
			oCur=oShape.createTextCursor
			with oCur
				.goToStart(false)
				.goToEnd(true)
			end with
			oDoc.CurrentController.Select(oCur)
			exit for
		end if
	next
End Sub

Or the simulation of key press

Sub EditTextbox
	dim oDoc as object, oShape as object
	oDoc=ThisComponent
	for each oShape in oDoc.DrawPage 'get the shape by the name
		if oShape.Name="myShape" then
			oDoc.CurrentController.Select(oShape)
			keyPress(oDoc)
			exit for
		end if
	next
End Sub

rem key pressing
Sub keyPress(oDoc as object) 'simulate F2/Enter press in oDoc
rem !!! this can cause the triggering of window elements.
	dim oKeyEvent as new com.sun.star.awt.KeyEvent
	oKeyEvent.Modifiers=0
	oKeyEvent.KeyCode=com.sun.star.awt.Key.F2 'or .RETURN
	'oKeyEvent.KeyChar=chr(13) 'can use for .RETURN
	simulate_KeyPress(oKeyEvent, oDoc)
End Sub

Sub simulate_KeyPress(oKeyEvent as com.sun.star.awt.KeyEvent, oDoc as object)
	if NOT isNull(oKeyEvent) then
		dim oWindow as object, oToolkit as object
		oWindow=oDoc.CurrentController.Frame.ContainerWindow
		oKeyEvent.Source=oWindow
		oToolkit=oWindow.Toolkit
		with oToolkit
			.keyPress(oKeyEvent)	
			.keyRelease(oKeyEvent)
		end with
	end if
End Sub
1 Like

My attempt for automation all process. Libre 7.6.4.1 Win10

Sub EditText
	dim oDoc as object, oShape as object, oCur as object
	oDoc=ThisComponent
	for each oShape in oDoc.DrawPage 'get the shape by the name
		if oShape.Name="myShape" then
			rem select text in text box via textCursor
			oCur=oShape.createTextCursor
			oDoc.CurrentController.Select(oCur)
			wait 10
			uno(oDoc, "SelectAll")
			wait 10
			uno(oDoc, "Copy")
			wait 10
			exit for
		end if
	next
	rem remove Text Box
	with oShape
		.ChainName=""
		.TextBox=False
		.TextBoxContent=Nothing
	end with
	rem Paste to Shape
	oDoc.CurrentController.Select(oShape)
	keyPress(oDoc) 'F2
	wait 500 'sometimes lesser value doesn't want to Paste copied text
	uno(oDoc, "Paste")
End Sub

Sub uno(oDoc as object, s$, optional arr())
	on local error goto bug
	if isMissing(arr) then arr=array()
	s=".uno:" & s
	createUnoService("com.sun.star.frame.DispatchHelper").executeDispatch(oDoc.CurrentController.Frame, s, "", 0, arr)
	exit sub
bug:
	bug(Erl, Err, Error, "uno")
End Sub

rem key pressing
Sub keyPress(oDoc as object) 'simulate F2/Enter press in oDoc
	rem !!! this can cause the triggering of window elements.
	dim oKeyEvent as new com.sun.star.awt.KeyEvent
	oKeyEvent.Modifiers=0
	oKeyEvent.KeyCode=com.sun.star.awt.Key.F2 'or .RETURN
	'oKeyEvent.KeyChar=chr(13) 'can use for .RETURN
	simulate_KeyPress(oKeyEvent, oDoc)
End Sub

Sub simulate_KeyPress(oKeyEvent as com.sun.star.awt.KeyEvent, oDoc as object)
	if NOT isNull(oKeyEvent) then
		dim oWindow as object, oToolkit as object
		oWindow=oDoc.CurrentController.Frame.ContainerWindow
		oKeyEvent.Source=oWindow
		oToolkit=oWindow.Toolkit
		with oToolkit
			.keyPress(oKeyEvent)
			.keyRelease(oKeyEvent)
		end with
	end if
End Sub
3 Likes

Hi @KamilLanda, thank you for the solution. I was not aware of “Toolkit” and did not thought, that a “wait” might be needed.

Unfortunately, there is bug 158927, so that I cannot use the solution for a rotated shape. But that is a different issue.

It seems it is possible to “bypass” bug 158927, because it is possible to set Angle of rotation to 0°, Paste text, and set the Angle back.
paste-to-rotated-shape.odt (20.4 kB)