1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| 'root object from OOo API
Public oSM As Object '---ServiceManager
Public ODesk As Object '---Desktop (Bureau)
Public oDoc As Object '---Document ouvert
'*****************************************************************************************
Public Sub ouvre_ObjetOOo()
'*****************************************************************************************
'Instanciate OOo : la première ligne est toujours exigée dans Visual Basic pour OOo
oSM = CreateObject("com.sun.star.ServiceManager")
ODesk = oSM.createInstance("com.sun.star.frame.Desktop")
End Sub
'*****************************************************************************************************
Public Sub CommandOuvreDoc_Click()
'*****************************************************************************************************
Dim CheminOOo As String, FichierOOo As String
CheminOOo = ""
FichierOOo = "private:factory/swriter"
ouvre_ObjetOOo()
ouvre_Document(CheminOOo, FichierOOo, "")
End Sub
'*****************************************************************************************************
Public Function ouvre_Document(ByVal CheminOOo As String, ByVal FichierOOo As String, ByVal MDPOOo As String) As Boolean
'*****************************************************************************************************
' ouverture de document avec paramètres
Dim OpenPar(2) As Object
Dim FichierCompletOOo As String
'----------------------------------------------------------------------------
'Nous appelons la fonction de setOOoProp, pour accéder à la structure
OpenPar(0) = setOOoProp("ReadOnly", False)
OpenPar(1) = setOOoProp("Password", MDPOOo)
OpenPar(2) = setOOoProp("Hidden", False)
If CheminOOo = "" Then
FichierCompletOOo = FichierOOo
Else
FichierCompletOOo = CheminOOo & "/" & FichierOOo
End If
'Maintenant nous pouvons appeler la méthode de loadComponentFromURL d'OOo,
'en lui donnant comme quatrième argument le résultat de notre appel précédent de setOOoProp
oDoc = ODesk.loadComponentFromURL(FichierCompletOOo, "_blank", 0, OpenPar)
End Function
'*****************************************************************************************
Public Function setOOoProp(ByVal cName As String, ByVal uValue As Object) As Object
'*****************************************************************************************
'Quelques propriétés d'objet dans OOo api ont la structure de type.
'Les structures sont l'équivalent du type défini pour l'utilisateur (UDT) dans visual basic.
'En raison de l'exécution du pont d'UNO-Automation, il n'est pas possible de passer un UDT
'comme argument de méthode.
'(Cf http://api.openoffice.org/docs/DevelopersGuide/ProfUNO/ProfUNO.htm#1+4+4+5+3+Usage+of+Types).
'dans visual basic, on doit employer une méthode particulière d'OOo api (Bridge_GetStruct):
'il donne l'accès à une structure d'OOo dans visual basic.
'Après ce, on peut donner le résultat de l'appel de Bridge_GetStruct comme argument dans un appel de méthode.
'Vous pourriez par exemple employer une fonction d'aide comme ceci:
Dim oPropertyValue As Object
oPropertyValue = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
oPropertyValue.Name = cName
oPropertyValue.Value = uValue
setOOoProp = oPropertyValue
End Function
Public Sub OOo_Test()
Dim Table, Cols, Rows, Cell, Separator As Object
Dim oText, oCursor As Object
'Dim args()
oText = oDoc.getText()
'Création du curseur d'écriture
oCursor = oText.createTextCursor
'Déplace le curseur a la fin , sans sélection(False)
oCursor.gotoEnd(False)
'insère du texte et un saut de ligne a l'emplacement du curseur , Sans sélection(False)
oText.insertString(oCursor, vbLf & vbLf & vbLf & vbLf & vbLf & vbLf, False)
Table = oDoc.CreateInstance("com.sun.star.text.TextTable")
Table.initialize(6, 5)
oText.insertTextContent(oCursor, Table, False)
Cols = Table.getColumns
Rows = Table.getRows
Separator = Table.TableColumnSeparators
Separator(0).Position = 1000
Separator(1).Position = 4850
Separator(2).Position = 5150
Separator(3).Position = 6150
Table.TableColumnSeparators = Separator
'Inscription des libellés
Cell = Table.getCellByName("E1")
Cell.String = "Test"
Cell = Table.getCellByName("A2")
Cell.String = "A2"
Cell = Table.getCellByName("A3")
Cell.String = "A3"
Cell = Table.getCellByName("A4")
Cell.String = "A4"
Cell = Table.getCellByName("B4")
Cell.String = "B4"
Cell = Table.getCellByName("A5")
Cell.String = "A5"
Cell = Table.getCellByName("B5")
Cell.String = "B5"
Cell = Table.getCellByName("A6")
Cell.String = "A6"
Cell = Table.getCellByName("B6")
Cell.String = "B6"
'Prise en main
oCursor.gotoEnd(False)
End Sub |