VBA Express
Code to switch between to open presentations [Archive] - VBA Express Forum

PDA

View Full Version : Code to switch between to open presentations



LLittle
06-27-2007, 01:37 PM
I need to copy a set of slides from one ppt file to another thru vba.

I can't figure out how to activate the other ppt file that is not activated so I can then copy the next slide.

Oh I am newbie to vba needless to say.

It would go something like this in pseudocode

Dim pptapp As PowerPoint.Application
Dim pptpres As PowerPoint.Presentation
Dim pptslide As PowerPoint.Slide
Dim pptpres2 As PowerPoint.Presentation
Dim pptslide2 As PowerPoint.Slide

'open presentation 1 slides end up here.
'open presentation 2
'Loop through slides on presentation 2
'select slide
'switch to presentation 1 and paste slide
'switch back to presentation 2
'Loop
'ActivePresentation.Slides.Add(Index:=13, Layout:=ppLayoutText).Select
' ActiveWindow.Selection.Copy
' ActiveWindow.View.Paste
' ActiveWindow.Selection.Copy
' ActiveWindow.View.Paste



I will figure it out if I could just find the way to activate an open presentation other than the on that is activated.

So e.g. if pptpres is activated I could copy the active slide and then activate pptpres2 and paste it until all done.

I thought the code would be pptpres.Activate(name of presentation) but no luck.

Also any could books on ppt vba out there. I can't find a one.

Thanks much Leon.:banghead:

TrippyTom
06-28-2007, 08:40 AM
Part of the problem is PowerPoint itself. The VBA Object model for PowerPoint is TERRIBLE! It's the worst out of the whole suite of applications and Microsoft should be whipped repeatedly with a wet noodle for making it so user UNfriendly!

Anyway, I had a similar need in the past, and what I ended up doing was creating the slide layout through code. I tried your way first, but it was proving to be just too unreliable.

So here's a link to the topic I posted my sample code in:

http://www.vbaexpress.com/forum/showthread.php?t=12800

LLittle
06-28-2007, 09:31 AM
Thanks very much for your response TrippyTom.

I was trying to keep the question simple but realize now I need to go into more detail.

I am limited to office 2003 and xp.

Also, those 3 other presentations with 10 slides are made of images of charts pasted from excel into one ppt presentatoin. I manipulated ppt from excel.

I am not the best programmer and this method is the only way I could figure out how to do it and I am about 5 days into creating that part.

So I very much just want to put the slides from one file into the other file and order them. I didn't mention that part yet but am thinking I can figure out how to order them by the slide name.

I appreciate your method but am afraid I would then have to recreate getting the graphs from excel into ppt. ...5 days worth of time. I am not very good at navigating the slides to land the right graph on the right slide also.

So much of the ppt code I see always uses the activepresentation method of id'ing. At this point I really just want to know how to activate a presentation and then activate the other open presentation...back and forth. Still very grateful for your time. Thank, Leon

Norie
07-01-2007, 07:41 AM
Leon

I've not coded much with Powerpoint VBA but when I looked at the code in Tom's link I see that he doesn't actually appear to be activating anything.

In Excel, which I normally code in, it's generally not needed to activate/select anything eg worksheets to work with them.

Perhaps that's the same in PowerPoint?

I've just done a small test.

Sub test()
Dim prSrc As Presentation
Dim prDst As Presentation

Set prSrc = Presentations(1) ' set a reference to the first presentation
Set prDst = Presentations(2) ' set a reference to the second presentation

prSrc.Slides(1).Shapes(1).Copy ' copy the first shape from the first slide in the first presentation
prDst.Slides(1).Shapes.Paste ' paste into the first slide in the second presentation

End Sub
This copies a shape from one presentation to another without activating anything.

Obviously you'll need something more complicated.

LLittle
07-02-2007, 02:15 PM
Thanks Norie,
that was the piece I needed. I ended up with

Sub CopySlides()

Dim pptapp As PowerPoint.Application
Dim pptpres As PowerPoint.Presentation
Dim pptslide As PowerPoint.Slide
Dim pptpres2 As PowerPoint.Presentation
Dim pptslide2 As PowerPoint.Slide

Set pptapp = New PowerPoint.Application
pptapp.Visible = Office.msoTrue
Set pptpres = pptapp.Presentations.Open("C:\Documents and Settings\llittle\My Documents\AMarketOps\DashboardMarketing\PPTShell_mdf.ppt")
Set pptpres2 = pptapp.Presentations.Open("C:\Documents and Settings\llittle\My Documents\AMarketOps\DashboardMarketing\PPTShell_paste.ppt")
Dim x As Integer
x = 1
For Each pptslide In pptpres.Slides
pptpres.Slides(x).Copy
pptpres2.Slides.Paste
x = x + 1
Next
:hi:

Norie
07-04-2007, 11:40 AM
Glad that worked.:)

It really was a kinda stab in the dark since I've never really coded with PowerPoint.