-
Notifications
You must be signed in to change notification settings - Fork 2
/
basic_extras_SaveDocAsXLS.bas
64 lines (61 loc) · 3.98 KB
/
basic_extras_SaveDocAsXLS.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'--------------------------------------------------------------------------------------------------'
' SaveDocAsXLS '
'--------------------------------------------------------------------------------------------------'
' Saves document in Microsoft Excel 97/2000/XP XLS format (XLS). '
' '
' Parameters: '
' '
' ByVal FilePath As String '
' Path to save. ByVal keyword prevents it from modification because by default arguments are '
' passed by reference. '
' '
' Examples: '
'--------------------------------------------------------------------------------------------------'
' '
' SaveDocAsXLS("/home/user/document.xls") '
' or '
' SaveDocAsXLS("C:\Users\Admin\Рабочий стол\document.xls") '
' '
'--------------------------------------------------------------------------------------------------'
' See also: '
' FilterName types can be found in "API Name" column at: '
' https://wiki.openoffice.org/wiki/Framework/Article/Filter/FilterList_OOo_2_1 '
' Manual to the ThisComponent.storeAsURL last seen at: '
' https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/StarDesktop '
'--------------------------------------------------------------------------------------------------'
' Feedback & Issues: '
' https://github.com/aa6/libreoffice_calc_basic_extras/issues '
'--------------------------------------------------------------------------------------------------'
Function SaveDocAsXLS(Optional FilePath As String, Optional Document As Object)
Dim SaveParams(1) As New com.sun.star.beans.PropertyValue
SaveParams(0).Name = "Overwrite"
SaveParams(0).Value = TRUE
SaveParams(1).Name = "FilterName"
SaveParams(1).Value = "MS Excel 97"
If IsMissing(Document) Then
Document = ThisComponent
End If
If IsMissing(FilePath) Then
If Document.hasLocation() Then
FilePath = Document.getLocation()
Else
Err.Raise("Document has no default path to save as.")
End If
End If
FilePath = ConvertToURL(FilePath)
If FileExists(FilePath) Then
For Iteration = 1 To 1000
TemporaryPath = FilePath + "." + CStr(CLng(999999 * Rnd)) + ".tmp"
If NOT FileExists(TemporaryPath) Then
Document.storeAsURL(TemporaryPath,SaveParams())
Kill(FilePath)
Document.storeAsURL(FilePath,SaveParams())
Kill(TemporaryPath)
Exit Function
End If
Next Iteration
Err.Raise("Can't generate temporary path to save file.")
Else
Document.storeAsURL(FilePath,SaveParams())
End If
End Function