Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Write me VBA codes for the Power Point program to present a topic on the effects of the…

  1. User

    Write me VBA codes for the Power Point program to present a topic on the effects of the Corona virus on the global economy, including 15 slides and graphical curves for the number of deaths, with some pictures.

  2. Assistant

    Sure, here's an example VBA code that generates a PowerPoint presentation with 15 slides, including graphical curves for the number of deaths and some pictures.

    Sub CreatePresentation()
        Dim pptApp As Object
        Dim pptPres As Object
        Dim pptSlide As Object
        Dim pptShape As Object
        Dim i As Integer
        
        ' Start PowerPoint application
        Set pptApp = CreateObject("PowerPoint.Application")
        pptApp.Visible = True
        
        ' Create a new presentation
        Set pptPres = pptApp.Presentations.Add
        
        ' Create slides
        For i = 1 To 15
            ' Add a new slide
            Set pptSlide = pptPres.Slides.Add(i, 11) '11 = ppLayoutTitleOnly
            
            ' Set slide title
            pptSlide.Shapes.Title.TextFrame.TextRange.Text = "Slide " & i
            
            ' Add graphical curve for number of deaths
            Set pptShape = pptSlide.Shapes.AddChart2(276, 12) '276 = xlLineMarkers
            
            ' Set chart data source
            With pptShape.Chart.ChartData
                .Activate
                .Workbook.Sheets(1).Cells.Clear
                .Workbook.Sheets(1).Range("A1").Value = "Date"
                .Workbook.Sheets(1).Range("B1").Value = "Number of Deaths"
                
                ' Add sample data (replace with your own data)
                .Workbook.Sheets(1).Range("A2").Value = "Jan"
                .Workbook.Sheets(1).Range("B2").Value = 100
                .Workbook.Sheets(1).Range("A3").Value = "Feb"
                .Workbook.Sheets(1).Range("B3").Value = 200
                ' ...
            End With
            
            ' Set chart title
            pptShape.Chart.HasTitle = True
            pptShape.Chart.ChartTitle.Text = "Number of Deaths"
            
            ' Add a picture (replace with your own picture)
            Set pptShape = pptSlide.Shapes.AddPicture("C:\Path\To\Your\Picture.jpg", False, True, 100, 100, 400, 200)
        Next i
        
        ' Save and close the presentation
        pptPres.SaveAs "C:\Path\To\Your\Presentation.pptx"
        pptPres.Close
        
        ' Quit PowerPoint application
        pptApp.Quit
        
        ' Cleanup
        Set pptShape = Nothing
        Set pptSlide = Nothing
        Set pptPres = Nothing
        Set pptApp = Nothing
    End Sub
    

    Make sure to replace the sample data and picture path with your own information. Also, adjust the chart type, position, and size according to your preferences.

  3. User

    THER IS ERRER IN THIS VBA

  4. Assistant

    Apologies for the error in the code. It seems that the chart type and slide layout constants were not recognized. Here's the modified code:

    Sub CreatePresentation()
        Dim pptApp As Object
        Dim pptPres As Object
        Dim pptSlide As Object
        Dim pptShape As Object
        Dim i As Integer
        
        ' Start PowerPoint application
        Set pptApp = CreateObject("PowerPoint.Application")
        pptApp.Visible = True
        
        ' Create a new presentation
        Set pptPres = pptApp.Presentations.Add
        
        ' Create slides
        For i = 1 To 15
            ' Add a new slide
            Set pptSlide = pptPres.Slides.Add(i, 1) ' 1 = ppLayoutTitleOnly
            
            ' Set slide title
            pptSlide.Shapes.Title.TextFrame.TextRange.Text = "Slide " & i
            
            ' Add graphical curve for number of deaths
            Set pptShape = pptSlide.Shapes.AddChart2(91, 1) ' 91 = xlLineMarkers
            
            ' Set chart data source
            With pptShape.Chart.ChartData
                .Workbook.Sheets(1).Cells.Clear
                .Workbook.Sheets(1).Range("A1").Value = "Date"
                .Workbook.Sheets(1).Range("B1").Value = "Number of Deaths"
                
                ' Add sample data (replace with your own data)
                .Workbook.Sheets(1).Range("A2").Value = "Jan"
                .Workbook.Sheets(1).Range("B2").Value = 100
                .Workbook.Sheets(1).Range("A3").Value = "Feb"
                .Workbook.Sheets(1).Range("B3").Value = 200
                ' ...
            End With
            
            ' Set chart title
            pptShape.Chart.HasTitle = True
            pptShape.Chart.ChartTitle.Text = "Number of Deaths"
            
            ' Add a picture (replace with your own picture)
            Set pptShape = pptSlide.Shapes.AddPicture("C:\Path\To\Your\Picture.jpg", False, True, 100, 100, 400, 200)
        Next i
        
        ' Save and close the presentation
        pptPres.SaveAs "C:\Path\To\Your\Presentation.pptx"
        pptPres.Close
        
        ' Quit PowerPoint application
        pptApp.Quit
        
        ' Cleanup
        Set pptShape = Nothing
        Set pptSlide = Nothing
        Set pptPres = Nothing
        Set pptApp = Nothing
    End Sub
    

    Please replace the sample data and picture path as required. Remember to adjust the chart type, picture position, and size to fit your preferences.