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

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

    CM.AddFromString Code
End Sub

Contact OfficeOne on email at Copyright © 2001-2023 OfficeOne. All rights reserved.