Rapport de message :*
 

Re: Mémorisation emplacement barre d'outil

Titre du sujet : Re: Mémorisation emplacement barre d'outil
par myDearFriend! le 21/08/2009 01:51:44

Bonsoir Alragorn, le Forum,

Tu trouveras en pièce jointe une proposition de réponse à ton problème. A un ou deux détails près, il devrait pouvoir coller à ta demande...

J'ai modifé le code comme suit :

DANS LE MODULE DE CODE DE L'OBJET THISWORKBOOK
Private Sub Workbook_Open()
    Barre_Outil_Projet True
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Barre_Outil_Projet False
End Sub
Remarque : le mot clé Call que tu avais utilisé est inutile et, sauf erreur, n'a aucun intérêt (sauf si tu veux faire appel à des fonctions issues de bibliothèques externes : type API Windows par exemple).


DANS LE MODULE DE CODE STANDARD : Module2
Après avoir créé (et caché) un onglet supplémentaire nommé Param pour stocker les attributs de la barre d'outils, j'ai modifié ta procédure Barre_Outil_Projet comme suit :
Sub Barre_Outil_Projet(CreerBarre As Boolean)
Dim TBar As CommandBar
Dim F As Worksheet
    Set F = Sheets("Param")
    If CreerBarre Then
    '   Crée la barre d'outils
        On Error Resume Next
        Set TBar = Application.CommandBars("Projet Outil")
        On Error GoTo 0
        If TBar Is Nothing Then
            Set TBar = Application.CommandBars.Add("Projet Outil", Temporary:=True)
        End If
        With TBar
            .Visible = True
            'Position barre
            .Position = F.Cells(1, 1).Value
            If .Position = msoBarFloating Then
                .Top = F.Cells(1, 4).Value
            Else
                .RowIndex = F.Cells(1, 2).Value
            End If
            .Left = F.Cells(1, 3).Value
            'Création boutons
            CreerBouton TBar, msoButtonIconAndCaption, "AjoutTâche", 4088
            CreerBouton TBar, msoButtonIconAndCaption, "AjoutContact", 4088
            CreerBouton TBar, msoButtonIconAndCaption, "EnvoiMail", 1757
        End With
    Else
        'On mémorise la position de la barre d'outil dans un onglet caché
        With Application.CommandBars("Projet Outil")
            F.Cells(1, 1).Value = .Position
            F.Cells(1, 2).Value = .RowIndex
            F.Cells(1, 3).Value = .Left
            F.Cells(1, 4).Value = .Top
            .Delete
        End With
    End If
End Sub

Private Sub CreerBouton(Bar As CommandBar, St As Byte, Capt As String, Fid As Integer)
'Création boutons barre
    With Bar.Controls.Add(Type:=msoControlButton, Temporary:=True)
        .Style = St
        .OnAction = Capt
        .FaceId = Fid
        .Caption = Capt
    End With
End Sub
Le principe est donc simple : au moment de la fermeture, on stocke dans un onglet caché les paramètres de position de la barre d'outils et on rétablit l'ensemble lors de la réouverture du classeur.

En espérant que ça puisse répondre à ton besoin...

Cordialement,