Macros/Basic/Documentos

From The Document Foundation Wiki
Jump to navigation Jump to search
This page is a translated version of the page Macros/Basic/Documents and the translation is 100% complete.
Other languages:
English • ‎español • ‎italiano

Return to Index


Documento activo

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

Todos los documentos abiertos

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

Nuevos documentos

  • Calc
1Dim path As String
2Dim args()
3
4path = "private:factory/scalc"
5doc = StarDesktop.loadComponentFromURL(path, "_default", 0, args())
  • Para Writer:
1"private:factory/swriter"
  • Para Impress:
1"private:factory/simpress"
  • Para Draw:
1"private:factory/sdraw"
  • Para Math:
1"private:factory/smath"
  • Para 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())

Abrir documentos

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())

Usando argumentos para abrir

  • Para abrir como plantilla
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())
  • O, abrir plantilla como archivo normal
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())
  • Otros argumentos comunes, abrir como solo lectura
1    args(0).Name = "ReadOnly"
2    args(0).Value = True
  • Con contraseña
1    args(0).Name = "Password"
2    args(0).Value = "letmein"
  • Con macros activas
1    args(0).Name = "MacroExecutionMode"
2    args(0).Value = 4
  • Autoniciar presentación ODP
1    args(0).Name = "StartPresentation"
2    args(0).Value = True
  • Como vista previa
1    args(0).Name = "Preview"
2    args(0).Value = True
  • Abrir oculto
1    args(0).Name = "Hidden"
2    args(0).Value = True

 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

Guardar documentos

  • Para documentos abiertos.
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)
  • Para documentos nuevos.
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())
  • Usando otras opciones, guardar con contraseña.
 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())
  • Usando filtro para cambiar el formato.
 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())

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

Exportar a PDF

  • Para documentos de Calc.
 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())
  • Para documentos de Writer.
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