Tuesday, October 22, 2024

VBA PowerPoint to Create Microsoft Presentation from Text File

Objectives:

The purpose of this VBA PowerPoint code is to automate the process of exporting paragraphs from a Text file into a PowerPoint presentation. Each paragraph is transformed into a separate slide, with the option to include a title and a company logo. The text file path and logo image is hard coded; these can be modified as per the need. The code is as follows:

Sub CreatePresentationFromTextFile()
    Dim pptApp As Object
    Dim pptPres As Object
    Dim slideIndex As Integer
    Dim slide As Object
    Dim textFileName As String
    Dim fileNumber As Integer
    Dim lineContent As String
    Dim logoPath As String
    Dim logoShape As Object
    Dim slideWidth As Single
    Dim logoWidth As Single
    Dim logoHeight As Single
    
    ' Set the path to your text file and logo
    textFileName = "C:\Users\ajeet\Documents\test.txt" ' Change to your text file path
    logoPath = "G:\# Pictures\Image Common\logo.png" ' Change to your logo image path
    
    ' Logo size in inches (convert inches to points, 1 inch = 72 points)
    logoWidth = 1.13 * 72
    logoHeight = 1.13 * 72
    
    ' Create a new PowerPoint application and presentation
    Set pptApp = CreateObject("PowerPoint.Application")
    Set pptPres = pptApp.Presentations.Add
    pptApp.Visible = True
    
    ' Get slide width for logo positioning
    slideWidth = pptPres.PageSetup.SlideWidth
    
    ' Open the text file for reading
    fileNumber = FreeFile
    Open textFileName For Input As fileNumber
    
    ' Initialize slide index
    slideIndex = 1
    
    ' Read each line from the text file
    Do While Not EOF(fileNumber)
        Line Input #fileNumber, lineContent
        
        ' Check if the line content is not empty or blank
        If Trim(lineContent) <> "" Then
            ' Add a new slide with Title and Content layout
            Set slide = pptPres.Slides.Add(slideIndex, ppLayoutText)
            
            ' Set the title of the slide (you can customize this as needed)
            slide.Shapes(1).TextFrame.TextRange.Text = "Slide " & slideIndex
            
            ' Set the content of the slide (using Hindi font)
            With slide.Shapes(2).TextFrame.TextRange
                .Text = lineContent
                .Font.Name = "Mangal" ' You can change this to the preferred Hindi font
                .Font.Size = 15 ' Adjust font size as per your need
            End With
            
            ' Add the logo to the slide in the top right corner
            Set logoShape = slide.Shapes.AddPicture(logoPath, _
                            msoFalse, msoCTrue, slideWidth - logoWidth - 10, 10, logoWidth, logoHeight)
            
            ' Increment slide index for the next slide
            slideIndex = slideIndex + 1
        End If
    Loop
    
    ' Close the text file
    Close fileNumber
    
    ' Clean up
    Set slide = Nothing
    Set pptPres = Nothing
    Set pptApp = Nothing
End Sub

No comments:

Post a Comment

Hot Topics