VBA Office

Office is a collection of Microsoft software applications. There are a number of shared components between these applications. Most Office applications have VBA embedded in the application to allow the user to programmatically interact with Office applications. The Office Object Library contains classes that are shared between Office applications which can be used in VBA. A reference is automatically included to the Office Object Library when using VBA in any Office application.

COMAddIn

The COMAddIn class represents a COM Add-In within an Office application's COMAddIns collection.

Public Sub Example()

    Application.COMAddIns.Update

    Dim CA As Office.COMAddIn
    For Each CA In Application.COMAddIns
        Debug.Print CA.Description
    Next CA

End Sub

CommandBars and CommandBarControls

The CommandBar class represents a command bar. The CommandBarControl class represents a control on a command bar. CommandBar and CommandBarControl objects can be used in an Office application to programmatically interact with the ribbon or with right-click menus.

Right-click Menu

Add the code below to a VBA project in Excel and restart the Workbook to activate the right-click menu item.

'Standard Module: Module1

Option Explicit

Public Sub TestSub()
    MsgBox "TestSub"
End Sub
'ThisWorkbook Module

Option Explicit

Private Sub Workbook_Open()

    On Error Resume Next

    Dim CBC As Office.CommandBarControl
    Set CBC = Application.CommandBars("Cell").Controls.Add(Temporary:=True)

    CBC.Caption = "Test Command Bar Control"
    CBC.OnAction = "TestSub"

End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    On Error Resume Next

    Application.CommandBars("Cell").Controls("Test Command Bar Control").Delete

End Sub

Print All CommandBars and CommandBarControls

Use the code below to print all CommandBar and CommandBarControl names to an Excel Worksheet.

Public Sub PrintAllCommandBarsAndControls()

    Dim WS As Worksheet
    Set WS = ThisWorkbook.Worksheets.Add

    Dim r As Long
    r = 1

    Dim c As Long
    c = 1

    Dim CB As Office.CommandBar
    For Each CB In Application.CommandBars

        Dim CBC As Office.CommandBarControl

        WS.Cells(r, c).Value2 = CB.Name

        For Each CBC In CB.Controls

            RecurseControl r, c + 1, WS, CBC

        Next CBC

    Next CB

End Sub

Private Sub RecurseControl(r As Long, c As Long, _
WS As Worksheet, CBC As Office.CommandBarControl)

    On Error GoTo StopRecurring

    WS.Cells(r, c).Value2 = CBC.Caption

    Dim CBC1 As Office.CommandBarControl

    For Each CBC1 In CBC.Controls
        RecurseControl r, c + 1, WS, CBC1
    Next CBC1

StopRecurring:

    If Err.Number <> 0 Then
        r = r + 1
    End If

End Sub

Print Undo List

Use the code below to print the user's actions in the undo list.

Public Sub PrintUndoList()

    On Error GoTo ExitSub

    Dim i As Long
    Do While True
        i = i + 1
        Debug.Print Application.CommandBars("Standard").Controls("&Undo").List(i)
    Loop

ExitSub:

End Sub

FileDialog

The FileDialog class represents a file dialog window. File dialogs can be used to select folders or files and return a path string.

Select Single File

Pass msoFileDialogFilePicker to the FileDialog property to create a file picker dialog. Set the AllowMultiSelect property to false to allow only one file to be selected.

Public Sub Example()

    Dim FD As Office.FileDialog
    Set FD = Application.FileDialog(msoFileDialogFilePicker)

    With FD
        .AllowMultiSelect = False
        .ButtonName = "Select File"
        .Filters.Clear
        .Filters.Add "All Files", "*.*"
        .Filters.Add "Excel Files", "*.xl*"
        .FilterIndex = 1
        .InitialFileName = Environ$("USERPROFILE") & "\Desktop\"
        .Title = "Select a File"
        If .Show Then
            Dim FilePath As String
            FilePath = .SelectedItems(1)
        End If
    End With

    If FilePath <> "" Then
        Debug.Print FilePath
    Else
        Debug.Print "No File Selected"
    End If

End Sub

Select Folder

Pass msoFileDialogFolderPicker to the FileDialog property to create a folder picker dialog.

