[Solved] Save Calc with VB

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Sool
Posts: 4
Joined: Tue Dec 12, 2017 4:47 am

[Solved] Save Calc with VB

Post by Sool »

I'm working with Visual Studio, made a desktop app which I use to control data, within the app I can export data to a ooo Calc, I can do a lot of things but now I want to save it once my code finished inside a Sub, I have this but something is missing:

Dim p As Object = objServiceManager.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
oDoc.storeToURL("C:\Some\random\path",p)

And If I only use:

oDoc.store()

It gives an error.
Last edited by Sool on Thu Dec 14, 2017 6:33 pm, edited 3 times in total.
OpenOffice 4.1 - Windows 10
FJCC
Moderator
Posts: 9270
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Save Calc with VB

Post by FJCC »

The first argument of storeToURL has to be a URL, so C:\some\random\path has to be converted to file:///C:/some/random/path

The second argument of storeToURL() has to be an array. In OpenOffice Basic you could do that with Array(p).

What error do you get with store()?
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Sool
Posts: 4
Joined: Tue Dec 12, 2017 4:47 am

Re: Save Calc with VB

Post by Sool »

About the error in the second argument, I'm working on a Visual Basic IDE, not within the Calc itself, I had no problem with the URL(the above was just an example) In VB you can't simple write "Array", it gives an error, then I declared it as an array Object but it's the same.
With store it just says that there is an error with the COM component, nothing else.
OpenOffice 4.1 - Windows 10
Sool
Posts: 4
Joined: Tue Dec 12, 2017 4:47 am

Save Calc with VB

Post by Sool »

I solved it, the whole problem was the path, it doesn't accept the inverted slash "\" but the normal one "/".
Also I want to share what I did as additional, in my project wether an existing file in order to update it or a template in order to create a new calc; before saving first I check if the file has location (if there was opened an existing file) if not, then just save it in the same path, this is my code:

Code: Select all

 Dim propertyValues(2) As Object
        propertyValues(0) = objServiceManager.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
        propertyValues(1) = objServiceManager.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
        Dim haslocation As Boolean = objDocument.haslocation()
        If haslocation = True Then
            objDocument.store()
        Else
            objDocument.storeAsURL("file:///" & a.Replace("\", "/").Remove(a.Length - 9, 9) & "Resources/Formatos/Diagnosticos/" & t & ".ods", propertyValues)
        End If
Last edited by Sool on Thu Dec 14, 2017 6:32 pm, edited 1 time in total.
OpenOffice 4.1 - Windows 10
User avatar
Zizi64
Volunteer
Posts: 11358
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Save Calc with VB

Post by Zizi64 »

I solved it, the whole problem was the path, it doesn't accept the inverted slash "\" but the normal one "/".

...

Code: Select all

objDocument.storeAsURL("file:///" & a.Replace("\", "/").Remove(a.Length - 9, 9) & "Resources/Formatos/Diagnosticos/" & t & ".ods", propertyValues)
There is a Library named "Tools" in the Basic Libraries of the AOO and LO.
And there are Functions to convert Windows style Full paths+filename+extension string to URL:
The function ConvertToURL()
- will replace the "\" characters to "/" characters,
- and it will insert the file:/// string at the begin of the URL,
- and it will convert the special characters of the path or file name to "% codes".

And the function ConvertFromURL() can convert an URL to Windows-like Fullpath+filename+extension string.


The library Tools will not loaded automatically into the memory, you need load it by a short program code:

Code: Select all

	If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
		GlobalScope.BasicLibraries.LoadLibrary("Tools")
	End If
Or you can write a short Sub for this. In this case you can load the library with a short command "LoadTools" :

Code: Select all

Sub LoadTools

	If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
		GlobalScope.BasicLibraries.LoadLibrary("Tools")
	End If
end sub

Usage of the ConvertToURL():

Code: Select all

LoadTools
...
sTheURLstring = ConvertToURL("C:\some\random\path\with some space spec char\filename.ext")
the result will be in the sTheURLstring variable:

Code: Select all

file:///some/random/path/with%20some%20space%20spec%20char/filename.ext
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
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.
Sool
Posts: 4
Joined: Tue Dec 12, 2017 4:47 am

Re: Save Calc with VB

Post by Sool »

Thanks for the info but what I code works perfectly, it first checks if there is an existing file called with by the current month and year, if not, opens a template, both ways data is inserted in the Calc, at the end uses store() if Calc was opened with an existing file, otherwise uses storeAsURL() for saving a new file through the template and there hasn't been any problem.
OpenOffice 4.1 - Windows 10
Post Reply