Cybb20 Super User


Joined: 02 Mar 2004 Posts: 1569 Location: Frankfurt, Germany
|
Posted: Sun May 15, 2005 6:35 am Post subject: How to delete all empty pages in a Writer document |
|
|
| Code: |
Sub Delete_Empty_Pages( )
'deletes all empty pages in a writer document recursively, meaning we start from the last page,
'working our way up to the first page.
odoc = thisComponent
'if you have problems with thisComponent for the new OOo 2.0 builds,
'use Stardesktop.getCurrentComponent() instead
view = odoc.getCurrentController()
cursor = view.getViewCursor()
cursor.setVisible(False)
cursor.jumpToLastPage()
DealWithPage(cursor)
End Sub
Function DealWithPage(cur)
'recursive function, watch out for the recursion limit which used to be around 500, but
'should have been increased by the time.
Dim charcount as Integer, currentpage as Integer
Dim bEmptyPage as Boolean
charcount = 0
bEmptyPage = True
currentpage = cur.getPage()
cur.jumpToEndOfPage()
Do While cur.getPage() = currentpage
If cur.goLeft(1, True) Then
charcount = charcount + 1
s = cur.getString()
If Not IsWhitespace(s) Then
bEmptyPage = False
Exit Do
End If
Else
'first page must be empty! Exit loop and handle later.
charcount = charcount + 1
Exit Do
End If
Loop
If bEmptyPage Then
DeletePage(cur, charcount, (currentpage = 1))
Else
cur.jumpToPreviousPage()
End If
If currentpage = 1 Then Exit Function 'careful, only exit condition!
DealWithPage(cur)
End Function
Function IsWhitespace(s) as Boolean
s = Left(s, 1)
If len(s) = 0 Then
IsWhitespace = True
Exit Function
Else
If Asc(s) <= 32 Then
IsWhitespace = True
Exit Function
End If
End If
'if it fell through, it is not whitespace!
IsWhiteSpace = False
End Function
Function DeletePage(cur, i as Integer, bFirstPage as Boolean)
'msgbox "charcount = " & i & " for page = " & cur.getPage()-1
If bFirstPage Then
cur.jumpToStartOfPage()
Else
cur.jumpToEndOfPage()
End If
cur.goRight(i, True)
cur.setString("")
End Function
|
Let me know if you have any problems using it or if you think something must be corrected or some parts should be improved.
UPDATE:
As of today, this macro only works for formatted text only, meaning it does not work for tables and other objects.
Christian _________________ - Knowledge is Power - |
|