Public Sub Example()

    Dim FD As Office.FileDialog
    Set FD = Application.FileDialog(msoFileDialogFolderPicker)

    With FD
        .ButtonName = "Select Folder"
        .InitialFileName = Environ$("USERPROFILE") & "\Desktop\"
        .Title = "Select a Folder"
        If .Show Then
            Dim FolderPath As String
            FolderPath = .SelectedItems(1)
        End If
    End With

    If FolderPath <> "" Then
        Debug.Print FolderPath
    Else
        Debug.Print "No Folder Selected"
    End If

End Sub

SmartArt

SmartArt graphics are very useful for visualizing data and the structure of that data. The SmartArt class represents a SmartArt graphic. To create a specific type of SmartArt graphic, create a SmartArtLayout object using the Application.SmartArtLayouts property of the Application object and pass it to the AddSmartArt method of the Shapes collection object of a Slide, Worksheet, or Document. In addition to the SmartArt graphics that are provided, users can create their own SmartArt graphics.

PowerPoint

Public Sub Example()

    Dim LayoutId As String
    LayoutId = "urn:microsoft.com/office/officeart/2005/8/layout/orgChart1"

    Dim Layout As Office.SmartArtLayout
    Set Layout = PowerPoint.Application.SmartArtLayouts(LayoutId)

    Dim S As PowerPoint.Slide
    Set S = PowerPoint.Application.Presentations(1).Slides(1)

    Dim SH As PowerPoint.Shape
    Set SH = S.Shapes.AddSmartArt(Layout)

    Dim SA As Office.SmartArt
    Set SA = SH.SmartArt

End Sub

Excel

Public Sub Example()

    Dim LayoutId As String
    LayoutId = "urn:microsoft.com/office/officeart/2005/8/layout/orgChart1"

    Dim Layout As Office.SmartArtLayout
    Set Layout = Excel.Application.SmartArtLayouts(LayoutId)

    Dim WS As Excel.Worksheet
    Set WS = Excel.Workbooks(1).Worksheets(1)

    Dim S As Excel.Shape
    Set S = WS.Shapes.AddSmartArt(Layout)

    Dim SA As Office.SmartArt
    Set SA = S.SmartArt

End Sub

Word

Public Sub Example()

    Dim LayoutId As String
    LayoutId = "urn:microsoft.com/office/officeart/2005/8/layout/orgChart1"

    Dim Layout As Office.SmartArtLayout
    Set Layout = Word.Application.SmartArtLayouts(LayoutId)
    
    Dim S As Word.Shape
    Set S = ThisDocument.Shapes.AddSmartArt(Layout)

    Dim SA As Office.SmartArt
    Set SA = S.SmartArt

End Sub

SmartArt Layouts

To print all SmartArt layouts to iterate over all items in the SmartArtLayouts collection object. SmartArt layout IDs can be use to create SmartArtLayout objects.

Public Sub Example()

    Dim WS As Worksheet
    Set WS = ActiveSheet

    Dim i As Long
    For i = 1 To Application.SmartArtLayouts.Count
        With Application.SmartArtLayouts(i)
            WS.Cells(i, 1).Value = .Name
            WS.Cells(i, 2).Value = .ID
            WS.Cells(i, 3).Value = .Description
            WS.Cells(i, 4).Value = .Application
            WS.Cells(i, 5).Value = .Category
            WS.Cells(i, 6).Value = .Creator
            WS.Cells(i, 7).Value = .Parent
        End With
    Next i

