How do I select text in a text frame with a macro?

I know I can get a view cursor for the document with

viewCursor = ThisComponent.CurrentController.getViewCursor

And I can get a text cursor in a frame with

oFrame = ThisComponent.createInstance( "com.sun.star.text.TextFrame" )
frameCursor = oFrame.getText().createTextCursor()

But I don’t know how to set the view cursor to the range of the text cursor. If I try to do

viewCursor.gotoRange(frameCursor, true)

it gives an error with no message. I assume it is a conflict because the view cursor is outside the frame and I’m trying to set it to a range inside the frame. I think I need to create a different view cursor inside the frame somehow?

Ultimately I want to be able to search for all-caps words inside the frame and set them to small caps. I don’t think this can be done directly with LO Basic, so I figured I would just select the frame text and then run a macro which is a recording of a regex search followed by

dispatcher.executeDispatch(document, ".uno:SmallCaps", "", 0, Array())

But if there is a better way to do this whole thing, then that is great too.

Out of date, but I add the solution :slight_smile:
Put Visible Cursor to Text Frame is possible via simulation of press the Enter.

Sub insertFrameWithVisibleCursor
	dim oDoc as object, oFrame as object, oVCur as object, oCur as object
	oDoc=StarDesktop.LoadComponentFromUrl("private:factory/swriter", "_blank", 0, array())
	oFrame=oDoc.createInstance("com.sun.star.text.TextFrame")
	with oFrame
		.setName("New Frame")
		.AnchorType=com.sun.star.text.TextContentAnchorType.AT_PARAGRAPH
		.HoriOrient=com.sun.star.text.HoriOrientation.NONE
		.VertOrient=com.sun.star.text.VertOrientation.NONE
		.HoriOrientPosition=1000
		.VertOrientPosition=1000
		.Width=4000
		.Height=4000
		.BorderDistance=100
	end with
	oDoc.Text.insertTextContent(oDoc.Text.createTextCursor, oFrame, True)
	with oFrame
		.String="Hello in new Text Frame"
		.FrameIsAutomaticHeight=false
	end with
	oDoc.CurrentController.Select(oFrame)
	simulate_KeyPress_RETURN
	wait 50 'increase if there will be extra Enter in Text Frame instead the part of String
	oVCur=oDoc.CurrentController.ViewCursor 'visible cursor
	oDoc.CurrentController.Select(oVCur) '"update" that Visible Cursor is in Text Frame
	oCur=oFrame.createTextCursor 'Text Cursor in Frame
	with oCur
		.goToStart(false)
		.goToNextWord(false)
	end with
	with oVCur
		.goToRange(oCur.End, false) 'Visible Cursor to position of Text Cursor
		.goRight(6, true) 'select some letters
		.CharCaseMap=1 'set UPPERCASES
	end with
End Sub

Sub simulate_KeyPress_RETURN
	dim oKeyEvent as new com.sun.star.awt.KeyEvent
	with oKeyEvent
		.Modifiers=0
		.KeyCode=com.sun.star.awt.Key.RETURN '1280
		.KeyChar=chr(13)
	end with
	simulate_KeyPress(oKeyEvent)
End Sub

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

Change some property in TextFrame is possible without Visible Cursor, with “double” enumeration

Sub enumerationInTextFrame
	dim oDoc as object, oFrame as object, oEnum as object, o as object, oEnum2 as object, o2 as object
	oDoc=ThisComponent
	oFrame=oDoc.DrawPages(0).getByIndex(0)
	oEnum=oFrame.createEnumeration
	do while oEnum.hasMoreElements
		o=oEnum.nextElement
		oEnum2=o.createEnumeration
		do while oEnum2.hasMoreElements
			o2=oEnum2.nextElement
			if o2.CharCaseMap=1 then o2.CharCaseMap=4 'UPPERCASES to SmallCaps
		loop
	loop
End Sub