Scrittura delle macro senza l'utilizzo del registratore

From Apache OpenOffice Wiki
Jump to: navigation, search






Pagina in traduzione - translation in progress




Gli esempi raccolti in questo capitolo sono stati creati utilizzando il registratore macro e il gestore di eventi. Potete anche scrivere macro che accedono direttamente agli oggetti che sono inclusi in OpenOffice.org. In altre parole, potete manipolare direttamente un documento.

Manipolare direttamente gli oggetti interni di OpenOffice.org è un argomento avanzato che va oltre gli scopi di questo capitolo. Tuttavia potete vederne il funzionamento tramite un piccolo esempio.

Listato 1: Aggiungere il testo "Ciao" alla fine del documento corrente.


Sub AggiungiCiao
  Dim oDoc
  Dim sTextService$
  Dim oCurs
  REM ThisComponent fa riferimento al documento attualmente attivo.
  oDoc = ThisComponent
  REM Verifica che si tratti di un documento di testo 
  sTextService = "com.sun.star.text.TextDocument"
  If NOT oDoc.supportsService(sTextService) Then
    MsgBox "Questa macro funziona solamente con documenti di testo"
    Exit Sub
  End If
  REM Prende il controllo del cursore del controller corrente.
  oCurs = oDoc.currentController.getViewCursor()
  REM Muove il cursore alla fine del documento
  oCurs.gotoEnd(False)
  REM Inserisce il testo "Ciao" alla fine del documento
  oCurs.Text.insertString(oCurs, "Ciao", False)  
End Sub








The examples covered in this chapter are created using the macro recorder and the dispatcher. You can also write macros that directly access the objects that comprise OpenOffice.org. In other words, you can directly manipulate a document.

Directly manipulating OOo's internal objects is an advanced topic that is beyond the scope of this chapter. A simple example, however, demonstrates how this works.

Listing 4: Append the text “Hello" to the current document.

 Sub AppendHello
   Dim oDoc
   Dim sTextService$
   Dim oCurs
   
   REM ThisComponent refers to the currently active document.
   oDoc = ThisComponent
 
   REM Verify that this is a text document  
   sTextService = "com.sun.star.text.TextDocument"
   If NOT oDoc.supportsService(sTextService) Then
     MsgBox "This macro only works with a text document"
     Exit Sub
   End If
 
   REM Get the view cursor from the current controller.
   oCurs = oDoc.currentController.getViewCursor()
 
   REM Move the cursor to the end of the document
   oCurs.gotoEnd(False)
 
   REM Insert text "Hello" at the end of the document
   oCurs.Text.insertString(oCurs, "Hello", False)  
 End Sub

Content on this page is licensed under the Creative Common Attribution 3.0 license (CC-BY).
Personal tools