VBA script to loop through directory of CSVs and create separate PowerPoint presentations for each file

By | February 26, 2015

As per the very long title, this is a Visual Basic for Application that will loop through directory of CSVs and create separate PowerPoint presentations for each file script which I am saving this for future reference.  VBA script to loop through directory of CSVs and create separate PowerPoint presentations for each file based on the contents of the CSV. Because many of the questions in my CSV have a comma I have used ; as a delimiter, I set this when exporting from my original data source (MySQL) , but you can also change the delimiter in OpenOffice without messing with the operating system.

Sub LoopThroughFiles()
 Dim MyObj As Object, MySource As Object, file As Variant
 Dim csvnamestring() As String

 sPath = "Macintosh HD:Users:David:Desktop:projects:quizes:RS:"
 file = Dir("Macintosh HD:Users:David:Desktop:projects:quizes:RS:")
   While (file <> "")
      If InStr(file, "csv") > 0 Then
         MsgBox "found " & file
            Dim ppPres As Presentation
            Dim filenamestring As String

            filenamestring = file + "PowerPoint" '<-change to your file path/name

            'Create an instance of PPT to work with
            Set ppApp = CreateObject("Powerpoint.Application")
            ppApp.Visible = True

            'Create a new presentation (or you can access an existing file with ppApp.Presentations.Open
            Set ppPres = ppApp.Presentations.Add

            'Save:
            ppPres.SaveAs filenamestring, 1

                Dim csvpath
                csvpath = sPath & file
                MsgBox csvpath


                Const strFileName As String = "Macintosh HD:Users:David:Desktop:projects:quizes:RS:"
                Open csvpath For Input As #1

                row_number = 0

                Do Until EOF(1)

                    Line Input #1, LineFromFile
                    LineItems = Split(LineFromFile, ";")
                    row_number = row_number + 1

                    Set Pre = ActivePresentation
                    Set Sld = Pre.Slides.Add(Index:=Pre.Slides.Count + 1, Layout:=ppLayoutText)
                    Sld.Shapes(1).TextFrame.TextRange = "Question"
                    Sld.Shapes(2).TextFrame.TextRange = LineItems(2) & vbNewLine & _
                    LineItems(4) & vbNewLine & _
                    LineItems(5) & vbNewLine & _
                    LineItems(6) & vbNewLine

                    Set Sld = Pre.Slides.Add(Index:=Pre.Slides.Count + 1, Layout:=ppLayoutText)
                    Sld.Shapes(1).TextFrame.TextRange = "Answer"
                    Sld.Shapes(2).TextFrame.TextRange = LineItems(3)

                Loop
                Close #1

 

 

One thought on “VBA script to loop through directory of CSVs and create separate PowerPoint presentations for each file

  1. cepat hamil

    hi david,

    iam looking script like this but iam a newbie for something like this, i want to create PPT for every rows from my CSV. but i also want it to save it automatically, is that possible david?

    thanks
    hamil

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *