Hi folks,
Digging deeper into programming with text protection in Writer, I did not only incorporate header protection into my macros, but also text portion protection. Only 2 subroutines. Please feel free to use them.
First how to turn on protection.
Code: Select all
sub ProtectParagraphsOnCursor(byref oDoc as object)
rem Protects all paragraphs covered by the view cursor, keeping them together
dim oStyle as object, oSection as object, oViewCursor as object, oCursor as object
'Protected zone begins at start of paragraph
oViewCursor = oDoc.CurrentController.getViewCursor
oCursor = oDoc.Text.createTextCursorByRange(oViewCursor.start)
oCursor.gotoStartOfParagraph(false)
'Protected zone ends ad end of paragraph (might be a different paragraph)
oCursor.gotoRange(oViewCursor.end, true)
oCursor.gotoEndOfParagraph(true)
'Create and insert section object
Style = oDoc.StyleFamilies.getByName("PageStyles").getByName("Standard")
oSection = oDoc.createInstance("com.sun.star.text.TextSection")
oDoc.Text.insertTextContent(oCursor, oSection, true)
'Visualize protected text with a different background color
oSection.BackColor = RGB(241, 207, 225)
oCursor.charBackColor = RGB(241, 207, 225)
'Turn protection on
oSection.isProtected = true
end sub
Next how to retrieve the section object onto the cursor
Code: Select all
function getProtectionOnCursor(byref oDoc as object) as object
rem Returns the protected text section onto the cursor (null if not in protected section)
dim oSections as object, names(0) as string, oSection as object, n as integer, _
oCursor as object, oViewCursor as object, _
viewcurpos as long, protect_startpos as long, protect_endpos as long
'Itterate through text sections
oSections = oDoc.getTextSections
names = oSections.getElementNames
for n = LBound(names) to UBound(names)
oSection = oSections.getByName(names(n))
if oSection.isProtected then
'Compare cursor position with section borders
oCursor = oDoc.Text.createTextCursorByRange(oSection.getAnchor)
oViewCursor = oDoc.CurrentController.getViewCursor
viewcurpos = PositionInDocument(oDoc, oViewCursor.start)
protect_startpos = PositionInDocument(oDoc, oCursor.start)
protect_endpos = PositionInDocument(oDoc, oCursor.end)
if viewcurpos >= protect_startpos and viewcurpos <= protect_endpos then
'Return result
getProtectionOnCursor = oSection
exit function
end if
end if
next n
end function
function PositionInDocument(byref oDoc as object, byref oRange as object) as long
rem Returns the position of the specified range object in the document
dim oCursor as object
'Create cursor spanning from start of the document till the specified range object
oCursor = oDoc.Text.createTextCursorByRange(oRange)
oCursor.collapseToEnd
oCursor.gotoStart(true)
'Return result
PositionInDocument = len(oCursor.string)
end function