[Solved] Save in the same directory with the same filename

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sw_and
Posts: 3
Joined: Mon Feb 20, 2012 11:26 am

[Solved] Save in the same directory with the same filename

Post by sw_and »

I think this should be simple though google didn't help
Please help.

Code: Select all

sub Main
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object

rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "URL"
args1(0).Value = "file:///home/user/Documents/cant_save_russiansaved.xls"
args1(1).Name = "FilterName"
args1(1).Value = "MS Excel 97"

dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args1())

end sub
Last edited by Hagar Delest on Thu Feb 23, 2012 5:01 pm, edited 1 time in total.
Reason: tagged [Solved].
LibreOffice 3.4.5, Ubuntu 11.10 & WinXP SP3
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: How to save in the same directory with the same filename

Post by JohnSUN-Pensioner »

Welcome to the forum, sw_and!
You have several options for solving the problem.
1. Read Chapter 5.17. Saving And Exporting A Document from the book by Andrew Pitonyak "My Macro Libraries" and copy the correct code from the listing.
2. Use text macros StoreDocument from the standard library Tools
3. Ask a question on the Russian forum. There your question will be easier to formulate and the answers will be clearer (I hope)
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
User avatar
kingfisher
Volunteer
Posts: 2123
Joined: Tue Nov 20, 2007 10:53 am

Re: How to save in the same directory with the same filename

Post by kingfisher »

This interested me. If you are stuck, this works on my system:

Code re-posted later.
Last edited by kingfisher on Tue Feb 21, 2012 12:24 pm, edited 5 times in total.
Apache OpenOffice 4.1.9 on Linux
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: How to save in the same directory with the same filename

Post by JohnSUN-Pensioner »

+1 kingfisher!
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
User avatar
kingfisher
Volunteer
Posts: 2123
Joined: Tue Nov 20, 2007 10:53 am

Re: How to save in the same directory with the same filename

Post by kingfisher »

Hi, John. Your reference to an empty url had me foxed. The script will throw an error if there is no 'ThisComponent'. Would you include provision for such an error? It could be "On error" or if no components.

Anyway, I had fun editing then re-editing after your initial post.
Apache OpenOffice 4.1.9 on Linux
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: How to save in the same directory with the same filename

Post by JohnSUN-Pensioner »

ThisComponent of the new file is also present. Normal full-featured object. But the URL it will be empty as long as the file will not be recorded.
A collaborative editing posts is very reminiscent of the chat. It was nice to talk to. :lol:
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
User avatar
kingfisher
Volunteer
Posts: 2123
Joined: Tue Nov 20, 2007 10:53 am

Re: How to save in the same directory with the same filename

Post by kingfisher »

Ok. Thanks to John, I realised someone could run this script from a document which has not been saved. I have tried that and the script is simply ineffective. It is possible, I suppose, that someone could try to run it without having a document open. I have therefore added an error routine.

Code: Select all

Sub SaveAsXLS
' Run this from an open spreadsheet which has been saved
On Error goto ErrorMsg

Dim oDoc as Object, sUrl as String, sDot as String, i as Integer, _
	sPath as String
oDoc = ThisComponent : sUrl = oDoc.url : sDot = "."
If NOT oDoc.hasLocation Then
Exit Sub : End If
i = InStr ( sUrl, sDot )
sPath = ConvertToUrl ( Left ( sUrl, i ) & "xls" )

Dim aProps(2) as new com.sun.star.beans.PropertyValue
aProps(0).Name = "Overwrite"
aProps(0).Value = "True" ' existing xls version overwritten either way
aProps(1).Name = "URL"
aProps(1).Value = sPath
aProps(2).Name = "FilterName"
aProps(2).Value = "MS Excel 97"

oDoc.storeAsURL ( sPath, aProps() ) ' The xls file is open
'oDoc.storeToURL ( sPath, aProps() ) ' The ods file is open
' - an alternative to storeAsURL

ErrorMsg:
Msgbox "This script failed to run"

End Sub
Last edited by kingfisher on Thu Feb 23, 2012 8:22 am, edited 2 times in total.
Apache OpenOffice 4.1.9 on Linux
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: How to save in the same directory with the same filename

Post by RPG »

Hello

Maybe a more easy solution in stead of using on error is use:

Code: Select all

Sub Main
print thiscomponent.haslocation
End Sub
http://www.openoffice.org/api/docs/comm ... rable.html

Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
User avatar
kingfisher
Volunteer
Posts: 2123
Joined: Tue Nov 20, 2007 10:53 am

Re: How to save in the same directory with the same filename

Post by kingfisher »

Added

Code: Select all

If sUrl = "" Then
Exit Sub : End If
Changed to

Code: Select all

If NOT oDoc.hasLocation Then
Exit Sub : End If
Apache OpenOffice 4.1.9 on Linux
sw_and
Posts: 3
Joined: Mon Feb 20, 2012 11:26 am

Re: How to save in the same directory with the same filename

Post by sw_and »

kingfisher
Thank you so much, script is rock solid !
LibreOffice 3.4.5, Ubuntu 11.10 & WinXP SP3
Post Reply