[Solved] Calc: Macro for 'Save a Copy'

Creating a macro - Writing a Script - Using the API

[Solved] Calc: Macro for 'Save a Copy'

Postby muaz12 » Fri Mar 09, 2018 10:56 pm

I am trying to write a macro to run the 'Save a Copy' command and save a copy of the active sheet as a csv file in the same directory as the workbook. I searched and found many examples on this forum but they were all for Save As, and not Save a Copy. I couldn't find a reference in the Programmer's Guide either. Hoping someone here can help me with that.
Last edited by muaz12 on Sat Mar 10, 2018 12:55 am, edited 1 time in total.
LibreOffice Version: on Ubuntu 16.04
Posts: 2
Joined: Fri Mar 09, 2018 10:52 pm

Re: Calc: Macro for 'Save a Copy'

Postby Zizi64 » Fri Mar 09, 2018 11:18 pm

You must study the API functions included the functions StoreAsURL() and the StoreToURL(). (...and the usage of the export filters)

https://wiki.openoffice.org/wiki/API/Tu ... PDF_export
https://forum.openoffice.org/en/forum/v ... f=9&t=2551
Tibor Kovacs, Hungary; LO4.4.7, LO6.1.5 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.0 and AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Posts: 7709
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Calc: Macro for 'Save a Copy'

Postby muaz12 » Sat Mar 10, 2018 12:54 am

Thank you for the amazingly quick reply!

I got it working. Here is the code if anyone is interested:

Code: Select all   Expand viewCollapse view
Sub Main
   dim document as object
   dim cell as object
   document = ThisComponent  'assigns the current document to the variable document
   Sheets = document.Sheets  'get the container of all Sheets
   Sheet = Sheets.getByName("data")   'get the sheet named data
   Controller = document.getcurrentController
   Sheet.IsVisible = True
   Dim Propval(1) as New com.sun.star.beans.PropertyValue
   Propval(0).Name = "FilterName"
   Propval(0).Value = "Text - txt - csv (StarCalc)"
   Propval(1).Name = "FilterOptions"
   Propval(1).Value ="44,34,0,1,1"   'Delimiters: ASCII  44 = ,  34 = "
   Doc = ThisComponent
    FileName = Tools.Strings.DirectoryNameoutofPath(ThisComponent.getURL(),"/") & "/schedule.csv"
   FileURL = convertToURL(FileName)
   Doc.storeToURL(FileURL, Propval())
   Sheet = Sheets.getByName("main")   'get the sheet named main
End Sub
LibreOffice Version: on Ubuntu 16.04
Posts: 2
Joined: Fri Mar 09, 2018 10:52 pm

Return to Macros and UNO API

Who is online

Users browsing this forum: Exabot [Bot] and 2 guests