Macro to generate powerpoint slides

  • Thread starter Thread starter Marty Girvan
  • Start date Start date
M

Marty Girvan

I have been reading and researching for days and cannot seem to find what Iam looking for. And I am close.
I have excel file and in the file are 150 different links (each in their own cell) to files on a server. Each file is a PowerPoint document (.pptx). I update my spreadsheet with a lot of different data but I want to run a macro that opens all the PowerPoint files into one PowerPoint presentation. I can generate a macro that creates a PowerPoint slide but not one that opens the files from the excel sheet I work from. Any example codes wouldbe helpful. I am close but yest so far away. :)

Thanks

Marty
 
So I have a idea or game plan I posted below if anyone can look at it and help me with this project.

Excel sheet has 5 cells (A1:A5) with hyperlinks (c:/documents/test.pptx) to PowerPoint slides on a server.

I would like to automate them (all 5 slides) to open in to one PowerPoint Project/Presentation.

Here is the code I am currently working on:

Sub CreatePowerPoint()

'Add a reference to the Microsoft PowerPoint Library by:
'1. Go to Tools in the VBA menu
'2. Click on Reference
'3. Scroll down to Microsoft PowerPoint X.0 Object Library, check the box, and press Okay

'First we declare the variables we will be using
Dim newPowerPoint As PowerPoint.Application
Dim activeSlide As PowerPoint.Slide
Dim c As Range

'Look for existing instance
On Error Resume Next
Set newPowerPoint = GetObject(, "PowerPoint.Application")
On Error GoTo 0

'Let's create a new PowerPoint
If newPowerPoint Is Nothing Then
Set newPowerPoint = New PowerPoint.Application
End If
'Make a presentation in PowerPoint
If newPowerPoint.Presentations.Count = 0 Then
newPowerPoint.Presentations.Add
End If

'Show the PowerPoint
newPowerPoint.Visible = True

'Add a new slide where we will open the file (hyperlink)
newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutText
newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.Count
Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)

'But this is where I am stuck. I need to open the hyperlinks and have them inserted into the slides. Or create the slides. The files are slides.


Next

AppActivate ("Microsoft PowerPoint")
Set activeSlide = Nothing
Set newPowerPoint = Nothing

End Sub
 
I have been reading and researching for days and cannot seem to find
what I am looking for. And I am close. I have excel file and in the
file are 150 different links (each in their own cell) to files on a
server. Each file is a PowerPoint document (.pptx). I update my
spreadsheet with a lot of different data but I want to run a macro
that opens all the PowerPoint files into one PowerPoint presentation.
I can generate a macro that creates a PowerPoint slide but not one
that opens the files from the excel sheet I work from. Any
example codes would be helpful. I am close but yest so far away. :)

Thanks

Marty

I think you're going about this entirely wrong!!
I read your code and I'm curious as to why you're not doing this in a
PowerPoint VBA project! You could read the files list without having to
open the Excel file (using ADODB) into VBA there and process the entire
task in PP. Easier yet, store the list in a txt file and use standard
VBA file I/O functions to read the file into an array, then loop to get
each file.

But.., if you insist on doing this in Excel then...

Add another variable of Variant type, and a counter for the loop:

Dim vList, n&

'Dump the list into an array
vList = ActiveSheet.Range("A1:A5")
'Iterate the array to process each list item
For n = LBound(vList) To UBound(vList)
Debug.Print vList(n, 1) '//process each file here
Next 'n

...where vList is a 2D array consisting of 5 rows and 1 col.

Note that best practice in VBA programming recommends *'NEVER hijack an
existing instance'* of an app for automation. (Exception is Outlook
because it doesn't allow multiple instances!)

Now I've never automated PP but reading its Object ref I suspect you
could revise your Excel code like so...

Sub CreatePowerPoint()
Dim vList, n&

vList = ActiveSheet.Range("A1:A5")
On Error GoTo Cleanup
'Automate a new instance of PowerPoint
With CreateObject("PowerPoint.Application")
'Make a presentation in PowerPoint
.Visible = True

For n = LBound(vList) To UBound(vList)
'Add a new slide from the file
With .Presentations.Add
'Insert the slide into the presentation
.slides.InsertFromFile vfile(n, 1), .slides.Count + 1
End With '.Presentations.Add
Next 'n
End With 'CreateObject
Cleanup:
End Sub

