[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: 5.1.6.2 on Ubuntu 16.04
muaz12
 
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/Saving_a_document
https://wiki.openoffice.org/wiki/API/Tu ... PDF_export
https://forum.openoffice.org/en/forum/v ... f=9&t=2551
Tibor Kovacs, Hungary; LO6.1.6 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.5; AOO4.1.6
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
Zizi64
Volunteer
 
Posts: 8139
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
   Controller.setActiveSheet(Sheet)
   
   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
   GlobalScope.BasicLibraries.loadLibrary("Tools")
    FileName = Tools.Strings.DirectoryNameoutofPath(ThisComponent.getURL(),"/") & "/schedule.csv"
   FileURL = convertToURL(FileName)
   Doc.storeToURL(FileURL, Propval())
   Sheet = Sheets.getByName("main")   'get the sheet named main
   Controller.setActiveSheet(Sheet)
End Sub
LibreOffice Version: 5.1.6.2 on Ubuntu 16.04
muaz12
 
Posts: 2
Joined: Fri Mar 09, 2018 10:52 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: UnklDonald418 and 13 guests