Macros/Basic/Documents

From The Document Foundation Wiki
Jump to: navigation, search
Other languages:
English • ‎español • ‎italiano

Return to Index


Active document

1 Sub Main()
2     doc = ThisComponent
3     MsgBox doc.Title
4 End Sub

All open documents

1 For Each doc In StarDesktop.Components
2     MsgBox doc.Title
3 Next

New documents

  • Calc
1 Dim path As String
2 Dim args()
3 
4 path = "private:factory/scalc"
5 doc = StarDesktop.loadComponentFromURL(path, "_default", 0, args())
  • For Writer:
1 "private:factory/swriter"
  • For Impress:
1 "private:factory/simpress"
  • For Draw:
1 "private:factory/sdraw"
  • For Math:
1 "private:factory/smath"
  • For Base
1 Dim path As String, dbc As Object, db As Object
2 Dim args()
3 
4 path = ConvertToURL("/home/USER/My_db.odb")
5 dbc = createUnoService("com.sun.star.sdb.DatabaseContext")
6 db = dbc.createInstance()
7 db.URL = "sdbc:embedded:firebird"
8 db.DatabaseDocument.storeAsURL(path, args())

Open documents

1 Dim path As String
2 Dim doc As Object
3 Dim args()
4 
5     path = ConvertToUrl("/home/USER/My_file_Calc.ods")
6     doc = StarDesktop.loadComponentFromURL(path, "default", 0, args())

Using argument for open

  • For open like template
1 Dim path As String
2 Dim doc As Object
3 Dim args(0) As New "com.sun.star.beans.PropertyValue"
4 
5     args(0).Name = "AsTemplate"
6     args(0).Value = True
7 
8     path = ConvertToUrl("/home/USER/My_file_Calc.ods")
9     doc = StarDesktop.loadComponentFromURL(path, "default", 0, args())
  • Or, open normal if file it’s template
1     args(0).Name = "AsTemplate"
2     args(0).Value = False
3 
4     path = ConvertToUrl("/home/USER/My_template_Calc.odt")
5     doc = StarDesktop.loadComponentFromURL(path, "default", 0, args())
  • Other common arguments, open read only.
1     args(0).Name = "ReadOnly"
2     args(0).Value = True
  • With password
1     args(0).Name = "Password"
2     args(0).Value = "letmein"
  • With active macros
1     args(0).Name = "MacroExecutionMode"
2     args(0).Value = 4
  • Automatic start presentation
1     args(0).Name = "StartPresentation"
2     args(0).Value = True
  • Like view preview
1     args(0).Name = "Preview"
2     args(0).Value = True
  • Open hidden
1     args(0).Name = "Hidden"
2     args(0).Value = True

Open with dialog box

 1 Dim dlg_select_file as Object
 2 Dim files() As String
 3 Dim args()
 4 Dim path As String
 5 Dim doc As Object
 6 
 7     dlg_select_file = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
 8     dlg_select_file.setTitle("Select one file")
 9 
10     If dlg_select_file.Execute() Then
11         path = dlg_select_file.getFiles()(0)
12         doc = StarDesktop.loadComponentFromURL(path, "_default", 0, args())
13     End If

Save document

  • For open documents
1     path = ConvertToUrl("/home/USER/My_file_Calc.ods")
2     doc = StarDesktop.loadComponentFromURL(path, "default", 0, args())
3 
4     MsgBox doc.Title
5 
6     doc.store()
7     doc.close(True)
  • For new documents
1 Dim args()
2 
3     path = "private:factory/scalc"
4     doc = StarDesktop.loadComponentFromURL(path, "_default", 0, args())
5 
6     path = ConvertToUrl("/home/mau/new_doc_calc.ods")
7     doc.storeAsURL(path, args())
  • Using other options, save with password
 1 Dim args(0) As New "com.sun.star.beans.PropertyValue"
 2 
 3     path = "private:factory/scalc"
 4     doc = StarDesktop.loadComponentFromURL(path, "_default", 0, args())
 5 
 6     args(0).Name = "Password"
 7     args(0).Value = "letmein"
 8 
 9     path = ConvertToUrl("/home/mau/new_doc_calc.ods")
10     doc.storeAsURL(path, args())
  • Using filter for change format
 1 Dim args(0) As New "com.sun.star.beans.PropertyValue"
 2 
 3     path = "private:factory/swriter"
 4     doc = StarDesktop.loadComponentFromURL(path, "_default", 0, args())
 5 
 6     args(0).Name = "FilterName"
 7     args(0).Value = "MS Word 97"
 8 
 9     path = ConvertToUrl("/home/mau/new_doc_Word.doc")
10     doc.storeAsURL(path, args())

Save with dialog box

Only show the selected path for user, you need add code for save in line 15.

 1 Dim dlg_save as Object
 2 Dim mArchivo() As String
 3 Dim mOpciones()
 4 
 5     dlg_save = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
 6     'com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION_PASSWORD = 2
 7     With dlg_save
 8         .Initialize(Array(2))
 9         .AppendFilter("Text documents ODF (.odt)", "*.odt" )
10         .AppendFilter("Spreadsheet ODF (.ods)", "*.ods" )
11     End With
12 
13     If dlg_save.Execute() Then
14         path = dlg_save.getFiles()(0)
15         MsgBox ConvertFromUrl(path)
16     End If

Export to PDF

  • For Calc document
 1 Dim args(0) As New "com.sun.star.beans.PropertyValue"
 2 Dim path As String
 3 Dim doc As Object
 4 
 5     doc = ThisComponent
 6     args(0).Name = "FilterName"
 7     args(0).Value = "calc_pdf_Export"
 8 
 9     path = ConvertToURL("/home/USER/output.pdf")
10     doc.storeToURL(path, args())
  • For Writer document
1     args(0).Name = "FilterName"
2     args(0).Value = "writer_pdf_Export"
  • For Draw document
1 args(0).Name = "FilterName"
2 args(0).Value = "draw_pdf_Export"
  • For Impress document
1 args(0).Name = "FilterName"
2 args(0).Value = "impress_pdf_Export"

Set focus

1 documents = StarDesktop.getComponents()
2 enumeration = documents.createEnumeration()
3 Do While enumeration.hasMoreElements()
4     doc = enumeration.nextElement()
5     window = doc.CurrentController.Frame.ComponentWindow
6     window.setFocus()
7     REM Sleep 1.5 seconds
8     Wait 1500
9 Loop

Status bar

 1 doc = ThisComponent
 2 status_bar = doc.CurrentController.StatusIndicator
 3 
 4 REM Set initial text and limit value
 5 status_bar.start("In line ", 10)
 6 For i = 1 To 10
 7     status_bar.setValue(i)
 8     status_bar.setText("Line: " & i)
 9     Wait 1000
10 Next
11  
12 REM Important for free status bar        
13 status_bar.end()

Zoom

1 doc = ThisComponent
2 cc = doc.CurrentController
3 cc.ZoomValue = 150