...to simplify the process

But I think you'd be better off doing a PP project and store the slides
list in a text file. That means you'll need to persue this in a PP
group. In this case the following revised Excel code should work...


Sub InsertSlidesFromFile()
' Inserts slides from a list of PPTs stored in a txt file
Dim vList, n&

vList = Split(ReadTextFile("C:\documents\TestPP.txt"), vbCrLf)
On Error GoTo Cleanup
With Application
For n = LBound(vList) To UBound(vList)
'Add a new slide from the file
With .Presentations.Add
'Insert the slide into the presentation
.slides.InsertFromFile vfile(n), .slides.Count + 1
End With '.Presentations.Add
Next 'n
End With 'Application
Cleanup:
End Sub

Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()

Optionally, if your slide files are the only files stored in a specific
folder then you could also drill that folder with VBA's Dir() function
to access each file without the need for the 'ReadTextFile' helper
routine...

Sub InsertSlidesFromFolder()
' Inserts slides from a list of PPTs stored in a txt file
Dim vFile, n&

vFile = Dir("C:\documents\*.*", vbDirectory)
On Error GoTo Cleanup
With Application
Do While Len(vFile)
'Add a new slide from the file
With .Presentations.Add
'Insert the slide into the presentation
.slides.InsertFromFile vFile, .slides.Count + 1
End With '.Presentations.Add
vFile = Dir()
Loop
End With 'Application
Cleanup:
End Sub

HTH

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
I forgot to include the path and so 'InsertSlidesFrom...' routines are
revised as follows...

Sub InsertSlidesFromFile()
' Inserts slides from a list of PPTs stored in a txt file
Dim vList, n&
Const sPath$ = "C:\documents\"

vList = Split(ReadTextFile(sPath & "TestPP.txt"), vbCrLf)
On Error GoTo Cleanup
With Application
For n = LBound(vList) To UBound(vList)
'Add a new slide where we will open the file (hyperlink)
With .Presentations.Add
'Insert the files into the slide
.slides.InsertFromFile sPath & vFile(n), .slides.Count + 1
End With '.Presentations.Add
Next 'n
End With 'Application
Cleanup:
End Sub

Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()

Sub InsertSlidesFromFolder()
' Inserts slides from a list of PPTs stored in a txt file
Dim vFile, n&
Const sPath$ = "C:\documents\"

vFile = Dir(sPath)
On Error GoTo Cleanup
With Application
Do While Len(vFile)
'Add a new slide from the file
With .Presentations.Add
'Insert the slide into the presentation
.slides.InsertFromFile sPath & vFile, .slides.Count + 1
End With '.Presentations.Add
vFile = Dir()
Loop
End With 'Application
Cleanup:
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
Note that if the path is included in the txt file list then use this
version...

Sub InsertSlidesFromFile()
' Inserts slides from a list of PPTs stored in a txt file
Dim vList, n&
Const sPath$ = "C:\documents\"

vList = Split(ReadTextFile(sPath & "TestPP.txt"), vbCrLf)
On Error GoTo Cleanup
With Application
For n = LBound(vList) To UBound(vList)
'Add a new slide where we will open the file (hyperlink)
With .Presentations.Add
'Insert the files into the slide
.slides.InsertFromFile vFile(n), .slides.Count + 1
End With '.Presentations.Add
Next 'n
End With 'Application
Cleanup:
End Sub

Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
This is great info. I did not think of the other side of the spectrum and using Excel as a database tool. DUH. I cant believe I didnt think of this.Since you spent some time helping me with the excel VBA I will play with it now and post some results. But I think you are right and if I did it through PPT then it could be much simpler. Thanks.
 
This is great info. I did not think of the other side of the
spectrum and using Excel as a database tool. DUH. I cant believe I
didnt think of this. Since you spent some time helping me with the
excel VBA I will play with it now and post some results. But I think
you are right and if I did it through PPT then it could be much
simpler. Thanks.

