Macros/Base/001

    From The Document Foundation Wiki
    < Macros‎ | Base


    Other languages:

    Description

    We would like to manage images without incorporating them in the database. The advantage is that it avoids duplication and especially to not increase the size of the database.

    This feature is accessible without a macro (see Special:MyLanguage/Faq/Base/144). We will see here the properties concerned can be managed via a macro. The desired result is as shown:

    screenshot of form
    Formulaire de saisie/affichage des images
    • The top Text Box allows retrieval of the image via a local filepath or URL.
    • The File Selection button "..." opens a dialog to select local filepath.
    • The bottom Text Box allows for entering a title to the image.
    • The Image Control allows for displaying the image.

    We can realize the above via the two subroutines as shown below.

    Code

    In LibreOffice Basic:

    Option Explicit
    
    Sub display_image()
        
        Dim oForm As Object
        
        oForm = ThisComponent.DrawPage.Forms.getByName("Standard")    
        oForm.getByName("ImageControl").ImageURL = convertToUrl( oForm.getByName("textbox_ImageURL").Text )
    
    End Sub ' display_image
    
    
    Sub save_path(oEvent)
        
        Dim oForm As Object
    
        oForm = ThisComponent.DrawPage.Forms.getByName("Standard")
        oForm.getByName("textbox_ImageURL").Text = oEvent.Source.Text
        oForm.getByName("textbox_ImageURL").commit
        
        display_image()
        
    End Sub ' save_path

    The subroutine display_image is associated with the following events:

    • After a change in saving of the form,
    • If the Image Control receives focus.

    The subroutine save_path is associated with the following events:

    • Loss of focus for a Text Box,
    • Modified text from the File Selection object.

    Example ODB file