[Writer] Macro to update indexes, merge revisions, save doc

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
stefano77
Posts: 1
Joined: Wed Aug 27, 2008 6:01 pm

[Writer] Macro to update indexes, merge revisions, save doc

Post by stefano77 »

macro to be executed before saving the current document.

WARNING: assign this macro only to the "Save document" event, and not to the "Save document as" event; this because when saving as a new document the the getURL function return the URL of the old document.

functionality:
- update indexes
- remember user to merge changes if RecordChanges is activated
- save document as html, txt, pdf, odf (backup copy)

on line references:
- Accept tracked changes from UNO or macro (http://www.oooforum.org/forum/viewtopic.phtml?t=33521)
- OO-Snippets: export as PDF (http://codesnippets.services.openoffice ... AsPDF.snip)
- OOoBasic crash course: Multi-format document backup (http://www.linux.com/articles/58110)

Code: Select all

sub before_save
' rev 2008-08-27 14:53

' ======================================================================
' define variables
' ======================================================================
dim document   as object
dim dispatcher as object
dim args(0) as new com.sun.star.beans.PropertyValue
dim sDocURL$
dim mergeflag as boolean


	' ======================================================================
	' update all indexes (to update TOC)
	' ======================================================================
	
	' get access to the document
	document   = ThisComponent.CurrentController.Frame
	dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
	
	' update all indexes
	dispatcher.executeDispatch(document, ".uno:UpdateAllIndexes", "", 0, Array())
	
	
	' ======================================================================
	' save sequence
	' ======================================================================
	
	' IF ask user: "do you want quick save ?" (default "NO")
	If MsgBox ("Do you want to quick save ?",  292) = 7 Then
	' No button pressed: normal save

		' set merge flag as false
		mergeflag = False

		' IF check if RecordChanges = True or False
		if ThisComponent.getPropertyValue("RecordChanges") = True then
		' RecordChanges = True

			' IF ask user to merge changes (default "NO")
			If MsgBox ("Do you want to merge changes ?",  292) = 6 Then
			' Yes button pressed: merge changes and stop macro
            
				' show changes and show accept or reject changes dialog
				args(0).Name = "ShowTrackedChanges"
				args(0).Value = true
				dispatcher.executeDispatch(document, ".uno:ShowTrackedChanges", "", 0, args())
				dispatcher.executeDispatch(document, ".uno:AcceptTrackedChanges", "", 0, Array())
				' set merge flag as true
				mergeflag = True
            
			' ENDIF ask user to merge changes
			End If
            
		' ENDIF check if RecordChanges = True or False
		End If

		' IF check if merge flag is True or Fale
		if mergeflag = False then
		' merge flag = false: continue with document save

			' set show changes as false
			args(0).Name = "ShowTrackedChanges"
			args(0).Value = false
			dispatcher.executeDispatch(document, ".uno:ShowTrackedChanges", "", 0, args())
	
			' Load required libraries
			If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
				GlobalScope.BasicLibraries.LoadLibrary("Tools")
			End if
	        
			' IF check if the current document is already saved (if has a location)
			If (ThisComponent.hasLocation()) Then
			' the current document has a location
	
				' read the full path of the current document
				sDocURL = ThisComponent.getURL()
	            
				' save the current document as HTML
				args(0).Name = "FilterName"
				args(0).Value = "HTML (StarWriter)"
				ThisComponent.storeToURL(sDocURL & ".html", args())
	
				' save the current document as TXT
				args(0).Name = "FilterName"
				args(0).Value = "Text"
				ThisComponent.storeToURL(sDocURL & ".txt", args())
				
				' save the current document as PDF
				args(0).Name = "FilterName"
				args(0).Value = "writer_pdf_Export"
				ThisComponent.storeToURL(sDocURL & ".pdf", args())
				
				' save the current document as ODF (backup copy)
				args(0).Name = "FilterName"
				args(0).Value = "writer8"
				ThisComponent.storeToURL(sDocURL & ".bak", args())
	
			' ELSE check if the current document is already saved (if has a location)
			Else
			' the current document has not a location
	
				' show error to the user
				msgbox ("Can't save this document in any other format. Save as 'quick save' and retry.", 16)
	
			' ENDIF check if the current document is already saved (if has a location)
			End If
		
		' ENDIF check if merge flag is True or Fale
		End if

	' ELSE ask user: "do you want quick save ?" (default "NO")
	Else
	' Yes button pressed: quick save

		' set show changes as false
		args(0).Name = "ShowTrackedChanges"
		args(0).Value = false
		dispatcher.executeDispatch(document, ".uno:ShowTrackedChanges", "", 0, args())

	' ENDIF ask user: "do you want quick save ?" (default "NO")
	End If

end sub

Hope this can be useful to someone as it is for me.

bye


---
Stefano Spinucci
oobuffett
Posts: 1
Joined: Sun Jul 12, 2009 3:57 am

Re: [Writer] macro to update indexes, merge revisions, save doc

Post by oobuffett »

Great code, it is very useful, tks.
Post Reply