[Solved] Writer - header and textportion protection macro

Creating a macro - Writing a Script - Using the API

[Solved] Writer - header and textportion protection macro

Postby ElFlamencoLoco » Thu Aug 10, 2017 8:50 pm

Hello folks,

Target to obtain
I have been writing a series of macros generating text documents in Writer. Those text documents have headers. Those headers should be read only, while the text body can be edited.

Problem
I know how to protect text portions (Insert -> Sections etc...) but it seems impossible to record a macro. I have tried several times:
- selecting the entire header (by mouse dragging)
- starting to record a macro
- protecting the selection (Insert -> Sections etc...)
- stopping to record the macro

There is no macro generated.

Question
Somebody an idea how to protect the entire header? I suggest to start solving the problem by the style object, although I could be wrong:
Code: Select all   Expand viewCollapse view
oStyle=ThisComponent.StyleFamilies.getByName("PageStyles").getByName("Standard")
oCursor=oStyle.HeaderText.createTextCursor
oCursor.gotoStart(false)
oCursor.gotoEnd(true)

'Start solving problem here
...


Thanks in advance
Last edited by ElFlamencoLoco on Sun Aug 13, 2017 3:33 pm, edited 3 times in total.
Windows 10
OpenOffice 4.1.2
ElFlamencoLoco
 
Posts: 16
Joined: Mon Dec 28, 2015 8:45 pm

Re: Writer - header protection macro

Postby FJCC » Thu Aug 10, 2017 11:36 pm

This inserts a protected section into the header. It doesn't protect all of the header from editing, just the inserted text. I hope that is a start.
Code: Select all   Expand viewCollapse view
oStyle=ThisComponent.StyleFamilies.getByName("PageStyles").getByName("Standard")
oSection = ThisComponent.createInstance("com.sun.star.text.TextSection")
oHtext = oStyle.HeaderText
oHtext.insertTextContent(oHtext.Start, oSection, True)
oSection.Anchor.String = "My Header Text"
oSection.IsProtected = True
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 6024
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Writer - header protection macro

Postby hubert lambert » Fri Aug 11, 2017 9:30 am

Just in case, a small change to FJCC's snippet to include existing header text in the added section:
Code: Select all   Expand viewCollapse view
oStyle=ThisComponent.StyleFamilies.getByName("PageStyles").getByName("Standard")
oSection = ThisComponent.createInstance("com.sun.star.text.TextSection")
oHtext = oStyle.HeaderText
oCursor = oHtext.createTextCursorByRange(oHtext.Start)
oCursor.gotoEnd(True)
oHtext.insertTextContent(oCursor, oSection, True)
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
hubert lambert
 
Posts: 57
Joined: Mon Jun 13, 2016 10:50 am

Re: Writer - header protection macro

Postby ElFlamencoLoco » Fri Aug 11, 2017 7:48 pm

Thanks very much, it works.
Windows 10
OpenOffice 4.1.2
ElFlamencoLoco
 
Posts: 16
Joined: Mon Dec 28, 2015 8:45 pm

Re: [Solved] Writer - header protection macro

Postby ElFlamencoLoco » Sun Aug 13, 2017 3:31 pm

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   Expand viewCollapse view
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   Expand viewCollapse view
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
Windows 10
OpenOffice 4.1.2
ElFlamencoLoco
 
Posts: 16
Joined: Mon Dec 28, 2015 8:45 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 5 guests