TinyObjects
TinyObjects

Reputation: 1

VB code to create slides based on number of audio files, and insert random background images

I am trying to create a VB Code that will create number of slides based on the audio files in the folder. Then, I want to insert random background images from the folder. I have written this code but it doesn't work.

    Sub CreatePowerPointSlides()

    'Declare variables
    Dim pptApp As Object
    Dim pptPres As Object
    Dim pptSlide As Object
    Dim audioFiles As String
    Dim audioFile As String
    Dim imageFiles As String
    Dim imageFile As String

    'Create a new instance of PowerPoint and open the presentation
    Set pptApp = CreateObject("PowerPoint.Application")
    pptApp.Visible = True
    Set pptPres = pptApp.Presentations.Add

    'Get a list of all audio files in the folder
    audioFiles = Dir("C:\Users\audio\*.mp3")

    'Iterate through each audio file in the folder
    Do While audioFiles <> ""
        audioFile = audioFiles

        'Create a new slide in PowerPoint and add the audio file
        Set pptSlide = pptPres.Slides.Add(pptPres.Slides.Count + 1, 11, ppViewSlide)
        pptSlide.Shapes.AddMediaObject2 FileName:=audioFile, LinkToFile:=msoFalse, 
        SaveWithDocument:=msoTrue

        'Get a list of all image files in the folder
        imageFiles = Dir("C:\Users\user1\Pictures\ImageBackground\Nature\*.jpg")

        'Choose a random image file from the folder
        Randomize
        imageFile = "C:\Users\user1\Pictures\ImageBackground\Nature\" & 
        Split(imageFiles, vbNewLine)(Int(Rnd() * 
        UBound(Split(imageFiles, vbNewLine))))

        'Add the image file as the background of the slide
        pptSlide.FollowMasterBackground = msoFalse
        pptSlide.Background.Fill.UserPicture PictureFile:=imageFile

        'Get the next audio file in the folder
        audioFiles = Dir()
    Loop

    pptApp.ActiveWindow.View.Type = ppViewSlide

    End Sub

It doesn't even create one slide.

Upvotes: 0

Views: 75

Answers (0)

Related Questions