You're welcome!
Note that I gave you *both* Excel and PPT code. The 2
'InsertSlidesFrom...' subs are PPT. The 'CreatePowerPoint' is Excel.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
Thanks again. I started working from the PPT macro first as it makes moresense. However, I am running into a issue. I created a txt file that only contains a list of paths for all the files (150 files. The files are stored in different locations throughout the server. The txt file only contains the file paths. Here is my (your) code:



Sub auto()

' Inserts slides from a list of PPTs stored in a txt file
Dim vList, n&
Dim vFile
Const sPath$ = "C:\Users\Marty\Documents"

'auto.txt is the file with the hyperlinks
vList = Split(ReadTextFile(sPath & "auto.txt"), vbCrLf)
On Error GoTo Cleanup
With Application
For n = LBound(vList) To UBound(vList)
'Add a new slide where we will open the file (hyperlink)
With .Presentations.Add
'Insert the files into the slide
.Slides.InsertFromFile vFile(n), .Slides.Count + 1
End With '.Presentations.Add
Next 'n
End With 'Application
Cleanup:
End Sub

Function ReadTextFile$(Filename$)


'Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise , Err.Number, , Err.Description
End Function 'ReadTextFile()


I am getting a File Not Found when I debug. The argument looks valid but iam not sure why it is not finding the file. The error is on line 53' which is this line of code:

" Close #iNum: If Err Then Err.Raise , Err.Number, , Err.Description"

Any thoughts.

Also, I did play with the excel code a bit and it generates the PowerPoint presentation and the first slide, but it does not open the files. Here is the code I using:

Sub Run()
Dim vList, n&
Dim vFile

vList = ActiveSheet.Range("A1:A5")
On Error GoTo Cleanup
'Automate a new instance of PowerPoint
With CreateObject("PowerPoint.Application")
'Make a presentation in PowerPoint
.Visible = True

For n = LBound(vList) To UBound(vList)
'Add a new slide from the file
With .Presentations.Add
'Insert the slide into the presentation
.slides.InsertFromFile vFile(n, 1), .slides.Count + 1
End With '.Presentations.Add
Next 'n
End With 'CreateObject
Cleanup:


End Sub



Thanks again. I would like to try and get both the excel code working and the PPT code so that I can hand some of these projects over to others for them to run and can automate some of the daily processes. Thanks again, it is much appreciated and fun to learn. :)

Marty
 
