Macros/Basic/Documents

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

Return to Index


Active document

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

All open documents

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

New documents

  • Calc
1Dim path As String
2Dim args()
3
4path = "private:factory/scalc"
5doc = 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
1Dim path As String, dbc As Object, db As Object
2Dim args()
3
4path = ConvertToURL("/home/USER/My_db.odb")
5dbc = createUnoService("com.sun.star.sdb.DatabaseContext")
6db = dbc.createInstance()
7db.URL = "sdbc:embedded:firebird"
8db.DatabaseDocument.storeAsURL(path, args())

Open documents

1Dim path As String
2Dim doc As Object
3Dim 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
1Dim path As String
2Dim doc As Object
3Dim 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

 1Dim dlg_select_file as Object
 2Dim files() As String
 3Dim args()
 4Dim path As String
 5Dim 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
1Dim 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
 1Dim 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
 1Dim 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.

 1Dim dlg_save as Object
 2Dim mArchivo() As String
 3Dim 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
 1Dim args(0) As New "com.sun.star.beans.PropertyValue"
 2Dim path As String
 3Dim 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
1args(0).Name = "FilterName"
2args(0).Value = "draw_pdf_Export"
  • For Impress document
1args(0).Name = "FilterName"
2args(0).Value = "impress_pdf_Export"

Set focus

1documents = StarDesktop.getComponents()
2enumeration = documents.createEnumeration()
3Do 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
9Loop

Status bar

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

Zoom

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