Advertisement
iTA360

Openoffice Save Backup of Files with Time Date Year Month Day Hour Minute Seconds

May 9th, 2022 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.27 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Sub AutomaticBackup
  4. '------------------------------------------------------------------------
  5. ' This macro saves the document plus a copy, in a backup folder.
  6. ' It adds to the document a TimeStamp, so the filename will be:
  7. ' "mydocument_YYYYMMDD_HHMMSS.xxx"
  8. ' where YYYY represents the current year, MM the month, DD the day,
  9. ' HHMMSS the time in hours, minutes and seconds
  10. '
  11. '------------------------------------------------------------------------
  12. ' History
  13. '------------------------------------------------------------------------
  14. ' v 1.3 2009-10-10 Now works with Windows XP and Linux (Ubuntu)
  15. ' file path format containing "/" or "\"
  16. '
  17. ' v 1.2 2007-04-16 Add several new filters and make the selection
  18. ' of which filters to use for backup
  19. ' relatively easy for the User. Allow save of
  20. ' the document in case it is not yet saved.
  21. '
  22. ' v 1.1.1 2007-04-12 Improve the handling of the path, with possibility
  23. ' of relative path (from current file path) or
  24. ' even empty path (same folder as current file).
  25. '
  26. ' v 1.1.0 2007-04-10 Work with the four main document types of OOo
  27. ' (Writer, Calc, Impress and Draw).
  28. '------------------------------------------------------------------------
  29.  
  30. Dim oDoc as Object
  31. Dim oDocNew as Object
  32. Dim sDocURL as String
  33. Dim sDocNameWithFullPath as String
  34. Dim sNewURL as String
  35. Dim sPathBackupFolder as String
  36. Dim sSaveToURL as String
  37. Dim sTimeStamp as String
  38. Dim sDocName as String
  39. Dim i as Long
  40. Dim j as Long
  41. Dim k as Long
  42. Dim sDocType as String
  43. Dim oDispatcher as Object
  44. Dim sBackup(200) as String
  45. Dim sSlash as String
  46. Dim sOtherSlash as String
  47. Dim s as String
  48.  
  49.  
  50. ' Change the line below to adapt the path of the
  51. ' backup folder. It can be:
  52. ' * A full path (ex. "C:\My Documents\BackupFolder")
  53. ' * A relative path starting with a \ (ex. "\Backup")
  54. ' * Empty
  55. sPathBackupFolder = "C:\BackupDocs"
  56.  
  57. ' This is the array that defines which extensions to use for
  58. ' backup purposes, based on the type of document
  59. ' Simply add the word "BACKUP" (without quotes) between the first
  60. ' two vertical bars and the program will behave accordingly
  61. ' Each string contains five parameters which are:
  62. ' - Module in OOo
  63. ' - The word BACKUP or an empty string
  64. ' - A free text describing the format
  65. ' - The extension of the file
  66. ' - The name of the filter that will write the file in the proper format
  67. i = 0
  68.  
  69. ' I strongly recommend that you keep these four lines as they are
  70. ' which means that the backup copy will retain all the formatting
  71. ' created in OOo as they are done using native OOo file types
  72. i = i + 1 : sBackup(i) = "Calc|BACKUP|Open Document Spreadsheet|ods|Calc8"
  73. i = i + 1 : sBackup(i) = "Draw|BACKUP|Open Document Drawing|odd|Draw8"
  74. i = i + 1 : sBackup(i) = "Impress|BACKUP|Open Document Presentation|odp|Impress8"
  75. i = i + 1 : sBackup(i) = "Writer|BACKUP|Open Document Text|odt|Writer8"
  76.  
  77. ' If you would like supplemental backups, feel free to add
  78. ' the word BACKUP between the first two vertical bars. Note that if
  79. ' you select several file types with the same extension, only the
  80. ' backup file with the last filter will be actually saved
  81. ' Only change the lines below if you often need to save your documents
  82. ' in non-native OOo formats
  83. i = i + 1 : sBackup(i) = "Calc||OpenOffice.org 1.0 Spreadsheet Template|stc|calc_StarOffice_XML_Calc_Template"
  84. i = i + 1 : sBackup(i) = "Calc||OpenOffice.org 1.0 Spreadsheet|sxc|StarOffice XML (Calc)"
  85. i = i + 1 : sBackup(i) = "Calc||Data Interchange Format|dif|DIF"
  86. i = i + 1 : sBackup(i) = "Calc||Microsoft Excel 97/2000/XP Template|xlt|MS Excel 97 Vorlage/Template"
  87. i = i + 1 : sBackup(i) = "Calc||HTML Document (OpenOffice.org Calc)|html|HTML (StarCalc)"
  88. i = i + 1 : sBackup(i) = "Calc||Microsoft Excel 5.0|xls|MS Excel 5.0/95"
  89. i = i + 1 : sBackup(i) = "Calc||Microsoft Excel 95|xls|MS Excel 95"
  90. i = i + 1 : sBackup(i) = "Calc||StarCalc 4.0|sdc|StarCalc 4.0"
  91. i = i + 1 : sBackup(i) = "Calc||Microsoft Excel 97/2000/XP|xls|MS Excel 97"
  92. i = i + 1 : sBackup(i) = "Calc||OpenDocument Spreadsheet Template|ots|calc8_template"
  93. i = i + 1 : sBackup(i) = "Calc||Text CSV|csv|Text - txt - csv (StarCalc)"
  94. i = i + 1 : sBackup(i) = "Calc||StarCalc 3.0 Template|vor|StarCalc 3.0 Vorlage/Template"
  95. i = i + 1 : sBackup(i) = "Calc||StarCalc 4.0 Template|vor|StarCalc 4.0 Vorlage/Template"
  96. i = i + 1 : sBackup(i) = "Calc||StarCalc 5.0 Template|vor|StarCalc 5.0 Vorlage/Template"
  97. i = i + 1 : sBackup(i) = "Calc||Pocket Excel|pxl|Pocket Excel"
  98. i = i + 1 : sBackup(i) = "Calc||Microsoft Excel 95 Template|xlt|MS Excel 95 Vorlage/Template"
  99. i = i + 1 : sBackup(i) = "Calc||dBase|dbf|dBase"
  100. i = i + 1 : sBackup(i) = "Calc||Microsoft Excel 2003 XML|xml|MS Excel 2003 XML"
  101. i = i + 1 : sBackup(i) = "Calc||XHTML|xml|XHTML Calc File"
  102. i = i + 1 : sBackup(i) = "Calc||Microsoft Excel 5.0 Template|xlt|MS Excel 5.0/95 Vorlage/Template"
  103. i = i + 1 : sBackup(i) = "Calc||StarCalc 3.0|sdc|StarCalc 3.0"
  104. i = i + 1 : sBackup(i) = "Calc||StarCalc 5.0|sdc|StarCalc 5.0"
  105. i = i + 1 : sBackup(i) = "Calc||PDF - Portable Document Format|pdf|calc_pdf_Export"
  106. i = i + 1 : sBackup(i) = "Calc||SYLK|slk|SYLK"
  107. i = i + 1 : sBackup(i) = "Draw||PNG - Portable Network Graphic|png|draw_png_Export"
  108. i = i + 1 : sBackup(i) = "Draw||JPEG - Joint Photographic Experts Group|jpg|draw_jpg_Export"
  109. i = i + 1 : sBackup(i) = "Draw||PPM - Portable Pixelmap|ppm|draw_ppm_Export"
  110. i = i + 1 : sBackup(i) = "Draw||WMF - Windows Metafile|wmf|draw_wmf_Export"
  111. i = i + 1 : sBackup(i) = "Draw||PGM - Portable Graymap|pgm|draw_pgm_Export"
  112. i = i + 1 : sBackup(i) = "Draw||TIFF - Tagged Image File Format|tiff|draw_tif_Export"
  113. i = i + 1 : sBackup(i) = "Draw||PBM - Portable Bitmap|pbm|draw_pbm_Export"
  114. i = i + 1 : sBackup(i) = "Draw||EMF - Enhanced Metafile|emf|draw_emf_Export"
  115. i = i + 1 : sBackup(i) = "Draw||XPM - X PixMap|xpm|draw_xpm_Export"
  116. i = i + 1 : sBackup(i) = "Draw||OpenDocument Drawing Template|otg|draw8_template"
  117. i = i + 1 : sBackup(i) = "Draw||BMP - Windows Bitmap|bmp|draw_bmp_Export"
  118. i = i + 1 : sBackup(i) = "Draw||Macromedia Flash (SWF)|swf|draw_flash_Export"
  119. i = i + 1 : sBackup(i) = "Draw||OpenOffice.org 1.0 Drawing|sxd|StarOffice XML (Draw)"
  120. i = i + 1 : sBackup(i) = "Draw||PDF - Portable Document Format|pdf|draw_pdf_Export"
  121. i = i + 1 : sBackup(i) = "Draw||GIF - Graphics Interchange Format|gif|draw_gif_Export"
  122. i = i + 1 : sBackup(i) = "Draw||RAS - Sun Raster Image|ras|draw_ras_Export"
  123. i = i + 1 : sBackup(i) = "Draw||StarDraw 3.0 Template|vor|StarDraw 3.0 Vorlage"
  124. i = i + 1 : sBackup(i) = "Draw||StarDraw 5.0 Template|vor|StarDraw 5.0 Vorlage"
  125. i = i + 1 : sBackup(i) = "Draw||HTML Document (OpenOffice.org Draw)|html|draw_html_Export"
  126. i = i + 1 : sBackup(i) = "Draw||StarDraw 3.0|sxd|StarDraw 3.0"
  127. i = i + 1 : sBackup(i) = "Draw||MET - OS/2 Metafile|met|draw_met_Export"
  128. i = i + 1 : sBackup(i) = "Draw||StarDraw 5.0|sxd|StarDraw 5.0"
  129. i = i + 1 : sBackup(i) = "Draw||XHTML|xml|XHTML Draw File"
  130. i = i + 1 : sBackup(i) = "Draw||EPS - Encapsulated PostScript|eps|draw_eps_Export"
  131. i = i + 1 : sBackup(i) = "Draw||SVG - Scalable Vector Graphics|svg|draw_svg_Export"
  132. i = i + 1 : sBackup(i) = "Draw||OpenOffice.org 1.0 Drawing Template|std|draw_StarOffice_XML_Draw_Template"
  133. i = i + 1 : sBackup(i) = "Draw||SVM - StarView Metafile|svm|draw_svm_Export"
  134. i = i + 1 : sBackup(i) = "Impress||StarDraw 3.0 Template (OpenOffice.org Impress)|vor|StarDraw 3.0 Vorlage (StarImpress)"
  135. i = i + 1 : sBackup(i) = "Impress||StarDraw 5.0 Template (OpenOffice.org Impress)|vor|StarDraw 5.0 Vorlage (StarImpress)"
  136. i = i + 1 : sBackup(i) = "Impress||Microsoft PowerPoint 97/2000/XP Template|pot|MS PowerPoint 97 Vorlage"
  137. i = i + 1 : sBackup(i) = "Impress||OpenOffice.org 1.0 Presentation Template|sti|impress_StarOffice_XML_Impress_Template"
  138. i = i + 1 : sBackup(i) = "Impress||StarImpress 5.0|sdd|StarImpress 5.0"
  139. i = i + 1 : sBackup(i) = "Impress||StarDraw 5.0 (OpenOffice.org Impress)|sda|StarDraw 5.0 (StarImpress)"
  140. i = i + 1 : sBackup(i) = "Impress||BMP - Windows Bitmap|bmp|impress_bmp_Export"
  141. i = i + 1 : sBackup(i) = "Impress||EMF - Enhanced Metafile|emf|impress_emf_Export"
  142. i = i + 1 : sBackup(i) = "Impress||EPS - Encapsulated PostScript|eps|impress_eps_Export"
  143. i = i + 1 : sBackup(i) = "Impress||GIF - Graphics Interchange Format|gif|impress_gif_Export"
  144. i = i + 1 : sBackup(i) = "Impress||JPEG - Joint Photographic Experts Group|jpg|impress_jpg_Export"
  145. i = i + 1 : sBackup(i) = "Impress||MET - OS/2 Metafile|met|impress_met_Export"
  146. i = i + 1 : sBackup(i) = "Impress||PBM - Portable Bitmap|pbm|impress_pbm_Export"
  147. i = i + 1 : sBackup(i) = "Impress||PCT - Mac Pict|pct|impress_pct_Export"
  148. i = i + 1 : sBackup(i) = "Impress||PDF - Portable Document Format|pdf|impress_pdf_Export"
  149. i = i + 1 : sBackup(i) = "Impress||PGM - Portable Graymap|pgm|impress_pgm_Export"
  150. i = i + 1 : sBackup(i) = "Impress||PNG - Portable Network Graphic|png|impress_png_Export"
  151. i = i + 1 : sBackup(i) = "Impress||PPM - Portable Pixelmap|ppm|impress_ppm_Export"
  152. i = i + 1 : sBackup(i) = "Impress||RAS - Sun Raster Image|ras|impress_ras_Export"
  153. i = i + 1 : sBackup(i) = "Impress||SVG - Scalable Vector Graphics|svg|impress_svg_Export"
  154. i = i + 1 : sBackup(i) = "Impress||SVM - StarView Metafile|svm|impress_svm_Export"
  155. i = i + 1 : sBackup(i) = "Impress||TIFF - Tagged Image File Format|tiff|impress_tif_Export"
  156. i = i + 1 : sBackup(i) = "Impress||WMF - Windows Metafile|wmf|impress_wmf_Export"
  157. i = i + 1 : sBackup(i) = "Impress||XPM - X PixMap|xpm|impress_xpm_Export"
  158. i = i + 1 : sBackup(i) = "Impress||PWP - PlaceWare|pwp|placeware_Export"
  159. i = i + 1 : sBackup(i) = "Impress||OpenOffice.org 1.0 Drawing (OpenOffice.org Impress)|odg|OpenOffice.org 1.0 Drawing (OpenOffice.org Impress)"
  160. i = i + 1 : sBackup(i) = "Impress||XHTML|xml|XHTML Impress File"
  161. i = i + 1 : sBackup(i) = "Impress||Macromedia Flash (SWF)|swf|impress_flash_Export"
  162. i = i + 1 : sBackup(i) = "Impress||Microsoft PowerPoint 97/2000/XP|ppt|MS PowerPoint 97"
  163. i = i + 1 : sBackup(i) = "Impress||StarImpress 4.0|sdd|StarImpress 4.0"
  164. i = i + 1 : sBackup(i) = "Impress||OpenDocument Presentation Template|stp|impress8_template"
  165. i = i + 1 : sBackup(i) = "Impress||StarImpress 4.0 Template|vor|StarImpress 4.0 Vorlage"
  166. i = i + 1 : sBackup(i) = "Impress||StarImpress 5.0 Template|vor|StarImpress 5.0 Vorlage"
  167. i = i + 1 : sBackup(i) = "Impress||OpenOffice.org 1.0 Presentation|sxi|StarOffice XML (Impress)"
  168. i = i + 1 : sBackup(i) = "Impress||StarDraw 3.0 (OpenOffice.org Impress)|sdd|StarDraw 3.0 (StarImpress)"
  169. i = i + 1 : sBackup(i) = "Impress||HTML Document (OpenOffice.org Impress)|html|impress_html_Export"
  170. i = i + 1 : sBackup(i) = "Writer||OpenOffice.org 1.0 Text Document|sxw|StarOffice XML (Writer)"
  171. i = i + 1 : sBackup(i) = "Writer||StarWriter 4.0|sdw|StarWriter 4.0"
  172. i = i + 1 : sBackup(i) = "Writer||HTML Document (OpenOffice.org Writer)|html|HTML (StarWriter)"
  173. i = i + 1 : sBackup(i) = "Writer||StarWriter 3.0 Template|vor|StarWriter 3.0 Vorlage/Template"
  174. i = i + 1 : sBackup(i) = "Writer||StarWriter 4.0 Template|vor|StarWriter 4.0 Vorlage/Template"
  175. i = i + 1 : sBackup(i) = "Writer||StarWriter 5.0 Template|vor|StarWriter 5.0 Vorlage/Template"
  176. i = i + 1 : sBackup(i) = "Writer||OpenOffice.org 1.0 Text Document Template|stw|writer_StarOffice_XML_Writer_Template"
  177. i = i + 1 : sBackup(i) = "Writer||Microsoft Word 2003 XML|xml|MS Word 2003 XML"
  178. i = i + 1 : sBackup(i) = "Writer||Pocket Word|psw|PocketWord File"
  179. i = i + 1 : sBackup(i) = "Writer||Rich Text Format|rtf|Rich Text Format"
  180. i = i + 1 : sBackup(i) = "Writer||PDF - Portable Document Format|pdf|writer_pdf_Export"
  181. i = i + 1 : sBackup(i) = "Writer||StarWriter 3.0|sdw|StarWriter 3.0"
  182. i = i + 1 : sBackup(i) = "Writer||OpenDocument Text Template|ott|writer8_template"
  183. i = i + 1 : sBackup(i) = "Writer||AportisDoc (Palm)|pdb|AportisDoc Palm DB"
  184. i = i + 1 : sBackup(i) = "Writer||BibTeX|bib|BibTeX_Writer"
  185. i = i + 1 : sBackup(i) = "Writer||StarWriter 5.0|sdw|StarWriter 5.0"
  186. i = i + 1 : sBackup(i) = "Writer||Microsoft Word 6.0|doc|MS WinWord 6.0"
  187. i = i + 1 : sBackup(i) = "Writer||Text Encoded|txt|Text (encoded)"
  188. i = i + 1 : sBackup(i) = "Writer||Microsoft Word 95|doc|MS Word 95"
  189. i = i + 1 : sBackup(i) = "Writer||Microsoft Word 97/2000/XP|doc|MS Word 97"
  190. i = i + 1 : sBackup(i) = "Writer||LaTeX 2e|ltx|LaTeX_Writer"
  191. i = i + 1 : sBackup(i) = "Writer||XHTML|html|XHTML Writer File"
  192. i = i + 1 : sBackup(i) = "Writer||Text|txt|Text"
  193. i = i + 1 : sBackup(i) = "Writer||DocBook|xml|DocBook File"
  194.  
  195. ' If this is a new document, we must save it first
  196. ' in order to later get its path and filter type
  197. oDoc = ThisComponent
  198. If Not(oDoc.hasLocation) Then
  199. oDocNew = ThisComponent.CurrentController.Frame
  200. oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
  201. oDispatcher.executeDispatch(oDocNew, ".uno:SaveAs", "", 0, array())
  202. End If
  203.  
  204. ' If the document still has no location, then we
  205. ' display an understandable error message
  206. If Not(oDoc.hasLocation) Then
  207. Msgbox "Your document has NOT been saved, and NO backup copy will be created. " & _
  208. "May be you tried to save your document on a folder where you cannot write, " & _
  209. "or you tried to override a file already in use. Please check and run again."
  210. Exit Sub
  211. End If
  212.  
  213. ' Initialization of variables
  214. ' Retrieve the document name and path from the URL
  215. ' We also calculate the timestamp, so it will be
  216. ' identical for all the backup copies
  217. sDocURL = oDoc.getLocation()
  218. sDocNameWithFullPath = ConvertFromURL(sDocURL)
  219. sTimeStamp = "_" & Format(Year(Now), "0000") & _
  220. Format(Month(Now), "00") & _
  221. Format(Day(Now), "00") & "_" & _
  222. Format(Hour(Now), "00") & _
  223. Format(Minute(Now), "00") & _
  224. Format(Second(Now), "00")
  225.  
  226. ' Determine whether the path should be written with
  227. ' "/" or "\" to indicate the hierarchy of folders
  228. ' (Modification of v1.3 to work with Windows and Ubuntu)
  229. If Instr(1, sDocNameWithFullPath, "/") > 0 Then
  230. sSlash = "/"
  231. sOtherSlash = "\"
  232. Else
  233. sSlash = "\"
  234. sOtherSlash= "/"
  235. End If
  236. sDocName = GetFileName(sDocNameWithFullPath, sSlash)
  237.  
  238. ' If the Backup path contains the wrong delimiter,
  239. ' we replace it
  240. s = ""
  241. For i = 1 to Len(sPathBackupFolder)
  242. If Mid(sPathBackupFolder, i, 1) = sOtherSlash Then
  243. s = s & sSlash
  244. Else
  245. s = s & Mid(sPathBackupFolder, i, 1)
  246. End If
  247. Next i
  248. sPathBackupFolder = s
  249.  
  250. ' If the backupfolder variable is empty, or starts with a \ or /
  251. ' indicating a sub-folder, then we will use the
  252. ' current folder of the document and append the sub-folder
  253. If sPathBackupFolder = "" or Left(sPathBackupFolder, 1) = sSlash Then
  254. i = Len(sDocNameWithFullPath)
  255. While Mid(sDocNameWithFullPath, i, 1) <> sSlash
  256. i = i - 1
  257. Wend
  258. sPathBackupFolder = Left(sDocNameWithFullPath, i - 1) & sPathBackupFolder
  259. End If
  260.  
  261. ' Check if the backup folder exists, if not we create it
  262. On Error Resume Next
  263. MkDir sPathBackupFolder
  264. On Error Goto 0
  265.  
  266. ' Add a slash at the end of the path,
  267. ' if not already present
  268. If Right(sPathBackupFolder, 1) <> sSlash Then
  269. sPathBackupFolder = sPathBackupFolder & sSlash
  270. End If
  271.  
  272. ' Save the current document only if it has changed,
  273. ' is not new (has been saved before) and is not read only
  274. If oDoc.isModified and oDoc.hasLocation and Not(oDoc.isReadOnly) Then
  275. oDoc.store()
  276. End If
  277.  
  278. ' For each file filter, let's see whether we should
  279. ' create a backup copy or not
  280. i = 1
  281. While sBackup(i) <> ""
  282. If GetString(sBackup(i), "|", 2) = "BACKUP" Then
  283.  
  284. sDocType = GetString(sBackup(i), "|", 1)
  285. If (oDoc.supportsService("com.sun.star.text.TextDocument") And sDocType = "Writer") Or _
  286. (oDoc.supportsService("com.sun.star.sheet.SpreadsheetDocument") And sDocType = "Calc") Or _
  287. (oDoc.supportsService("com.sun.star.presentation.PresentationDocument") And sDocType = "Impress") Or _
  288. (oDoc.supportsService("com.sun.star.drawing.DrawingDocument") And sDocType = "Draw") Then
  289.  
  290. sSaveToURL = ConvertToURL(sPathBackupFolder & sDocName & sTimeStamp & "." & GetString(sBackup(i), "|", 4))
  291. oDoc.storeToUrl(sSaveToURL, Array(MakePropertyValue("FilterName", GetString(sBackup(i), "|", 5))))
  292.  
  293. End If
  294.  
  295. End If
  296. i = i + 1
  297. Wend
  298.  
  299. End Sub
  300.  
  301.  
  302. Function MakePropertyValue(Optional sName As String, Optional sValue) As com.sun.star.beans.PropertyValue
  303. '-------------------------------------------------------------------
  304. ' Create and return a new com.sun.star.beans.PropertyValue
  305. '-------------------------------------------------------------------
  306.  
  307. Dim oPropertyValue As New com.sun.star.beans.PropertyValue
  308.  
  309.  
  310. If Not IsMissing(sName) Then
  311. oPropertyValue.Name = sName
  312. EndIf
  313.  
  314. If Not IsMissing(sValue) Then
  315. oPropertyValue.Value = sValue
  316. EndIf
  317.  
  318. MakePropertyValue() = oPropertyValue
  319.  
  320. End Function
  321.  
  322.  
  323. Function GetFileName(byVal sNameWithFullPath as String, byval sSlash as String) as String
  324. '-------------------------------------------------------------------
  325. ' Return the name of the file without path nor extension from a string
  326. ' that contains the full name (including path) of a file
  327. '-------------------------------------------------------------------
  328.  
  329. Dim i as Long
  330. Dim j as Long
  331.  
  332.  
  333. GetFileName = ""
  334.  
  335. ' Let's search from the end of the full name
  336. ' a "." that will indicate the end of the
  337. ' file name and the beginning of the extension
  338. For i = Len(sNameWithFullPath) To 1 Step -1
  339. If Mid(sNameWithFullPath, i, 1) = "." Then
  340.  
  341. ' We have found a ".", so now we continue
  342. ' backwards and search for the path delimiter "\" or "/"
  343. For j = i - 1 to 1 Step -1
  344. If Mid(sNameWithFullPath, j, 1) = sSlash Then
  345.  
  346. ' We have found it, the file name is the
  347. ' piece of string between the two
  348. GetFileName = Mid(sNameWithFullPath, j + 1, i - j - 1)
  349. j = 0
  350. i = 0
  351. End If
  352. Next j
  353. End If
  354. Next i
  355.  
  356. End Function
  357.  
  358.  
  359. Function GetString(byVal sInput as String, byVal sDelimiter as String, ByVal iPosition as Long) as String
  360. '-------------------------------------------------------------------
  361. ' This function returns a substring from sInput, located after
  362. ' the iPositionth of sDelimiter and the next occurence
  363. ' of sDelimiter or the end of the string, whatever occurs first
  364. '-------------------------------------------------------------------
  365.  
  366. Dim i as Long
  367. Dim j as Long
  368. Dim k as Long
  369.  
  370.  
  371. ' Let's add a delimiter at the beginning and end of the string
  372. ' it makes the processing easier for the first and last portion
  373. ' of the string
  374. sInput = sDelimiter & sInput & sDelimiter
  375.  
  376. ' Find the nth delimiter
  377. j = 0
  378. For i = 1 to iPosition
  379. j = InStr(j + 1, sInput, sDelimiter)
  380. If j = 0 Then
  381. GetString = ""
  382. Exit Function
  383. End If
  384. Next i
  385.  
  386. ' Find the delimiter n+1
  387. If j = Len(sInput) Then
  388. GetString = ""
  389. Exit Function
  390. End If
  391.  
  392. k = InStr(j + 1, sInput, sDelimiter)
  393.  
  394.  
  395. ' Return the portion of string between the two delimiters
  396. If k = j + 1 Then
  397. GetString = ""
  398. Else
  399. GetString = Trim(Mid(sInput, j + 1, k - j - 1))
  400. End If
  401.  
  402. End Function
  403.  
  404.  
  405. Function GetFilterType(byVal sFileName as String) as String
  406. '-------------------------------------------------------------------
  407. ' This function returns the filter type of a file
  408. ' Credit: http://www.oooforum.org/forum/viewtopic.phtml?t=52047
  409. '-------------------------------------------------------------------
  410.  
  411. Dim sURL as String
  412. Dim oSFA as Object
  413. Dim oInpStream as Object
  414. Dim oTD as Object
  415. Dim aProps(0) as new com.sun.star.beans.PropertyValue
  416.  
  417.  
  418. sUrl = ConvertToUrl(sFileName)
  419.  
  420. oSFA = createUNOService ("com.sun.star.ucb.SimpleFileAccess")
  421. oInpStream = oSFA.openFileRead(sUrl)
  422.  
  423. aProps(0).Name = "InputStream"
  424. aProps(0).Value = oInpStream
  425. oTD = createUnoService("com.sun.star.document.TypeDetection")
  426. GetFilterType = oTD.queryTypeByDescriptor(aProps(), true)
  427.  
  428. oInpStream.closeInput()
  429.  
  430. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement