Ask Your Question

Basic Macro to export specific pages of a writer document [closed]

asked 2018-01-28 22:18:17 +0200

daniel1 gravatar image

updated 2020-07-20 23:30:01 +0200

Alex Kemp gravatar image


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:

 Text of page 1
 Text of page 2
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2020-07-20 23:30:25.413041

1 Answer

Sort by » oldest newest most voted

answered 2018-01-29 06:40:25 +0200

librebel gravatar image

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 Based upon Original Code by ( Sébastien C ) here:
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( "" ) Then

        Dim oDocBuilder As Object : oDocBuilder = createUnoService( "" )
        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.gotoRange( oStart, True )
                strPage = oCursor.getString()
            End If

            oPage = oXML.createElement( "page" )
            oLeaf = oXML.createTextNode( strPage )
            oPage.appendChild( oLeaf )
            oPages.appendChild( oPage )

        If strFile <> "" Then   REM Export the XML structure to file:
            Dim oFileAccess As Object : oFileAccess = createUNOService( "" )
            If oFileAccess.exists( strFile ) Then oFileAccess.kill( strFile )       REM Overwrite.
            Dim oStream As Object : oStream = oFileAccess.openFileWrite( ConvertToURL( strFile ) )
            oXML.setOutputStream( oStream )
        End If

    End If
End Sub

HTH, With Regards,


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

edit flag offensive delete link more


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

Sub Test
End Sub

If i do this, i get a runtime error "Type: Message: an error occurred during file opening."

daniel1 gravatar imagedaniel1 ( 2018-01-29 23:26:07 +0200 )edit

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

( NB. Windows path uses the other slashes "\" )

librebel gravatar imagelibrebel ( 2018-01-29 23:33:01 +0200 )edit

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

daniel1 gravatar imagedaniel1 ( 2018-01-29 23:46:49 +0200 )edit

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.

librebel gravatar imagelibrebel ( 2018-01-30 00:16:46 +0200 )edit

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.

librebel gravatar imagelibrebel ( 2018-01-30 00:18:33 +0200 )edit

Question Tools

1 follower


Asked: 2018-01-28 22:18:17 +0200

Seen: 382 times

Last updated: Jan 29 '18