I would suggest to use marker characters different from any character usually occurring in our texts - and also “parenthesing” in the sense that start marker and end marker are different.
For my example I chose
♯ (U+266F) and
Then I wrote a piece of framework (distinguishing by positions in TextFrames, TextTables, and the Text body itself) and a loop going through the occurrences of strings accepted by the RegEx
For every finding the marker characters were replaced with spaces. The remaining string was selected and this way passed to the dispatcher command
For unknown reasons this doesn’t work reliably. In specific formula strings occurring inside TextTable or TextFrame were sometimes converted (obscure context in proceeding), but often not - and I failed trying to find clear conditions distinguishing the cases. Findings occurring in ordinary table cells without nesting seem to be converted, but…
The formula string (simple examples!) found in the BodyText, generally were treated correctly, but since there is much darkness, I wouldn’t expect this to be expectable …
It’s obviously a mess again. I haven’t the nerve now to spend another hour with what would be needed to omit the use of any dipatch command. See attached example.
PS Another test with
V18.104.22.168.beta1 also gave irreproducibkle results, but now sometimes converted the same formula (kind of a vector) correctly from a table cell, but not at all from the text body.
You may do the steps needed to omit the diapatcher, and report your success here.
=== Edit 2020-07-03 about 22:00UTC ===
Ignore the previous suggestions. The reworked macro works perfectly. (afaidt)
Sub makeFormulasFromMarkedTextAsNewOleObjects(Optional pDoc, Optional pScope, Optional pRegex As String)
If IsMissing(pDoc) Then pDoc = ThisComponent
If IsMissing(pScope) Then pScope = "fbt" REM Body, Frames, TextTables
pScope = Ucase(pScope)
fOK = InStr(pScope, "F")>0
tOK = InStr(pScope, "T")>0
bOK = InStr(pScope, "B")>0
If IsMissing(pRegEx) Then pRegEx = "♯.+?‼" REM Used in the example. LeadIn and LeadOut may be chosen differently.
seDe = pDoc.createSearchDescriptor REM You need to do it in the code.
.SearchRegularExpression = True
.SearchString = pRegEx
fi = pDoc.FindNext(pDoc.Text.Start, seDe)
REM The findings are only ordered predictably interior per text object.
While NOT IsNull(fi)
posF = IsObject(fi.TextFrame) REM Nesting: Also True if the Frame is inside a TexTable cell or another Frame.
posT = IsObject(fi.TextTable) REM Nesting: Also True if the TextTable is inside a Frame or a cell of another TextTable.
posB = NOT (posF OR posT)
If (fOK AND posF) OR (tOK AND posT) OR (bOK AND posB) Then
foStr = Mid(fi.String, 2, Len(fi.String) - 2)
fi.String = ""
mathOle = pDoc.createInstance("com.sun.star.text.TextEmbeddedObject")
mathOle.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
mathOle.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
fi.Text.insertTextContent(fi, mathOle, False)
mathOle.EmbeddedObject.Component.Formula = foStr
fi = pDoc.FindNext(fi.End, seDe)
avoids all the problems I had previously. It is also demonstrated in the new attachment.