I have been using the code below (not my code) with success to have users open a presentation and from the title slide, be able to select a .txt file from any location on their computer and have Powerpoint import the text into the Powerpoint and create the slides adhering to the master slide formatting I have set.
Sub AddSlides(text As String)
Dim Pre As Presentation
Dim Sld As Slide
Set Pre = ActivePresentation
Set Sld = Pre.Slides.Add(Index:=Pre.Slides.Count + 1, Layout:=1)
Sld.Shapes(1).TextFrame.TextRange = text
End Sub
Sub ReadFile(sFileName As String)
Dim iFileNum As Integer
Dim sBuf As String
' edit this:
'sFileName = "test.csv"
' does the file exist? simpleminded test:
If Len(Dir$(sFileName)) = 0 Then
Exit Sub
End If
iFileNum = FreeFile()
Open sFileName For Input As iFileNum
Do While Not EOF(iFileNum)
Line Input #iFileNum, sBuf
AddSlides (sBuf)
Loop
' close the file
Close iFileNum
End Sub
Sub SelectFile()
Dim In_file As Variant
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(Type:=msoFileDialogOpen)
dlgOpen.AllowMultiSelect = False
If dlgOpen.Show = -1 Then
In_file = dlgOpen.SelectedItems.Item(1)
ReadFile (In_file)
End If
End Sub
However, now I would like to work with sections, effectively creating a title and a conclusion slide. Section 1 would include the title slide and button for users to select their .txt file. Section 2 would consist of a single slide that concludes the presentation. My problem is, when the code generates the slides from the .txt file, it places them after the conclusion slide in Section 2 instead of after the title slide in Section 1.
I have researched various codes for working with sections and codes for importing/inserting from external files and have had no success working with them to achieve this.
Although I wanted the number of slides generated between the first and last slides to be variable, I can specify how many slides can be generated if this is more feasible. If this does need to be specified, I would also be comfortable creating the slides first and have them populated with the text from the .txt file if this is a more workable option.
Appreciate any help with this.
Note: Current code limits text import to single lines on each slide. If there is a simple way to append this to include 2 lines per slide - that would be extremely useful.
Ok, I'll start with where to put the new slides. You need to change the addSlides function so that it places all of the slides in the position of your conclusion slide. This ones easy, you just change the index from
Index:=Pre.Slides.Count + 1
to
Index:=Pre.Slides.Count
Making the addSlides function as follows:
Sub AddSlides(text As String)
Dim Pre As Presentation
Dim Sld As Slide
Set Pre = ActivePresentation
Set Sld = Pre.Slides.Add(Index:=Pre.Slides.Count, Layout:=1)
Sld.Shapes(1).TextFrame.TextRange = text
End Sub
On your second issue, getting two lines of text per slide, this is mildly more difficult. You need to read each line, and everytime you get to the second line, add the page, then reset the holding variable. Something like the following should work:
Sub ReadFile(sFileName As String)
Dim iFileNum As Integer
Dim sBuf As String
Dim bFlag As Boolean
Dim sHolder As String
' edit this:
'sFileName = "test.csv"
' does the file exist? simpleminded test:
If Len(Dir$(sFileName)) = 0 Then
Exit Sub
End If
iFileNum = FreeFile()
Open sFileName For Input As iFileNum
bFlag = False
Do While Not EOF(iFileNum)
If bFlag = False Then
Line Input #iFileNum, sBuf
holder = sBuf
bFlag = True
Else
Line Input #iFileNum, sBuf
holder = holder & vbCrLf & sBuf
addSlides (holder)
holder = ""
bFlag = False
End If
Loop
' close the file
Close iFileNum
End Sub