Macros/Basic/Documentos

From The Document Foundation Wiki
Jump to: navigation, 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

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

Todos los documentos abiertos

For Each doc In StarDesktop.Components
    MsgBox doc.Title
Next

Nuevos documentos

  • Calc
Dim path As String
Dim args()

path = "private:factory/scalc"
doc = StarDesktop.loadComponentFromURL(path, "_default", 0, args())
  • Para Writer:
"private:factory/swriter"
  • Para Impress:
"private:factory/simpress"
  • Para Draw:
"private:factory/sdraw"
  • Para Math:
"private:factory/smath"
  • Para Base
Dim path As String, dbc As Object, db As Object
Dim args()

path = ConvertToURL("/home/USER/My_db.odb")
dbc = createUnoService("com.sun.star.sdb.DatabaseContext")
db = dbc.createInstance()
db.URL = "sdbc:embedded:firebird"
db.DatabaseDocument.storeAsURL(path, args())

Abrir documentos

Dim path As String
Dim doc As Object
Dim args()

    path = ConvertToUrl("/home/USER/My_file_Calc.ods")
    doc = StarDesktop.loadComponentFromURL(path, "default", 0, args())

Usando argumentos para abrir

  • Para abrir como plantilla
Dim path As String
Dim doc As Object
Dim args(0) As New "com.sun.star.beans.PropertyValue"

    args(0).Name = "AsTemplate"
    args(0).Value = True

    path = ConvertToUrl("/home/USER/My_file_Calc.ods")
    doc = StarDesktop.loadComponentFromURL(path, "default", 0, args())
  • O, abrir plantilla como archivo normal
    args(0).Name = "AsTemplate"
    args(0).Value = False

    path = ConvertToUrl("/home/USER/My_template_Calc.odt")
    doc = StarDesktop.loadComponentFromURL(path, "default", 0, args())
  • Otros argumentos comunes, abrir como solo lectura
    args(0).Name = "ReadOnly"
    args(0).Value = True
  • Con contraseña
    args(0).Name = "Password"
    args(0).Value = "letmein"
  • Con macros activas
    args(0).Name = "MacroExecutionMode"
    args(0).Value = 4
  • Autoniciar presentación ODP
    args(0).Name = "StartPresentation"
    args(0).Value = True
  • Como vista previa
    args(0).Name = "Preview"
    args(0).Value = True
  • Abrir oculto
    args(0).Name = "Hidden"
    args(0).Value = True

Dim dlg_select_file as Object
Dim files() As String
Dim args()
Dim path As String
Dim doc As Object

    dlg_select_file = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
    dlg_select_file.setTitle("Select one file")

    If dlg_select_file.Execute() Then
        path = dlg_select_file.getFiles()(0)
        doc = StarDesktop.loadComponentFromURL(path, "_default", 0, args())
    End If

Guardar documentos

  • Para documentos abiertos.
    path = ConvertToUrl("/home/USER/My_file_Calc.ods")
    doc = StarDesktop.loadComponentFromURL(path, "default", 0, args())

    MsgBox doc.Title

    doc.store()
    doc.close(True)
  • Para documentos nuevos.
Dim args()

    path = "private:factory/scalc"
    doc = StarDesktop.loadComponentFromURL(path, "_default", 0, args())

    path = ConvertToUrl("/home/mau/new_doc_calc.ods")
    doc.storeAsURL(path, args())
  • Usando otras opciones, guardar con contraseña.
Dim args(0) As New "com.sun.star.beans.PropertyValue"

    path = "private:factory/scalc"
    doc = StarDesktop.loadComponentFromURL(path, "_default", 0, args())

    args(0).Name = "Password"
    args(0).Value = "letmein"

    path = ConvertToUrl("/home/mau/new_doc_calc.ods")
    doc.storeAsURL(path, args())
  • Usando filtro para cambiar el formato.
Dim args(0) As New "com.sun.star.beans.PropertyValue"

    path = "private:factory/swriter"
    doc = StarDesktop.loadComponentFromURL(path, "_default", 0, args())

    args(0).Name = "FilterName"
    args(0).Value = "MS Word 97"

    path = ConvertToUrl("/home/mau/new_doc_Word.doc")
    doc.storeAsURL(path, args())

Dim dlg_save as Object
Dim mArchivo() As String
Dim mOpciones()

    dlg_save = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
    'com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION_PASSWORD = 2
    With dlg_save
        .Initialize(Array(2))
        .AppendFilter("Text documents ODF (.odt)", "*.odt" )
        .AppendFilter("Spreadsheet ODF (.ods)", "*.ods" )
    End With

    If dlg_save.Execute() Then
        path = dlg_save.getFiles()(0)
        MsgBox ConvertFromUrl(path)
    End If

Exportar a PDF

  • Para documentos de Calc.
Dim args(0) As New "com.sun.star.beans.PropertyValue"
Dim path As String
Dim doc As Object

    doc = ThisComponent
    args(0).Name = "FilterName"
    args(0).Value = "calc_pdf_Export"

    path = ConvertToURL("/home/USER/output.pdf")
    doc.storeToURL(path, args())
  • Para documentos de Writer.
    args(0).Name = "FilterName"
    args(0).Value = "writer_pdf_Export"