Hello @daniel1,
To export specific pages from the current Writer document to an xml-file , according to the stated goal structure, you could use the following Basic macro:
Sub Writer_Export_Pages_To_XML( strFile As String, Optional iStartPage, Optional iEndPage )
REM Export Pages from the current Writer document as XML structure.
REM See https://ask.libreoffice.org/en/question/144315/basic-macro-to-export-specific-pages-of-a-writer-document/
REM Based upon Original Code by ( Sébastien C ) here: https://forum.openoffice.org/en/forum/viewtopic.php?f=21&t=86044
REM <strFile> : Path to the XML-file into which the pages will be exported ( e.g. "/home/user/Desktop/myPages.xml" );
REM This file will be created if non-existent, and will be overwritten if it already exists.
REM <iStartPage>: Integer specifying the first page to Export; DEFAULT=1.
REM <iEndPage> : Integer specifying the last page to Export; DEFAULT=<Total number of Pages>.
Dim oDoc As Object : oDoc = ThisComponent
If oDoc.supportsService( "com.sun.star.text.TextDocument" ) Then
Dim oDocBuilder As Object : oDocBuilder = createUnoService( "com.sun.star.xml.dom.DocumentBuilder" )
Dim oXML As Object : oXML = oDocBuilder.newDocument()
Dim oPages As Object : oPages = oXML.createElement( "pages" ) REM Root Node = "<pages>".
oXML.appendChild( oPages )
Dim iPageCount As Integer REM Get the number of pages in the current Writer document:
Dim oCursor As Object : oCursor = oDoc.CurrentController.getViewCursor()
If oCursor.jumpToLastPage() Then iPageCount = oCursor.getPage()
If IsMissing( iStartPage ) Then iStartPage = 1
If IsMissing( iEndPage ) Then iEndPage = iPageCount
If iEndPage > iPageCount Then iEndPage = iPageCount
Dim oPage As Object, strPage As String REM Add a Child Node for each Page:
Dim oLeaf As Object, oStart As Object, i As Integer
For i = iStartPage To iEndPage
oCursor.gotoStart( False ) REM Unselect the previous selection.
If oCursor.jumpToPage( i ) Then REM Get the text of page <i>.
oStart = oCursor.getStart()
oCursor.jumpToEndOfPage()
oCursor.gotoRange( oStart, True )
strPage = oCursor.getString()
End If
oPage = oXML.createElement( "page" )
oLeaf = oXML.createTextNode( strPage )
oPage.appendChild( oLeaf )
oPages.appendChild( oPage )
Next
If strFile <> "" Then REM Export the XML structure to file:
Dim oFileAccess As Object : oFileAccess = createUNOService( "com.sun.star.ucb.SimpleFileAccess" )
If oFileAccess.exists( strFile ) Then oFileAccess.kill( strFile ) REM Overwrite.
Dim oStream As Object : oStream = oFileAccess.openFileWrite( ConvertToURL( strFile ) )
oXML.setOutputStream( oStream )
oXML.start()
oStream.closeOutput()
End If
End If
End Sub
HTH, With Regards,
lib