Thanks again. I started working from the PPT macro first as it makes
more sense. However, I am running into a issue. I created a txt
file that only contains a list of paths for all the files (150 files.
The files are stored in different locations throughout the server.
The txt file only contains the file paths...

The intent of using the text file is so it works with files stored in
different locations, as would the hyperlinks on your worksheet. You say
your text file "only contains a list of 'paths'", but should also
include the filename.

Also, your sPath constant does not end with a backslash and so this
posted code is looking for a file named...

"Documentsauto.txt"
IN
"C:\User\Marty\"

...when it should be looking for...

"auto.txt"
IN
"C:\User\Marty\Documents\"

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
Also, I did play with the excel code a bit and it generates the
PowerPoint presentation and the first slide, but it does not open the
files

As I mentioned.., I don't work with PPT and so any code relating to it
should be asked in a PPT group. That said, after looking at the code it
appears to create a new Presentation for each file when what I think
you want is 1 presentation consisting of slides inserted from the
source files...

'[XL routine]
Sub CreatePowerPoint()
Dim vList, n&

vList = ActiveSheet.Range("A1:A5")
On Error GoTo Cleanup
'Automate a new instance of PowerPoint
With CreateObject("PowerPoint.Application")
.Visible = True
'Add a new presentation
With .Presentations.Add
For n = LBound(vList) To UBound(vList)
'Insert the files into the slide
.slides.InsertFromFile vFile(n, 1), .slides.Count + 1
Next 'n
End With '.Presentations.Add
End With 'CreateObject
Cleanup:
End Sub

Sub InsertSlidesFromFile()
' Inserts slides from a list of PPTs stored in a txt file
Dim vList, n&
Const sPath$ = "C:\documents\"

vList = Split(ReadTextFile(sPath & "TestPP.txt"), vbCrLf)
On Error GoTo Cleanup
'Add a new presentation
With Application.Presentations.Add
For n = LBound(vList) To UBound(vList)
'Insert the files into the slide
.slides.InsertFromFile sPath & vFile(n), .slides.Count + 1
Next 'n
End With 'Application.Presentations.Add
Cleanup:
End Sub

'[PPT routines]
Sub InsertSlidesFromFolder()
' Inserts slides from a list of PPTs stored in a txt file
Dim vFile, n&
Const sPath$ = "C:\documents\"

vFile = Dir(sPath)
On Error GoTo Cleanup
'Add a new presentation
With Application.Presentations.Add
Do While Len(vFile)
'Insert the slide into the presentation
.slides.InsertFromFile sPath & vFile, .slides.Count + 1
vFile = Dir()
Loop
End With 'Application.Presentations.Add
Cleanup:
End Sub

Sub ReformatBankAmounts()
Dim c As Range
For Each c In Selection
c = Format(c / 1000, "000.000")
Next 'c
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
Oops.., too many subs AND properly located section marker for PPT
subs...
'[XL routine]
Sub CreatePowerPoint()
Dim vList, n&

vList = ActiveSheet.Range("A1:A5")
On Error GoTo Cleanup
'Automate a new instance of PowerPoint
With CreateObject("PowerPoint.Application")
.Visible = True
'Add a new presentation
With .Presentations.Add
For n = LBound(vList) To UBound(vList)
'Insert the files into the slide
.slides.InsertFromFile vFile(n, 1), .slides.Count + 1
Next 'n
End With '.Presentations.Add
End With 'CreateObject
Cleanup:
End Sub

'[PPT routines]
Sub InsertSlidesFromFile()
' Inserts slides from a list of PPTs stored in a txt file
Dim vList, n&
Const sPath$ = "C:\documents\"

vList = Split(ReadTextFile(sPath & "TestPP.txt"), vbCrLf)
On Error GoTo Cleanup
'Add a new presentation
With Application.Presentations.Add
For n = LBound(vList) To UBound(vList)
'Insert the files into the slide
.slides.InsertFromFile sPath & vFile(n), .slides.Count + 1
Next 'n
End With 'Application.Presentations.Add
Cleanup:
End Sub

Sub InsertSlidesFromFolder()
' Inserts slides from a list of PPTs stored in a txt file
Dim vFile, n&
Const sPath$ = "C:\documents\"

vFile = Dir(sPath)
On Error GoTo Cleanup
'Add a new presentation
With Application.Presentations.Add
Do While Len(vFile)
'Insert the slide into the presentation
.slides.InsertFromFile sPath & vFile, .slides.Count + 1
vFile = Dir()
Loop
End With 'Application.Presentations.Add
Cleanup:
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
I see the missing backslash. My apologies. I will update and run. anotherapology as I misstated the info about the txt file and the paths of the files. They are all full path names with the file extensions. Same with the Excel file. Ill work on these and update. Cheers for the quick response.
 
Here's all new code for the entire module...

Option Explicit

Const sPath$ = "C:\Users\Marty\Documents\"

Sub CreatePowerPoint()
Dim vList, n&

vList = ActiveSheet.Range("A1:A5")
On Error GoTo Cleanup
'Automate a new instance of PowerPoint
With CreateObject("PowerPoint.Application")
.Visible = True
'Add a new presentation
With .Presentations.Add
'Insert the slides into the presentation
For n = LBound(vList) To UBound(vList)
.slides.InsertFromFile vFile(n, 1), .slides.Count + 1
Next 'n
End With '.Presentations.Add
End With 'CreateObject
Cleanup:
End Sub

Sub InsertSlidesFromFile()
' Inserts slides from a list of PPTs stored in a txt file
Dim vList, n&

vList = Split(ReadTextFile(sPath & "auto.txt"), vbCrLf)
On Error GoTo Cleanup
'Add a new presentation
With Application.Presentations.Add
'Insert the slides into the presentation
For n = LBound(vList) To UBound(vList)
.slides.InsertFromFile sPath & vFile(n), .slides.Count + 1
Next 'n
End With 'Application.Presentations.Add
Cleanup:
End Sub

Sub InsertSlidesFromFolder()
' Inserts slides from a list of PPTs stored in a txt file
Dim vFile, n&

vFile = Dir(sPath)
On Error GoTo Cleanup
'Add a new presentation
With Application.Presentations.Add
'Insert the slides into the presentation
Do While Len(vFile)
.slides.InsertFromFile sPath & vFile, .slides.Count + 1
vFile = Dir()
Loop
End With 'Application.Presentations.Add
Cleanup:
End Sub

Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
The entire module partially works. If I run Sub CreatePowerPoint it generates a PowerPoint presentation but it does not insert the slides form the hyperlinks in the excel sheet. Same with InsertSlidesFromFile. Any thoughts. We are so close.

I did have to add Dim vFile as I was getting some errors. So here is the new module:

Option Explicit

Const sPath$ = "C:\Users\marty\Documents\"

Sub CreatePowerPoint()
Dim vList, n&
Dim vFile

vList = ActiveSheet.Range("A1:A5")
On Error GoTo Cleanup
'Automate a new instance of PowerPoint
With CreateObject("PowerPoint.Application")
.Visible = True
'Add a new presentation
With .Presentations.Add
'Insert the slides into the presentation
For n = LBound(vList) To UBound(vList)
.slides.InsertFromFile vFile(n, 1), .slides.Count + 1
Next 'n
End With '.Presentations.Add
End With 'CreateObject
Cleanup:
End Sub

Sub InsertSlidesFromFile()
' Inserts slides from a list of PPTs stored in a txt file
Dim vList, n&
Dim vFile

vList = Split(ReadTextFile(sPath & "auto.txt"), vbCrLf)
On Error GoTo Cleanup
'Add a new presentation
With Application.Presentations.Add
'Insert the slides into the presentation
For n = LBound(vList) To UBound(vList)
.slides.InsertFromFile sPath & vFile(n), .slides.Count + 1
Next 'n
End With 'Application.Presentations.Add
Cleanup:
End Sub

Sub InsertSlidesFromFolder()
' Inserts slides from a list of PPTs stored in a txt file
Dim vFile, n&

vFile = Dir(sPath)
On Error GoTo Cleanup
'Add a new presentation
With Application.Presentations.Add
'Insert the slides into the presentation
Do While Len(vFile)
.slides.InsertFromFile sPath & vFile, .slides.Count + 1
vFile = Dir()
Loop
End With 'Application.Presentations.Add
Cleanup:
End Sub

Function ReadTextFile$(Filename$)
' Reads large amounts of data from a text file in one single step.
Dim iNum%
On Error GoTo ErrHandler
iNum = FreeFile(): Open Filename For Input As #iNum
ReadTextFile = Space$(LOF(iNum))
ReadTextFile = Input(LOF(iNum), iNum)

ErrHandler:
Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description
End Function 'ReadTextFile()
 
Sorry.., my bad copy/paste. Should be...

Sub CreatePowerPoint()
Dim vList, n&

vList = ActiveSheet.Range("A1:A5")
On Error GoTo Cleanup
'Automate a new instance of PowerPoint
With CreateObject("PowerPoint.Application")
.Visible = True
'Add a new presentation
With .Presentations.Add
'Insert the slides into the presentation
For n = LBound(vList) To UBound(vList)
.slides.InsertFromFile vList(n, 1), .slides.Count + 1
Next 'n
End With '.Presentations.Add
End With 'CreateObject
Cleanup:
End Sub

Sub InsertSlidesFromFile()
' Inserts slides from a list of PPTs stored in a txt file
Dim vList, n&

vList = Split(ReadTextFile(sPath & "auto.txt"), vbCrLf)
On Error GoTo Cleanup
'Add a new presentation
With Application.Presentations.Add
'Insert the slides into the presentation
For n = LBound(vList) To UBound(vList)
.slides.InsertFromFile sPath & vList(n), .slides.Count + 1
Next 'n
End With 'Application.Presentations.Add
Cleanup:
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
no worries,

I fixed the copy and paste part but I am still not getting the slides to come up when running CreatePowerPoint. And now when I run InserSlidesFromFile nothing happens. Weird.
 
no worries,
I fixed the copy and paste part but I am still not getting the slides
to come up when running CreatePowerPoint. And now when I run
InserSlidesFromFile nothing happens. Weird.

This is where you need to get help in a PPT forum...

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
I am thinking of something like this would work with the

..objPPT.Presentations.Open


Some sample code:

Dim objPPT As Object

Set objPPT = CreateObject("PowerPoint.Application")
objPPT.Visible = True

objPPT.Presentations.Open "\\ServerName\FileName.pptx"
 
Back
Top