0

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.

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34
  • What part doesn't work? Are you running the code from inside PowerPoint or Excel? – PeterT Dec 08 '22 at 19:33
  • You may find this https://software-solutions-online.com/add-images-folder-powerpoint-presentation-using-vba/ and this https://www.craig-tolley.co.uk/2011/11/23/vba-create-slide-for-every-picture-in-a-folder/ useful – Oran G. Utan Dec 08 '22 at 20:02
  • Put a break at audioFiles = Dir("C:\Users\audio\*.mp3") , let that one line run, then check the value of audioFiles. If it's blank, nothing will happen. Also, why not put your routine for creating an array of image files outside the main loop so it only runs once rather than for every audio file? – Steve Rindsberg Dec 09 '22 at 04:06
  • @PeterT it runs but does not create anything. – TinyObjects Dec 09 '22 at 06:35
  • After the line `audioFiles = Dir("C:\Users\audio\*.mp3")` add the following code: `If audioFiles = vbNullString Then MsgBox "No MP3 files found!"` – PeterT Dec 09 '22 at 14:25
  • As an aside, ChatGPT will only ever give you a starting point for your solution, not a complete set of code. When you run into a problem like this, ask your questions here and learn about debugging and fixing your code. It will take you much farther that ChatGPT ever can. – PeterT Dec 09 '22 at 14:26
  • >> I am using the ChatGPT There's a reason why ChatGPT "answers" are now banned here. And even the people here won't be able to help you if you don't answer their questions/try the suggestions they offer. HAVE you tried either my or PeterT's suggestion? – Steve Rindsberg Dec 09 '22 at 15:01
  • @PeterT thank you for your help, the code doesn't work, maybe what I want to do, but the PowerPoint is not capable of doing it. – TinyObjects Dec 10 '22 at 08:26
  • @SteveRindsberg, I am sorry, I forgot to answer your question, I did put a break it did not return anything. – TinyObjects Dec 10 '22 at 08:27
  • @TinyObjects Ok, that's probably where the problem originates, then. If audioFiles = Dir("C:\Users\audio\*.mp3") returns nothing, then the rest of the code won't run. Have you verified that there really is a directory there and that it contains MP3 files? – Steve Rindsberg Dec 11 '22 at 17:33
  • @SteveRindsberg, I have copied the directory and pasted it, But it still gives me error wrong number of arguments or invalid property assignment on this line: 'Set pptSlide = pptPres.Slides.Add(pptPres.Slides.Count + 1, 11, ppViewSlide)' – TinyObjects Dec 12 '22 at 18:53
  • You've given too many arguments and the wrong value for the second parameter. Use Set pptSlide = pptPres.Slides.Add(pptPres.Slides.Count + 1, pptPres.SlideMaster.CustomLayouts(11)) – Steve Rindsberg Dec 13 '22 at 19:01

0 Answers0