In addition a user (identified by his profile) or different users (with different profiles) may want to be able to find back to one of the positions they recently worked at. This can be done with the help of bookmarks, but in many cases arbitrary names won't be the best choice.
I would suggest to use for this purpose bookmarks with names showing all the relevant information: The user who set it, and a date-time-stamp telling when it was set, both parts in a strict but short format being alphabetically sortable without semantic confusion.
The well tested code below can do that. Efficient usage will require to assign it to some caller, most likely a keyboard shortcut. If a click on a toolbar area is used as the trigger or the routine is called due to a FormControl event, some additional lines of code will allow branching depending on passed information.
The example contains in addition a function usable as an event handler for onGoingToBeClosed(). If used it will automate the setting of a bookmark for the "last position".
As always: If seriously interested in the feature provided here, please also download the example file. This way I can estimate the "impact".
Code: Select all
Sub createUserNamedDTstampedSinglePositionBookmark(Optional pEventInfo) REM Currently pEventInfo isn't evaluated. You may change that if you want to REM modify the action dending on what mofiers wer used with a toolbar area REM or any info provided by a FormControl used for the call. cDoc = ThisComponent If NOT cDoc.supportsService("com.sun.star.text.TextDocument") Then Exit Sub REM hard-coded names Const dataNode = "/org.openoffice.UserProfile/Data" Const useAsID = "initials" REM format strings Const longDTformat = "YYYY-MM-DD_HH:MM:SS" Const shortDTformat = "YYYYMMDDHHMMSS" REM preferred by my current camera, e.g. REM Assumed preferences Const dtDelim = "_" REM inserted between user ID and dat. Const bookmarkStart = False cCtrl = cDoc.CurrentController cSel = cCtrl.Selection If cSel.Count>1 Then Exit Sub REM Bookmark shall point to a single position. User should define it unambiguosly. insPos = IIf(bookmarkStart, cSel(0).Start, cSel(0).End) newBm = ThisComponent.createInstance("com.sun.star.text.Bookmark") newBmN = simplePropertyValueFromRegistryModificationsXcu(dataNode, useAsID) newBmN = newBmN & dtDelim & Format(Now(), longDTformat) newBm.Name = newBmN cText = insPos.Text insCur = cText.createTextCursorByRange(insPos) cText.insertTextContent(insCur, newBm, False) End Sub REM Thanks to Andrew Pitonyak I could easily code this needed helper: ' Function simplePropertyValueFromRegistryModificationsXcu( _ pNodePath As String, pPropertyName As String) As Variant simplePropertyValueFromRegistryModificationsXcu = ":err:" On Local Error Goto fail Dim structNode, providerSrv Dim arguments(0) As New com.sun.star.beans.PropertyValue Const providerSrvN = "com.sun.star.configuration.ConfigurationProvider" Const structNodeN = "com.sun.star.configuration.ConfigurationAccess" providerSrv = createUnoService(providerSrvN) arguments(0).Name = "nodepath" arguments(0).Value = pNodePath structNode = providerSrv.createInstanceWithArguments(structNodeN, arguments()) simplePropertyValueFromRegistryModificationsXcu _ = structNode.getPropertyValue(pPropertyName) fail: End Function