Results 1 to 19 of 19

Thread: Solved: Remember last saved toolbar position

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #17
    VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,168
    Location
    I tested this one here. It writes to and reads from correctly. I had something wrong I think it was the way I called the sub, it thought I was still writting to the regestry.


    Option Explicit
    Public Const YOUR_TOOLBAR_NAME As String = "A Name For Your ToolBar"
    Public MyButton As CommandBarButton
    
    Sub SaveSettingIni(iToolBar As CommandBar)
        Dim DirLoc As String
        DirLoc = "I:\"
        With iToolBar
        WriteIniValue DirLoc & "MyADDIN.ini", "Settings", "Position", CStr(.Position)
        WriteIniValue DirLoc & "MyADDIN.ini", "Settings", "RowIndex", CStr(.RowIndex)
        WriteIniValue DirLoc & "MyADDIN.ini", "Settings", "Left", CStr(.Left)
        WriteIniValue DirLoc & "MyADDIN.ini", "Settings", "Top", CStr(.Top)
        End With
    End Sub
    
    Sub ReadSettingIni(iToolBar As CommandBar)
        Dim DirLoc As String
        DirLoc = "I:\"
        With iToolBar
        .Position = Val(ReadIniValue(DirLoc & "MyADDIN.ini", "Settings", "Position"))
        .RowIndex = Val(ReadIniValue(DirLoc & "MyADDIN.ini", "Settings", "RowIndex"))
        .Left = Val(ReadIniValue(DirLoc & "MyADDIN.ini", "Settings", "Left"))
        .Top = Val(ReadIniValue(DirLoc & "MyADDIN.ini", "Settings", "Top"))
        End With
    End Sub
    
    Public Sub AddToolBar()
        On Error Resume Next
        Dim I As Integer
        Dim J As Integer
        Dim sToolBar As String
        On Error Resume Next
        sToolBar = Application.CommandBars(YOUR_TOOLBAR_NAME).Name
        If Err.Number <> 0 Then
        Application.CommandBars.Add YOUR_TOOLBAR_NAME, , , True
        ReadSettingIni Application.CommandBars(YOUR_TOOLBAR_NAME) '<- this line
        End If
        Application.CommandBars(YOUR_TOOLBAR_NAME).Visible = True
        I = Application.CommandBars(YOUR_TOOLBAR_NAME).Controls.Count
        For J = I To 1 Step -1
        Application.CommandBars(YOUR_TOOLBAR_NAME).Controls(J).Delete
        Next
        On Error GoTo 0
        If MyButton Is Nothing Then
        Set MyButton = Application.CommandBars(YOUR_TOOLBAR_NAME).Controls.Add(1)
        End If
        With MyButton
        .Caption = "A Name For Your Button"
        .Style = msoButtonIconAndCaption 'icon and caption
        '.Style = msoButtonCaption 'caption only
        '.Style = msoButtonIcon 'icon only
        .Tag = "VBA CustomButtons"
        .OnAction = "Macro.1"
        .FaceId = 362 'standard ms face icons
        .Visible = True
        End With
    End Sub
    
    Public Sub DeleteToolBar()
        Dim I As Integer
        Dim J As Integer
        On Error Resume Next
        SaveSettingIni Application.CommandBars(YOUR_TOOLBAR_NAME) '<- this line
        I = Application.CommandBars(YOUR_TOOLBAR_NAME).Controls.Count
        For J = I To 1 Step -1
        Application.CommandBars(YOUR_TOOLBAR_NAME).Controls(J).Delete
        Next
        Set MyButton = Nothing
        Application.CommandBars(YOUR_TOOLBAR_NAME).Delete
        On Error GoTo 0
    End Sub
    Last edited by Aussiebear; 04-28-2023 at 09:41 PM. Reason: Adjusted the code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •