Main.bas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. REM ***** BASIC *****
  2. Dim Interactive As Boolean
  3. Dim WaitFor
  4. Function Convert(Optional inFileURL, Optional filterSpec, Optional outFileURL)
  5. Dim inDoc, inDocType, openParams, closeInDoc, presentationDoc
  6. ' Set Interactivity i.e., LogMessage pops up a message.
  7. Interactive = False
  8. WaitFor = 10
  9. ' Init dependencies
  10. BasicLibraries.LoadLibrary("Tools")
  11. ' BasicLibraries.LoadLibrary("XrayTool")
  12. ' Setup Export filters
  13. InitExportFilters
  14. ' Export to doc format by default
  15. If IsMissing(filterSpec) Then
  16. If Interactive Then
  17. filterSpec = InputBox("Export to: ")
  18. Else
  19. filterSpec = "doc"
  20. End If
  21. End If
  22. filterSpec = Trim(filterSpec)
  23. closeInDoc = False
  24. If IsMissing(inFileURL) Then
  25. ' Most likely, the Macro is run interactively. Act on
  26. ' the current document
  27. If Not ThisComponent.HasLocation() Then
  28. LogMessage("Document doesn't have a location")
  29. Goto Failure
  30. End If
  31. inDoc = ThisComponent
  32. inFileURL = inDoc.GetLocation()
  33. closeInDoc = False
  34. Else
  35. ' Load the document
  36. On Error Goto Failure
  37. openParams = Array(MakePropertyValue("Hidden", True),MakePropertyValue("ReadOnly", True),)
  38. 'openParams = Array()
  39. inDoc = StarDesktop.loadComponentFromURL(inFileURL, "_blank", 0, OpenParams())
  40. closeInDoc = True
  41. End If
  42. If IsMissing(outFileURL) Then
  43. outFileURL = GetURLWithoutExtension(inFileURL)
  44. End If
  45. If ExportDocument(inDoc, filterSpec, outFileURL) Then
  46. Goto Success
  47. End If
  48. LogMessage("filterSpec1 is " & filterSpec)
  49. ' Export didn't go through. Maybe didn't find a valid filter.
  50. ' Check whether the request is to convert a Text or a Web
  51. ' Document to a Presentation Document
  52. inDocType = GetDocumentType(inDoc)
  53. If (inDocType = "com.sun.star.text.TextDocument" Or _
  54. inDocType = "com.sun.star.text.WebDocument") Then
  55. LogMessage("Filterspec2 is " & filterSpec)
  56. filter = GetFilter("com.sun.star.presentation.PresentationDocument", filterSpec)
  57. If IsNull(filter) Then
  58. LogMessage("We tried our best. Nothing more to do"
  59. Goto Failure
  60. Else
  61. LogMessage("Trying to create presentation document. Found valid filter for " & filterSpec)
  62. End If
  63. Else
  64. Goto Failure
  65. End If
  66. ' Export Outline to Presentation
  67. dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
  68. dispatcher.executeDispatch(inDoc.CurrentController.Frame, ".uno:SendOutlineToStarImpress", "", 0, Array())
  69. ' Dispatch event above is aynchronous. Wait for a few seconds for the above event to finish
  70. Wait(WaitFor * 1000)
  71. ' After the dispatch, the current component is a presentation
  72. ' document. Note that it doesn't have a location
  73. presentationDoc = ThisComponent
  74. If IsNull(ExportDocument(presentationDoc, filter, outFileURL)) Then
  75. Goto Failure
  76. Else
  77. presentationDoc.Close(True)
  78. End If
  79. Success:
  80. LogMessage("Successfully exported to " & outFileURL )
  81. Goto Done
  82. Failure:
  83. LogMessage("Export failed " & outFileURL )
  84. Goto Done
  85. Done:
  86. If closeInDoc Then
  87. inDoc.Close(True)
  88. End If
  89. End Function
  90. ' http://codesnippets.services.openoffice.org/Writer/Writer.MergeDocs.snip
  91. ' http://user.services.openoffice.org/en/forum/viewtopic.php?f=20&t=39983
  92. ' http://user.services.openoffice.org/en/forum/viewtopic.php?f=21&t=23531
  93. ' http://wiki.services.openoffice.org/wiki/Documentation/BASIC_Guide/Files_and_Directories_%28Runtime_Library%29
  94. Function ExportDocument(inputDoc, filterSpec, outFileURL) As Boolean
  95. Dim inputDocType, filter
  96. ExportDocument = False
  97. On Error Goto Failure
  98. inputDocType = GetDocumentType(inputDoc)
  99. If IsArray(filterSpec) Then
  100. ' Filter is fully specified
  101. filter = filterSpec
  102. Else
  103. ' Filter is specified by it's name
  104. filter = GetFilter(inputDocType, filterSpec)
  105. End If
  106. If InStr(outFileURL, ".") = 0 Then
  107. outFileURL = outFileURL & "." & FilterSaveExtension(filter)
  108. End If
  109. LogMessage("outFileURL is " & outFileURL)
  110. inputDoc.storeToUrl(outFileURL, Array(MakePropertyValue("FilterName", FilterHandler(filter))))
  111. ExportDocument = True
  112. LogMessage("Export to " & outFileURL & " succeeded")
  113. Done:
  114. Exit Function
  115. Failure:
  116. LogMessage("Export to " & outFileURL & " failed")
  117. Resume Done
  118. End Function
  119. Function GetURLWithoutExtension(s As String)
  120. Dim pos
  121. pos = Instr(s, ".")
  122. If pos = 0 Then
  123. GetURLWithoutExtension = s
  124. Else
  125. GetURLWithoutExtension = Left(s, pos - 1)
  126. End If
  127. End Function
  128. Function GetDocumentType(oDoc)
  129. For Each docType in DocTypes
  130. If (oDoc.supportsService(docType)) Then
  131. GetDocumentType = docType
  132. Exit Function
  133. End If
  134. Next docType
  135. GetDocumentType = Nothing
  136. End Function
  137. Function MakePropertyValue(Optional sName As String, Optional sValue) As com.sun.star.beans.PropertyValue
  138. Dim oPropertyValue As New com.sun.star.beans.PropertyValue
  139. If Not IsMissing(sName) Then
  140. oPropertyValue.Name = sName
  141. EndIf
  142. If Not IsMissing(sValue) Then
  143. oPropertyValue.Value = sValue
  144. EndIf
  145. MakePropertyValue() = oPropertyValue
  146. End Function
  147. Sub LogMessage(message)
  148. If Interactive Then
  149. If Err <> 0 Then
  150. Print "Error " & Err & ": " & Error$ & " (line : " & Erl & ")"
  151. End If
  152. Print message
  153. End If
  154. End Sub