# Revision history [back]

I don't feel sure if I got everything right.
-1- Why do you use the ViewCursor? If you want to make lots of insertions in one run based on the selections you made in the text, you should use the CurrentSelction, imo.
-2- A frame not yet inserted as a TextContent obviously cannot take a string. That's a result of your (and my) experiments. As so often I do not know a place where this is specified explicitly. We should take it as a fact nonetheless. If it actually is a bug you would have to report it and wait about 20 years for the fixing.
-3- With the code posted below I inserted 20 frames into a dummy text within about 1 second. Of course insertions into a very long text may need longer. After all every single insertion should cause a new page-wrap process, shouldn't it? When I still had to use MS Word in the 1990es, it often needed minutes for the page wrap.

Code:

REM  *****  BASIC  *****
' NOT from profound knowledge about specifications
' BUT from my experiences and "research" (reverse engineering?)
' A single selection counts for ONE TextRange.
' With a multiple selection the (next term invented!) 'BlinkCursorPosition'
' counts for an extra TextRange and is returned as the
' ZEROth range of the CurrentSelection object.
Sub insertManyFramesAtMultiSelectedPositions()
theText = ThisComponent.Text
theSel  = ThisComponent.CurrentSelection
u = theSel.Count - 1
If (u>0) Then
lastRg  = theSel(u)
h1 = theText.CompareRegionStarts(blinkRg, lastRg)
h2 = theText.CompareRegionEnds  (blinkRg, lastRg)
If (h1*h2=0) Then
u = u-1
End If
End If
Dim theFrames(0 To u) As Object
ThisComponent.UndoManager.EnterUndoContext("manyFrames")
For j = 0 To u
oneFr = ThisComponent.createInstance( "com.sun.star.text.TextFrame" )
oneFr.widthType = 0
oneFr.FrameStyleName = "MySpecialFrameStyle"
oneFr.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
theText.InsertTextContent(theSel(j).Start, oneFr, False )
theFrames(j) = oneFr
Next j
For j = 0 To u
theFrames(j).Text.String = "" & (j+1) &". Important Stuff"
Next j
ThisComponent.UndoManager.LeaveUndoContext
End Sub
' "MySpecialFrameStyle" was defined in the document used for testing.


You may use this example containing the code.
An attempt to runl the Sub for a multiselection NOT only containing regions within ThisComponent.Text will fail. The Sub does not contain code to test for this.