Save all Shapes of slide into single JPG image - vba

I have a small tool to export the pictures inside ppt documents to image files, so I can import them somewhere else.
For this, I have been using the following piece of code:
For Each slideShape In slide
If slideShape.Type = msoPicture Then
Call slideShape.Export(materialPresentation.Path & "\" & ecode & "_" & cont & ".jpg", ppSaveAsJPG)
cont = cont + 1
End If
Next slideShape
However, in a different tool, I am required to export all the shapes of a slide at once, exactly like selecting them with the mouse, and clicking in "Save as image", which creates an image with all the shapes.
Is there a way to do it using VBA? All I found online were examples of exporting single shapes (Which I already know).

Group the shapes you want to export then export the resulting group shape.

What may also work is exporting via ShapeRange (slide is a Slide object), e.g.
slide.Shapes.Range().Export("C:\output.jpg", ppShapeFormatJPG)

Related

VBA code for giving slides a constant name

I am trying to reduce the effort needed to keep a certain slide (lets call it SlideXYZ) up to date. SlideXYZ is an important content slide that can be found in multiple slide decks. I initially created slide objects that updated automatically when a change was made in the "source slide". However, slide objects unfortunately don't contain animations (they are simply a snapshot of the actual slide). I am now trying to write a VBA script that will search and replace SlideXYZ in each deck with a newer version of SlideXYZ. However, the slide number is dynamic (it changes when a new slide is added above). I need a static, constant reference to SlideXYZ.
I thought of copying SlideXYZ into all presentations once and then using the Slide.Name property to find all instances of it once an update is needed.
However, it appears that the Slide.Name is reassigned by powerpoint each time the slide is pasted into a new presentation. I need a reference that will not change so that I can find and replace SlideXYZ.
#Asger's suggestion would work but a more consistent approach (IMO) would be to use tags. Any presentation, slide, or shape on a slide can have one or more bits of text attached in the form of a tag.
For example:
ActivePresentation.Slides(1).Tags.Add "SlideIdentifier", "Bob"
will create a tag named SlideIdentifier with a value of Bob on slide #1 in the current presentation. These tags will travel with the slide, wherever it goes.
This page on the PowerPoint FAQ that I maintain has more info on using tags:
http://www.pptfaq.com/FAQ00815_Working_with_Tags_-and_a_bit_about_Functions-.htm
As you already found out: Neither SlideIndex, SlideNumber, SlideID nor Name can be used to identify a copied slide.
You may work with the "alternative text" of a characteristic shape to identify a slide:
Just do a right mouseclick on the shape and edit its alternative text.
Also slide notes may help to identify a slide.
Following prints some slide information to your debug window:
Private Sub IdentifyMySlide()
Dim myslide As PowerPoint.Slide
For Each myslide In ActivePresentation.Slides
Debug.Print "Index: " & myslide.SlideIndex,
Debug.Print "Number: " & myslide.SlideNumber,
Debug.Print "ID: " & myslide.SlideID,
Debug.Print "Name: " & myslide.Name,
If myslide.Shapes.Count > 0 Then
Debug.Print "Alternative ShapeText: " & myslide.Shapes(1).AlternativeText,
End If
If myslide.HasNotesPage Then
If myslide.NotesPage(1).Shapes.Count > 0 Then
If myslide.NotesPage(1).Shapes(1).HasTextFrame Then
Debug.Print "Notes: " & Left(myslide.NotesPage(1).Shapes(1).TextFrame.TextRange.Text, 10)
Else
Debug.Print
End If
Else
Debug.Print
End If
Else
Debug.Print
End If
Next myslide
End Sub

VBA Picture Shadow Macro

I have a bunch of images that I would like to apply a specific picture style to - the 4th one that is shown in Word 2010:
I have a macro that will loop through all images, but need to know the possibilities for the shadow.type .
What would be really helpful is a reference to the commands that would be used for each type of picture style, with a visual example.
I don't have VBA, so can't examine the elements of the picture. I have tried various msoShadowxx, but that didn't work.
Is there a good reference with visual examples, or a reference with the settings for each picture style? Or the settings to use for the 4th picture style (shown in the screenshot here)?
Here is the macro code that I use to loop through all the pictures.
Sub BorderMacroshadow()
Dim oInlineShp As InlineShape
For Each oInlineShp In ActiveDocument.InlineShapes
With oInlineShp
.Line.Weight = 1
.Line.ForeColor.RGB = vbBlack
.Shadow.Type = msoShadow14
End With
Next
End Sub
Added
A closer look at the reference for msoShadow shows that it is referring to Picture Effects, Shadows 'dialog', not the 'Picture Styles', which I assumes uses some elements of msoShadow in addition to other elements.
So, I am looking for the elements that are needed to duplicate the 4th 'Picture Style' (see the screenshot). Haven't found those yet.
The msoShadowType enumeration is a group of pre-sets. These aren't necessarily used in the gallery on the Ribbon.
In order to ascertain the settings of any Shadows formatting use the various properties available for Shape.Shadow, such as Transparency, Size, Blur. Inthe UI, these can be seen in Picture Effects, Shadow, Shadow Options of the Picture Style group on the Ribbon.
To determine/set them programmatically, see the following code sample. Note that Angle is not one property, but a combintation of OffsetX and OffsetY.
Sub ShadowProperties()
Dim shp As Word.Shape
Dim shw As Word.ShadowFormat
Set shp = Selection.ShapeRange(1)
Set shw = shp.Shadow
With shw
Debug.Print "Blur: " & .Blur, _
"size: " & .Size, _
"Transparency: " & .Transparency, _
"Offset x: " & .OffsetX, _
"Offset y: " & .OffsetY
End With
End Sub

Save PPT as PNG without "Export" VBA

Is there a way to save a single slide as a PNG image with VBA that does not use Slides(#).Export? I am trying to save the image directly to SharePoint but the export causes an error if I make the save location anywhere besides my machine. I know that saving directly to SharePoint as an image is possible since I can click through the save as menu, but I cant replicate with VBA.
Application.FileDialog(msoFileDialogSaveAs _
).InitialFileName = "https://home.thesite.com/sample"
intChoice = Application.FileDialog(msoFileDialogSaveAs).Show
If intChoice <> 0 Then
sImagePath = Application.FileDialog(msoFileDialogSaveAs _
).SelectedItems(1)
Else
MsgBox "Action Cancelled"
Exit Sub
End If
CurSlide = ActiveWindow.Selection.SlideRange.SlideIndex
ActivePresentation.Slides(CurSlide).Export sImagePath, "PNG"
Turns out Export will work if the link is modified so instead of
"https://home.thesite.com/sample"
its
"\\home.thesite.com#SSL\DavWWWRoot\sample"

maximum number/size of videos to load via VBA in PowerPoint?

I was wondering, if there is a maximum number of videos in ppt that one can import via "AddMediaObject2" in VBA?
I get a strange error, if I try to import several videos with the following macro (which is a dummy version just to show the problem) :
Sub loadVideos()
Const path As String = "D:\video.avi"
For folder = 1 To 20
Debug.Print "folder: " & folder
Dim sld As Slide
Set sld = ActivePresentation.Slides.Add(ActivePresentation.Slides.count + 1, ppLayoutTitleOnly)
sld.Select
For i = 1 To 8
Dim oShp As Shape
Set oShp = sld.Shapes.AddMediaObject2(FileName:=(path), _
LinkToFile:=msoFalse, _
SaveWithDocument:=msoTrue, _
Left:=0, _
Top:=0, _
Width:=-1, _
Height:=-1)
Next i
Next folder
End Sub
It works as expected with smaller video files (so code should be alright!?) but I get the error
"Runtime error -214....
Shapes.AddMediaObject2 : Invalid request, PowerPoint cannot insert any video from the chosen file. Check whether the necessary codec for this media format is installed and repeat the procedure.
"
if I use it with larger videos or alternatively increase the number of videos on each slide or the number of slides containing videos.
I doubt that it has something to do with video codecs, because inserting the videos manually works fine and also the videos inserted so far (till the error occurs) work fine. I also checked that RAM is not completely filled by all these inserted videos.
So I suppose there may be some internal stack or cache memory problem in ppt. Is there any restriction to the number or size of media files that can be loaded in ppt?
I would be very grateful, if someone could either tell me that it may be some bug in ppt or may point to some sort of remedy. Maybe I can configure ppt to accept more/bigger videos?
Many thanks.
Same story for me i could only add 8 mpg files (regardles of installed codecs).
Solution is to open and close presentation after adding single file (sample code above)
ps After converting mpg to avi method works fine...
For Each myBrand In Brands
Set ReturnPPPresentation = PptApp.Presentations.Open(tmpPresenationPath, withwindow:=msoFalse)
Set mySlajd = PPTTemplate.Slides(2).Duplicate
With mySlajd
Set shp = .Shapes.AddMediaObject2(myBrand.MultimediaPath, msoFalse, msoTrue, 218, 118.22, 384.71, 211.77)
Set shp = Nothing
End With
ReturnPPPresentation.Save
ReturnPPPresentation.Close
Next myBrand

Auto Update every picture in a PowerPoint Slideshow

I have a very large and complex PowerPoint with a full size image as the background for every individual slide. I have a directory full of these background pictures, which are all JPEG's. Sometimes I change these photographs, which are made with Photoshop, and when I change them, I save them over the original JPEG in the same directory.
I then have to update them in the PowerPoint. The manual functios to do this is to right-click the photograph, and click replace photo, then pick the new JPEG from the file menu. I want to write a macro in Visual Basic that will go through the entire slideshow, and reload the graphic from the updated JPEGs, without me having to do it manually. I don't know if it is possible, because I don't know if PowerPoint actually remembers the path and filename of each picture that it places.
Is there a picture property which contains the path and filename that was used to paste the picture in the first place? If there is, I could find that property, query it, and use that as the path to reload and thereby update the picture recursively all the way through slide by slide.
Would this be possible?
Is there a picture property which contains the path and filename that was used to paste the picture in the first place?
Not that I'm aware of, but you can create your own, using the Tags property:
http://msdn.microsoft.com/en-us/library/office/ff744290(v=office.15).aspx
You will have to assign a custom tag for each slide, like:
Sub AssignTag()
Dim sld as Slide
Set sld = ActivePresentation.Slides(1)
sld.Tags.Add "img_location", "C:\files\image1.JPG"
End Sub
Once the slide's Tags have been configured, then you can do something like this to update from that location:
Sub UpdateJPGs()
Dim sld As Slide
Dim path As String
For Each sld In ActivePresentation.Slides
path = sld.Tags("img_location")
If Not path = vbNullString Then
On Error Resume Next
sld.Background.Fill.UserPicture path
If Err Then
MsgBox "Unable to update slide #" & sld.SlideNumber
Err.Clear
End If
On Error GoTo 0
End If
Next
End Sub