Monday, 5 December 2022

Create New Toolbar VBA

 


Option Explicit


Sub Auto_Open_ToolBar()

    Dim oToolbar As CommandBar

'    Dim oButton As CommandBarButton

    Dim oButton1 As CommandBarButton

    Dim oButton2 As CommandBarButton

    Dim MyToolbar As String


    ' Give the toolbar a name

    MyToolbar = "Macro Control"


    On Error Resume Next

    ' so that it doesn't stop on the next line if the toolbar's already there


    ' Create the toolbar; Word will error if it already exists

    Set oToolbar = CommandBars.Add(Name:=MyToolbar, _

        Position:=msoBarFloating, Temporary:=True)

    

    If Err.Number <> 0 Then

          ' The toolbar's already there, so we have nothing to do

          Exit Sub

    End If


    On Error GoTo ErrorHandler


    ' Now add a button to the new toolbar

'    Set oButton = oToolbar.Controls.Add(Type:=msoControlButton)

'

'    ' And set some of the button's properties

'

'    With oButton

'

'         .DescriptionText = "Add The Source data for Mail Merge"

'          'Tooltip text when mouse if placed over button

'

'         .Caption = "Add The Source data for Mail Merge"

'         'Text if Text in Icon is chosen

'

'         .OnAction = "Pick_The_Source_File"

'          'Runs the Sub Pick_The_Source_File() code when clicked

'

'         .Style = msoButtonIconAndCaptionBelow

'          ' Button displays as icon, not text or both

'

'         .FaceId = 8

'          ' chooses icon #7 from the available Office icons

'

'    End With

    

    Set oButton1 = oToolbar.Controls.Add(Type:=msoControlButton)

    With oButton1


         .DescriptionText = "Run Macro to Export to PDF"

          'Tooltip text when mouse if placed over button


         .Caption = "Run Macro to Export to PDF"

         'Text if Text in Icon is chosen


         .OnAction = "Run_Confirmation"

          'Runs the Sub Run_Confirmation() code when clicked


         .Style = msoButtonIconAndCaptionBelow

          ' Button displays as icon, not text or both


         .FaceId = 7

          ' chooses icon #7 from the available Office icons


    End With

    

    

    Set oButton2 = oToolbar.Controls.Add(Type:=msoControlButton)

    With oButton2


         .DescriptionText = "Run Macro to send Mail"

          'Tooltip text when mouse if placed over button


         .Caption = "Run Macro to Send Mail"

         'Text if Text in Icon is chosen


         .OnAction = "Run_Confirmation_Outlook"

          'Runs the Sub Run_Confirmation_Outlook() code when clicked


         .Style = msoButtonIconAndCaptionBelow

          ' Button displays as icon, not text or both


         .FaceId = 6

          ' chooses icon #6 from the available Office icons


    End With


    ' Repeat the above for as many more buttons as you need to add

    ' Be sure to change the .OnAction property at least for each new button


    ' You can set the toolbar position and visibility here if you like

    ' By default, it'll be visible when created. Position will be ignored in WORD 2007 and later

    oToolbar.Top = 150

    oToolbar.Left = 150

    oToolbar.Visible = True

    

NormalExit:

    Exit Sub   ' so it doesn't go on to run the errorhandler code


ErrorHandler:

     'Just in case there is an error

     Msgbox Err.Number & vbCrLf & Err.Description

     Resume NormalExit:

End Sub


Sub Run_Confirmation()

' This code will run when you click Run_Confirmation () added above

' Add a similar subroutine for each additional button you create on the toolbar


    Dim MsgAns As String

    Dim MsgAns1 As String

    

    MsgAns = Msgbox("Do you want to Run macro?", vbQuestion + vbYesNo)


        If MsgAns = vbYes Then

            Call ExporttoPDF

        End If

        

    MsgAns1 = Msgbox("Do you want to send Mails for recently generated PDF?", vbQuestion + vbYesNo)

    

        If MsgAns1 = vbYes Then

            Call send_oulook_mail

        End If

    

End Sub

Sub Run_Confirmation_Outlook()

' This code will run when you click Run_Confirmation () added above

' Add a similar subroutine for each additional button you create on the toolbar


    Dim MsgAns As String

     

    MsgAns = Msgbox("Do you want to Run macro?", vbQuestion + vbYesNo + vbCritical)


        If MsgAns = vbYes Then

            Call send_oulook_mail

        End If

        

End Sub


Sub Delete_MyToolbar()


' Delete the unwanted toolbar that is attached to this workbook.

    Dim cmdbar As CommandBar

    For Each cmdbar In Application.CommandBars

        If cmdbar.Name = "Macro Control" Then

            cmdbar.Delete

        End If

    Next

    

End Sub








add below code in the new Class module

Option Explicit

Private Sub Document_Close()
    
    Call Delete_MyToolbar
    
End Sub

Private Sub Document_Open()

    Call Auto_Open_ToolBar
   
End Sub

No comments:

Post a Comment

featured Post

Recurring Deposite

  https://tax2win.in/guide/5-year-post-office-recurring-deposit

About Me

My photo
Kalyan, Mumbai, Maharashtra, India

Quick Search Formula