J'utilise le code Listner de PSY merci à lui,
https://forum.openoffice.org/fr/forum/d ... hp?id=3562
avec initialisation de variables globales.
L'événement est validé comme ceci sous LO:
Démarrer l'application Standard.Module1.PysListenerAdd
Document Fermé Standard.Module1.PysListenerRemove
Pb pas de chargement les macros sont inactives et les variables global non initialisées.
Le code ci-dessous se trouve dans:
BibTest.ods->Standard->Module1.
Code : Tout sélectionner
REM ***** BASIC *****
Option Explicit
Global PysDocView as object, PysListener as object, PysPreced as object
Global giligne as Long, giCol as Long, giLivreNo as Long
Global giNameAddressCell as String, gsMemoValCell as String
Global oDoc as object, oSheet as Object, oDocF as object
Global oSel as object
Global oActiveCells as object
'***********************************************************************************
' Procédure à lancer pour mettre en route le traitement
'***********************************************************************************
sub PysListenerAdd
'***********************************************************************************
PysDocView = ThisComponent.getCurrentController 'en gros la fenêtre du document courant
' Si la sélection courante est une cellule ou une plage
if thiscomponent.currentselection.supportsService("com.sun.star.table.CellRange") then
' Mémorise la sélection courante (pour remettre la couleur par défaut quand on
' change de ligne)
PysPreced = thiscomponent.currentselection
' Déclenche la surveillance du changement de sélection à la fenêtre courante
PysListener = createUnoListener("Pys_","com.sun.star.view.XSelectionChangeListener")
PysDocView.addSelectionChangeListener(PysListener)
else
msgbox "Non disponible avec la sélection courante"
end if
end sub
'***********************************************************************************
' Procédure pour arrêter le traitement
sub PysListenerRemove
'***********************************************************************************
if not(isnull(PysListener)) then
thiscomponent.currentselection.rows(0).cellBackColor = -1
PysDocView.removeSelectionChangeListener(PysListener)
end if
end sub
'***********************************************************************************
' Procédure appelée par l'événement "arrêt du listener".
' Nécessaire même si ne fait rien
sub Pys_disposing(PysListener)
'***********************************************************************************
end sub
'***********************************************************************************
' Procédure déclenchée quand changement de sélection
Sub Pys_selectionChanged(PysListener)
'***********************************************************************************
Dim PysEnCours as object
PysEnCours = thiscomponent.currentselection
' Si la sélection courante est une cellule ou une plage
if PysEnCours.supportsService("com.sun.star.table.CellRange") then
'pas d'action pour les premières lignes
' Initialisation var Global
main
select case giligne
case 0 to 2
exit sub
end select
ResumeDispo
' Rétablit la couleur par défaut pour la ligne quittée
PysPreced.rows(0).cellBackColor = -1
' Modifie la couleur de la ligne courante
thiscomponent.currentselection.rows(0).cellBackColor = 13421823
' La ligne courante devient la "précédente"
PysPreced = PysEnCours
end if
End Sub 'Pys_selectionChanged
'********************************************************************************
'********************************************************************************
Sub main
'Iinitialisation des variable se faisant avec Pys_selectionChanged
oDoc = ThisComponent
oDocF = ThisComponent.CurrentController.Frame
oSheet = oDoc.getSheets.getByName("Biblio")
oSel = oDoc.CurrentSelection
oActiveCells = oSel.RangeAddress
giligne = oActiveCells.Startrow
giCol = oActiveCells.StartColumn
giNameAddressCell = oSel.AbsoluteName
giLivreNo = oSheet.getCellByPosition(8, giligne ).Value
gsMemoValCell = oSheet.getCellByPosition(giCol, giligne ).String
oSheet.getCellByPosition(16,0).Value= giligne
oSheet.getCellByPosition(16,1).Value= giCol
oSheet.getCellByPosition(18,0).String = giNameAddressCell
ResumeDispo
End Sub 'main
'**************
Sub AffichResume
'Affiche le résumé du livre
Dim oSelResu , oSelTitre
oSelResu = oSheet.getCellByPosition(18, giligne )
oSelTitre = oSheet.getCellByPosition(2, giligne )
If oSel.supportsService("com.sun.star.sheet.SheetCell") Then
If oSelResu.getString()="" Then
MsgBox (" Pas de résumé disponible ",0,oSelTitre.getString())
Else
MsgBox ( oSelResu.getString(),0,oSelTitre.getString())
End If
End If
End Sub 'AffichResume
'**************
Sub ColleResume
'Colle le résumé qui se trouve dans le presse papier
Dim dispatcher as object
osheet.getCellByPosition(18,giligne ).String= RecupDepuisPP
'oDocF = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = giNameAddressCell
dispatcher.executeDispatch(oDocF, ".uno:GoToCell", "", 0, args1())
End Sub 'ColleResume
'**************
Sub ResumeDispo
' applique ou n'applique pas le caption du btn résumé
Dim nLig as Long
Dim oSheet as Object
Dim oActiveCells as object
Dim oForm as Object
Dim oForms as Object
Dim oControl as Object
oSheet = ThisComponent.Sheets.getByIndex(0)
oForm = oSheet.DrawPage.Forms
oForms = oSheet.DrawPage.Forms.getByIndex(0)
oControl = oForms.getByIndex(3)
If oSheet.getCellByposition(18,giligne).String = "" Then
oControl.Label=""
Else
oControl.Label="Résumé"
End If
End Sub 'ResumeDispo
'****************
Function RecupDepuisPP() As String
Dim oPP as object, oSC as object, oContenu as object, oType as object
Dim i as integer
oPP = createUnoService("com.sun.star.datatransfer.clipboard.SystemClipboard")
oSC = createUnoService("com.sun.star.script.Converter")
oContenu = oPP.getContents
oType = oContenu.getTransferDataFlavors()
For i = 0 To UBound(oType)
If oType(i).MimeType = "text/plain;charset=utf-16" Then Exit For
Next i
RecupDepuisPP = oSC.convertToSimpleType _
(oContenu.getTransferData(oType(i)), _
com.sun.star.uno.TypeClass.STRING)
End Function 'RecupDepuispp
'**************
sub DebutDeTable
'Déplace le focus en début de table
dim dispatcher as object
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$C$4"
dispatcher.executeDispatch(oDocf, ".uno:GoToCell", "", 0, args1())
end sub 'DebutDeTable
'**************
sub FinDeTable
'Déplace le focus en fin de table
dim dispatcher as object
dim nlig as long
nLig = oSheet.getCellByPosition(1,1).Value
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = ConvPosToNameCell(0,nLig)
dispatcher.executeDispatch(oDocF, ".uno:GoToCell", "", 0, args1())
end sub 'FinDeTable
'**************
Function ConvPosToNameCell(x As Long, y As Long) As String
'Conversio (1515,15) => ("Feuille1.$P$1515")
Dim oCell as object
Dim sNom as string
oCell = ThisComponent.sheets("Biblio").getCellByPosition(x,y)
sNom = oCell.AbsoluteName
ConvPosToNameCell= sNom
End Function 'ConvPosToNameCell
'**************
Sub AjoutLivre
' Ajoute un livre en fin de table
Dim nNumLivre as Long, nLig as Long
nLig = oSheet.getCellByPosition(1,1).Value
nNumLivre = oSheet.getCellByPosition(1,0).Value
FinDeTable
osheet.getCellByPosition(0,nLig).String="en attente"
osheet.getCellByPosition(7,nLig).String="V18"
osheet.getCellByPosition(8,nLig).Value = nNumLivre + 1
osheet.getCellByPosition(13,nLig).String="Broche"
osheet.getCellByPosition(14,nLig).String="Fra"
osheet.getCellByPosition(19,nLig).formulaLocal="=TEXTE(MAINTENANT();""JJ/MM/AAAA"")"
End Sub 'AjoutLivre
'**************
Sub TriAs
TriAsOrDs(true)
End Sub 'TriAs
'**************
Sub TriDs
TriAsOrDs(false)
End sub 'Trids
'**************
sub TriAsOrDs(Ascend as Boolean)
'aiguillage du Tri asc ou desc
dim dispatcher as object
Dim sAdd as String
Dim nLig as Long, nMemogiligneAvantTRI as Long
Dim sValCellSpan, nMemogiColAvantTRI as Long
Dim nMemoiLivreNoAvantTRI as Long
nMemogiColAvantTRI = giCol
nMemogiligneAvantTRI = giligne
nMemoiLivreNoAvantTRI = giLivreNo
' Tri Ascend ou descend de la table selon la position du focus
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "FullBiblio"
dispatcher.executeDispatch(oDocF, ".uno:GoToCell", "", 0, args1())
dim args2(9) as new com.sun.star.beans.PropertyValue
args2(0).Name = "ByRows"
args2(0).Value = true
args2(1).Name = "HasHeader"
args2(1).Value = true
args2(2).Name = "CaseSensitive"
args2(2).Value = false
args2(3).Name = "NaturalSort"
args2(3).Value = false
args2(4).Name = "IncludeAttribs"
args2(4).Value = true
args2(5).Name = "UserDefIndex"
args2(5).Value = 0
args2(6).Name = "Col1"
args2(6).Value = nMemogiColAvantTRI + 1
args2(7).Name = "Ascending1"
if Ascend then
args2(7).Value = true
else
args2(7).Value = false
end if
args2(8).Name = "IncludeComments"
args2(8).Value = false
args2(9).Name = "IncludeImages"
args2(9).Value = true
dispatcher.executeDispatch(oDocF, ".uno:DataSort", "", 0, args2())
'Repositionnement sur le livre actif avant le tri
nLig = 3
while nMemoiLivreNoAvantTRI <> sValCellSpan
nLig = nLig + 1
sValCellSpan = oSheet.getCellByPosition(8,nLig).Formula
wend
sAdd = ConvPosToNameCell(nMemogiColAvantTRI,nLig)
dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "ToPoint"
args3(0).Value = sAdd
dispatcher.executeDispatch(oDocF, ".uno:GoToCell", "", 0, args3())
end sub 'TriAsOrDs
'**************
sub EffaceLigneSel
' Efface la ligne sélectionnée
dim dispatcher as object
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(oDocF, ".uno:DeleteRows", "", 0, Array())
end sub
'**************
Sub RecherchWeb
RechercheWebOrBabelio(true)
End Sub 'RecherchWeb
'**************
Sub RcherchBabelio
RechercheWebOrBabelio(false)
End Sub 'RecherchBabelio
'**************
Sub RechercheWebOrBabelio(Web as Boolean)
'Recherche d'un résumé pour le livre
dim LibOUrl as string
dim LibOFlags as long
dim LibOService as object
dim stitre as String, sNom as String, sPrenom as String, sRecherche as String
dim nLig as long, nCol as Long
sNom = oSheet.getCellByPosition(0, giligne ).String
sPrenom = oSheet.getCellByPosition(1, giligne ).String
sTitre = oSheet.getCellByPosition(2, giligne ).String
sRecherche = sTitre & " " & sNom & " " & sPrenom
osheet.getCellByPosition(20,giligne).String=sRecherche
CopieRefLivreToPP(giCol,giligne)
If web Then
LibOUrl = convertToUrl("https://fr.search.yahoo.com/search?fr=yhs-invalid&p=" _
& sRecherche)
else
LibOUrl = convertToUrl("https://www.babelio.com/Livres/")
end if
LibOFlags = com.sun.star.system.SystemShellExecuteFlags.DEFAULTS
LibOService = CreateUnoService("com.sun.star.system.SystemShellExecute")
LibOService.execute(LibOUrl, "", LibOFlags)
End sub 'RechercheWebOrBabelio
'**************
sub CopieRefLivreToPP(ByVal nCol as Long, ByVal nLgn as Long)
'Copie les références du livre dans le press papier pour le Web/Babelio
dim dispatcher as object
dim sAdr as String
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
sAdr=ConvPosToNameCell(20,nLgn)
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = sAdr
dispatcher.executeDispatch(oDocF, ".uno:GoToCell", "", 0, args1())
dispatcher.executeDispatch(oDocF, ".uno:Copy", "", 0, Array())
dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "Flags"
args3(0).Value = "SVDFN"
dispatcher.executeDispatch(oDocF, ".uno:Delete", "", 0, args3())
sAdr=ConvPosToNameCell(nCol,nLgn )
dim args4(0) as new com.sun.star.beans.PropertyValue
args4(0).Name = "ToPoint"
args4(0).Value = sAdr
dispatcher.executeDispatch(oDocF, ".uno:GoToCell", "", 0, args4())
end sub 'CopieRefLivreToPP
'**************
pour fonctionner il faut:
Outils->Macro->exécuter PysListenerAdd
pour arrter sans erreur il faut:
Outils->Macro->exécuter PysListenerRemove.
Quelle est la solution?
Merci pour votre aide.