End Sub
Name Id
Basic Block List urn:microsoft.com/office/officeart/2005/8/layout/default
Alternating Hexagons urn:microsoft.com/office/officeart/2008/layout/AlternatingHexagons
Picture Caption List urn:microsoft.com/office/officeart/2005/8/layout/pList1
Lined List urn:microsoft.com/office/officeart/2008/layout/LinedList
Vertical Bullet List urn:microsoft.com/office/officeart/2005/8/layout/vList2
Vertical Box List urn:microsoft.com/office/officeart/2005/8/layout/list1
Vertical Bracket List urn:diagrams.loki3.com/BracketList
Varying Width List urn:diagrams.loki3.com/VaryingWidthList
Tab List urn:microsoft.com/office/officeart/2011/layout/TabList
Horizontal Bullet List urn:microsoft.com/office/officeart/2005/8/layout/hList1
Square Accent List urn:microsoft.com/office/officeart/2008/layout/SquareAccentList
Picture Accent List urn:microsoft.com/office/officeart/2005/8/layout/hList2
Bending Picture Accent List urn:microsoft.com/office/officeart/2005/8/layout/bList2
Stacked List urn:microsoft.com/office/officeart/2005/8/layout/hList9
Increasing Circle Process urn:microsoft.com/office/officeart/2008/layout/IncreasingCircleProcess
Pie Process urn:microsoft.com/office/officeart/2009/3/layout/PieProcess
Detailed Process urn:microsoft.com/office/officeart/2005/8/layout/hProcess7
Grouped List urn:microsoft.com/office/officeart/2005/8/layout/lProcess2
Horizontal Picture List urn:microsoft.com/office/officeart/2005/8/layout/pList2
Continuous Picture List urn:microsoft.com/office/officeart/2005/8/layout/hList7
Picture Strips urn:microsoft.com/office/officeart/2008/layout/PictureStrips
Vertical Picture List urn:microsoft.com/office/officeart/2005/8/layout/vList4
Alternating Picture Blocks urn:microsoft.com/office/officeart/2008/layout/AlternatingPictureBlocks
Vertical Picture Accent List urn:microsoft.com/office/officeart/2005/8/layout/vList3
Titled Picture Accent List urn:microsoft.com/office/officeart/2008/layout/PictureAccentList
Vertical Block List urn:microsoft.com/office/officeart/2005/8/layout/vList5
Vertical Chevron List urn:microsoft.com/office/officeart/2005/8/layout/chevron2
Vertical Accent List urn:microsoft.com/office/officeart/2008/layout/VerticalAccentList
Vertical Arrow List urn:microsoft.com/office/officeart/2005/8/layout/vList6
Trapezoid List urn:microsoft.com/office/officeart/2005/8/layout/hList6
Descending Block List urn:microsoft.com/office/officeart/2009/3/layout/BlockDescendingList
Table List urn:microsoft.com/office/officeart/2005/8/layout/hList3
Segmented Process urn:microsoft.com/office/officeart/2005/8/layout/process4
Vertical Curved List urn:microsoft.com/office/officeart/2008/layout/VerticalCurvedList
Pyramid List urn:microsoft.com/office/officeart/2005/8/layout/pyramid2
Target List urn:microsoft.com/office/officeart/2005/8/layout/target3
Hierarchy List urn:microsoft.com/office/officeart/2005/8/layout/hierarchy3
Vertical Circle List urn:microsoft.com/office/officeart/2008/layout/VerticalCircleList
Table Hierarchy urn:microsoft.com/office/officeart/2005/8/layout/hierarchy4
Architecture Layout urn:microsoft.com/office/officeart/2005/8/layout/architecture
Basic Process urn:microsoft.com/office/officeart/2005/8/layout/process1
Step Up Process urn:microsoft.com/office/officeart/2009/3/layout/StepUpProcess
Step Down Process urn:microsoft.com/office/officeart/2005/8/layout/StepDownProcess
Accent Process urn:microsoft.com/office/officeart/2005/8/layout/process3
Picture Accent Process urn:microsoft.com/office/officeart/2005/8/layout/hProcess10
Alternating Flow urn:microsoft.com/office/officeart/2005/8/layout/hProcess4
Continuous Block Process urn:microsoft.com/office/officeart/2005/8/layout/hProcess9
Increasing Arrows Process urn:microsoft.com/office/officeart/2009/3/layout/IncreasingArrowsProcess
Interconnected Block Process urn:microsoft.com/office/officeart/2011/layout/InterconnectedBlockProcess
Continuous Arrow Process urn:microsoft.com/office/officeart/2005/8/layout/hProcess3
Converging Text urn:microsoft.com/office/officeart/2011/layout/ConvergingText
Process Arrows urn:microsoft.com/office/officeart/2005/8/layout/hProcess6
Circle Accent Timeline urn:microsoft.com/office/officeart/2008/layout/CircleAccentTimeline
Basic Timeline urn:microsoft.com/office/officeart/2005/8/layout/hProcess11
Circle Process urn:microsoft.com/office/officeart/2011/layout/CircleProcess
Basic Chevron Process urn:microsoft.com/office/officeart/2005/8/layout/chevron1
Chevron Accent Process urn:microsoft.com/office/officeart/2005/8/layout/chevronAccent+Icon
Closed Chevron Process urn:microsoft.com/office/officeart/2005/8/layout/hChevron3
Chevron List urn:microsoft.com/office/officeart/2005/8/layout/lProcess3
Sub-Step Process urn:microsoft.com/office/officeart/2009/3/layout/SubStepProcess
Phased Process urn:microsoft.com/office/officeart/2009/3/layout/PhasedProcess
Random to Result Process urn:microsoft.com/office/officeart/2009/3/layout/RandomtoResultProcess
Vertical Process urn:microsoft.com/office/officeart/2005/8/layout/process2
Staggered Process urn:microsoft.com/office/officeart/2005/8/layout/vProcess5
Process List urn:microsoft.com/office/officeart/2005/8/layout/lProcess1
Circle Arrow Process urn:microsoft.com/office/officeart/2009/layout/CircleArrowProcess
Basic Bending Process urn:microsoft.com/office/officeart/2005/8/layout/process5
Repeating Bending Process urn:microsoft.com/office/officeart/2005/8/layout/bProcess3
Vertical Bending Process urn:microsoft.com/office/officeart/2005/8/layout/bProcess4
Ascending Picture Accent Process urn:microsoft.com/office/officeart/2008/layout/AscendingPictureAccentProcess
Upward Arrow urn:microsoft.com/office/officeart/2005/8/layout/arrow2
Descending Process urn:microsoft.com/office/officeart/2009/3/layout/DescendingProcess
Circular Bending Process urn:microsoft.com/office/officeart/2005/8/layout/bProcess2
Equation urn:microsoft.com/office/officeart/2005/8/layout/equation1
Vertical Equation urn:microsoft.com/office/officeart/2005/8/layout/equation2
Funnel urn:microsoft.com/office/officeart/2005/8/layout/funnel1
Gear urn:microsoft.com/office/officeart/2005/8/layout/gear1
Arrow Ribbon urn:microsoft.com/office/officeart/2005/8/layout/arrow6
Opposing Arrows urn:microsoft.com/office/officeart/2005/8/layout/arrow4
Converging Arrows urn:microsoft.com/office/officeart/2005/8/layout/arrow5
Diverging Arrows urn:microsoft.com/office/officeart/2005/8/layout/arrow1
Basic Cycle urn:microsoft.com/office/officeart/2005/8/layout/cycle2
Text Cycle urn:microsoft.com/office/officeart/2005/8/layout/cycle1
Block Cycle urn:microsoft.com/office/officeart/2005/8/layout/cycle5
Nondirectional Cycle urn:microsoft.com/office/officeart/2005/8/layout/cycle6
Continuous Cycle urn:microsoft.com/office/officeart/2005/8/layout/cycle3
Multidirectional Cycle urn:microsoft.com/office/officeart/2005/8/layout/cycle7
Segmented Cycle urn:microsoft.com/office/officeart/2005/8/layout/cycle8
Basic Pie urn:microsoft.com/office/officeart/2005/8/layout/chart3
Hexagon Radial urn:microsoft.com/office/officeart/2011/layout/HexagonRadial
Radial Cycle urn:microsoft.com/office/officeart/2005/8/layout/radial6
Basic Radial urn:microsoft.com/office/officeart/2005/8/layout/radial1
Diverging Radial urn:microsoft.com/office/officeart/2005/8/layout/radial5
Radial Venn urn:microsoft.com/office/officeart/2005/8/layout/radial3
Cycle Matrix urn:microsoft.com/office/officeart/2005/8/layout/cycle4
Radial Cluster urn:microsoft.com/office/officeart/2008/layout/RadialCluster
Organization Chart urn:microsoft.com/office/officeart/2005/8/layout/orgChart1
Picture Organization Chart urn:microsoft.com/office/officeart/2005/8/layout/pictureOrgChart+Icon
Name and Title Organization Chart urn:microsoft.com/office/officeart/2008/layout/NameandTitleOrganizationalChart
Half Circle Organization Chart urn:microsoft.com/office/officeart/2008/layout/HalfCircleOrganizationChart
Circle Picture Hierarchy urn:microsoft.com/office/officeart/2009/layout/CirclePictureHierarchy
Hierarchy urn:microsoft.com/office/officeart/2005/8/layout/hierarchy1
Labeled Hierarchy urn:microsoft.com/office/officeart/2005/8/layout/hierarchy6
Horizontal Organization Chart urn:microsoft.com/office/officeart/2009/3/layout/HorizontalOrganizationChart
Horizontal Multi-Level Hierarchy urn:microsoft.com/office/officeart/2008/layout/HorizontalMultiLevelHierarchy
Horizontal Hierarchy urn:microsoft.com/office/officeart/2005/8/layout/hierarchy2
Horizontal Labeled Hierarchy urn:microsoft.com/office/officeart/2005/8/layout/hierarchy5
Balance urn:microsoft.com/office/officeart/2005/8/layout/balance1
Circle Relationship urn:microsoft.com/office/officeart/2009/3/layout/CircleRelationship
Hexagon Cluster urn:microsoft.com/office/officeart/2008/layout/HexagonCluster
Opposing Ideas urn:microsoft.com/office/officeart/2009/3/layout/OpposingIdeas
Plus and Minus urn:microsoft.com/office/officeart/2009/3/layout/PlusandMinus
Reverse List urn:microsoft.com/office/officeart/2009/layout/ReverseList
Counterbalance Arrows urn:microsoft.com/office/officeart/2005/8/layout/arrow3
Segmented Pyramid urn:microsoft.com/office/officeart/2005/8/layout/pyramid4
Nested Target urn:microsoft.com/office/officeart/2005/8/layout/target2
Converging Radial urn:microsoft.com/office/officeart/2005/8/layout/radial4
Radial List urn:microsoft.com/office/officeart/2005/8/layout/radial2
Tabbed Arc urn:diagrams.loki3.com/TabbedArc+Icon
Basic Target urn:microsoft.com/office/officeart/2005/8/layout/target1
Basic Venn urn:microsoft.com/office/officeart/2005/8/layout/venn1
Linear Venn urn:microsoft.com/office/officeart/2005/8/layout/venn3
Stacked Venn urn:microsoft.com/office/officeart/2005/8/layout/venn2
Interconnected Rings urn:microsoft.com/office/officeart/2005/8/layout/rings+Icon
Basic Matrix urn:microsoft.com/office/officeart/2005/8/layout/matrix3
Titled Matrix urn:microsoft.com/office/officeart/2005/8/layout/matrix1
Grid Matrix urn:microsoft.com/office/officeart/2005/8/layout/matrix2
Basic Pyramid urn:microsoft.com/office/officeart/2005/8/layout/pyramid1
Inverted Pyramid urn:microsoft.com/office/officeart/2005/8/layout/pyramid3
Accented Picture urn:microsoft.com/office/officeart/2008/layout/AccentedPicture
Circular Picture Callout urn:microsoft.com/office/officeart/2008/layout/CircularPictureCallout
Radial Picture List urn:microsoft.com/office/officeart/2011/layout/RadialPictureList
Snapshot Picture List urn:microsoft.com/office/officeart/2009/3/layout/SnapshotPictureList
Spiral Picture urn:microsoft.com/office/officeart/2009/3/layout/SpiralPicture
Captioned Pictures urn:microsoft.com/office/officeart/2008/layout/CaptionedPictures
Bending Picture Caption urn:microsoft.com/office/officeart/2008/layout/BendingPictureCaption
Picture Frame urn:microsoft.com/office/officeart/2011/layout/Picture Frame
Bending Picture Semi-Transparent Text urn:microsoft.com/office/officeart/2008/layout/BendingPictureSemiTransparentText
Bending Picture Blocks urn:microsoft.com/office/officeart/2008/layout/BendingPictureBlocks
Bending Picture Caption List urn:microsoft.com/office/officeart/2008/layout/BendingPictureCaptionList
Titled Picture Blocks urn:microsoft.com/office/officeart/2008/layout/TitledPictureBlocks
Picture Grid urn:microsoft.com/office/officeart/2008/layout/PictureGrid
Picture Accent Blocks urn:microsoft.com/office/officeart/2008/layout/PictureAccentBlocks
Theme Picture Accent urn:microsoft.com/office/officeart/2011/layout/ThemePictureAccent
Theme Picture Grid urn:microsoft.com/office/officeart/2011/layout/ThemePictureGrid
Theme Picture Alternating Accent urn:microsoft.com/office/officeart/2011/layout/ThemePictureAlternatingAccent
Alternating Picture Circles urn:microsoft.com/office/officeart/2008/layout/AlternatingPictureCircles
Title Picture Lineup urn:microsoft.com/office/officeart/2008/layout/TitlePictureLineup
Picture Lineup urn:microsoft.com/office/officeart/2008/layout/PictureLineup
Framed Text Picture urn:microsoft.com/office/officeart/2009/3/layout/FramedTextPicture
Bubble Picture List urn:microsoft.com/office/officeart/2008/layout/BubblePictureList