Home Products Customers Tips VBA OfficeOne on Mac Contact us

Programmatically Insert Code in a Presentation

MS Office defines the modules that can contain code as VBComponents. The VBComponent types include Standard Modules, Slides, Forms, etc. The following code accepts a Presentation and the code as input parameters. It will create Standard Module type VBComponent if required and add the code in it.

Microsoft Visual Basic for Applications Extensibility needs to be included in the set of references. To add a reference to it

  1. Select Tools | References....
  2. Select Microsoft Visual Basic for Applications Extensibility from the available references.
  3. Click OK.

Sub InsertCode(ByVal Pres As Presentation, ByVal Code As String)
    Dim CM As CodeModule
    Dim I As Long

    For I = 1 To Pres.VBProject.VBComponents.Count
        With Pres.VBProject.VBComponents(I)
            If .Type = vbext_ct_StdModule Then
                Set CM = .CodeModule
            End If
        End With
    Next

    If CM Is Nothing Then
        Set CM = Pres.VBProject.VBComponents.Add( _
            vbext_ct_StdModule).CodeModule
    End If

    CM.AddFromString Code
End Sub


Home | Products | Tips | VBA | FAQ | Utilities | Disclaimer | Privacy Policy | Mac | Links | Contact us

Contact OfficeOne on email at officeone@officeoneonline.com. Copyright © 2001-2017 OfficeOne. All rights reserved.

page counter