Basic Macro to export specific pages of a writer document

Hey,

can anyone tell me how to export specific pages of a writer document to a xml-file with a basic macro?
The goal is to get a xml-file like that:

<pages>
 <page>
 Text of page 1
 <\page>
 <page>
 Text of page 2
 <\page>
<\pages>

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/t/basic-macro-to-export-specific-pages-of-a-writer-document/30553
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


To accept this answer, please click on the checkmark icon on the left so that it turns green.

Thank you for your answer!
For testing i tried to execute the macro with this Sub:

Sub Test
Writer_Export_Pages_To_XML("/test.xml")
End Sub

If i do this, i get a runtime error “Type: com.sun.star.ucb.InteractiveAugmentedIOException
Message: an error occurred during file opening.”

it must be a full path, e.g. “/home/user/Desktop/test.xml”

( NB. Windows path uses the other slashes “” )

Oh, i see, thank you.
Another question: How can i get it to export line breaks?

Linebreak characters typed explicitly into the text by pressing the “ENTER”-key, are exported.

But Automatic Linebreaks caused by Wrapping the text inside the current frame width, are not exported, since these are not part of the text.

There is a way to insert explicit linebreak characters into the exported text at the end of each visible line in the original text, but then the exported text would be different from the original text.

If you still want to do that, then please make it into another Question, and mark this Answer as correct by clicking on the Checkmark icon on the left.