PowerPoint VBA change picture of inserted audio file - vba

I'm struggling with the following issue in Powerpoint:
I insert an audio file into a slide; the inserted shape looks like below:
I want to change the default picture (the speaker) with a picture loaded from a file. I can do this by going to "Audio format -> Change picture -> From a file" menu
The shape will now look like this, showing the picture loaded from file:
How can I do this from VBA code?
a) Using Fill.UserPicture is not working, as UserPicture is something else - the shape will look like this:
b) Using the classic technique of remembering original shape position, deleting it and replacing it with a new shape/picture doesn't work here, as it will also delete the audio.
Thank you for any suggestion,
Adrian

Thanks to John SR Wilson, here is a working approach (as Change Picture is not exposed in vba):
Set oMed = ActivePresentation.Slides(1).Shapes(1) 'For example!
T = oMed.Top
L = oMed.Left
With oMed
.MediaFormat.SetDisplayPictureFromFile ("PATH TO IMAGE")
.Height = 40 ' image will be 100% of original size so resize to about 40 points (you can change the value if you want)
.Top = T
.Left = L
End With

Related

Save all Pictures of slide into single JPG image using VBA

I would like to do the following in VBA powerPoint:
I have a powerPoint with six pictures on it. Ideally I would like to group the six pictures to create one new picture as a .jpg, then export the .jpg picture to a file. After exporting the photo, I would like to then delete the 6 individual pictures so I can import the single .jpg picture.
I have used this to start: Save all Shapes of slide into single JPG image . I understand the comment, but do not know how to execute it.
If there is a better way to do this, please share.
Thank you!
This will convert the currently selected shapes to a PNG, paste it back onto the slide and delete the original shapes.
You may want to modify this to ensure that something is selected and quit gracefully if not, or if it's just for your own use, let PPT/VBA scold you if you forget to select something.
Sub ConvertSelectionToImage()
Dim oShapes As ShapeRange
Dim oGroup As Shape
Dim oSingleShape As Shape
' Get a reference to the selected shapes
Set oShapes = ActiveWindow.Selection.ShapeRange
' Group them so we can later pick up their coordinates
Set oGroup = oShapes.Group
' copy to clipboard
oGroup.Copy
' paste from clipboard as PNG to retain transparency
Set oSingleShape = ActiveWindow.Selection.SlideRange.Shapes.PasteSpecial(ppPastePNG)(1)
' Position the pasted PNG to match original shapes
With oSingleShape
.Left = oGroup.Left
.Top = oGroup.Top
End With
' And delete the original shapes
oGroup.Delete
End Sub

PowerPoint: Repeat text on every slide

I'm working on creating a PowerPoint template for daily class presentations. In the template I'd like to have a hunk of text that is prominently displayed on the first slide and which repeats at the bottom of the subsequent slides at the bottom in a smaller size. The text will change every day.
The ideas I've had so far:
Use a text field. As far as I can tell, PowerPoint doesn't have anything like a text field that can be dynamically set.
Use a footer - this works and I can modify the master to get the look I want, but I'd really like to be picking up the value of the text from the first page so that edits would be automatically applied and to save the initial step of setting the footer.
Using VBA - I'd be willing to give this a shot, but I've never used VBA and the learning curve seems steep so it would be nice to know if the idea is feasible in VBA.
Can this be done? How would you approach it?
Here's an example of what I'm hoping to be able to do. Ideally the solution would work on both the Mac (2013) and Windows (2016) version of PowerPoint.
You can connect your presentation with an excel file. And running the code in the ppt would pull out the text in the excel file and update the titles instantly.
Create a presentation with a textbox with some temporary text. Put the below code in ppt. save as pptm.
Sub AddMotionPath()
Dim Temp As String
Excel.Application.Workbooks.Open ("D:\Users\Desktop\Book1.xlsx") ' update the path of the excel file
Workbooks("Book1.xlsx").Activate 'activate the file
For p = 1 To 4
Temp = Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("B" & p + 1).Value ' Column B has the titles
ActivePresentation.Slides(p).Shapes(1).TextFrame.TextRange.Text = Temp ' this updates the titles from excel to ppt slide
Next
Excel.Application.Workbooks("Book1.xlsx").Close False 'closes the excel file
End Sub
Let me know if this works for you. You can update the excel file and run the macro in ppt. The text in the slides will be updated automatically.

How to choose a different header image in MS Word 2013 using a macro/button

I'd like to create a Word stationary template with ability to cycle through different colored logos in its header. My company uses a logo in five different colors and I would like to create a single template with a button that would allow me to cycle through the different colored logos every time I create a new document from this template. Can this be done, perhaps with a little VBA?
Edit:
After working with an answer from Olle Sjögren I've come up with the following working script:
Option Explicit
Public imgCounter As Integer
Sub cycle_logos()
Dim I As Variant
Dim logoColors(4) As String
logoColors(0) = "logo_magenta.png"
logoColors(1) = "logo_teal.png"
logoColors(2) = "logo_orange.png"
logoColors(3) = "logo_red.png"
logoColors(4) = "logo_grayscale.png"
For Each I In logoColors
ActiveDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item(I).Visible = msoFalse
Next I
imgCounter = imgCounter + 1
If imgCounter = 5 Then imgCounter = 0
ActiveDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item(logoColors(imgCounter)).Visible = msoTrue
End Sub
It is worth mentioning how I came up with the image names, since I didn't find a way to do this from inside Word. I renamed the template extension to zip and unzipped it. In the extracted files I opened word\header2.xml (this may vary, depending on the position in the document) and edited the nodes containing the names of pictures, i.e. <wp:docPr/>, e.g.:
<wp:docPr name="Picture 1" id="1"/>
became:
<wp:docPr name="logo_magenta.png" id="1"/>
etc. I then replaced the XML file in the ZIP with my edited version and changed the extension back to dotm.
As mentioned, there are several ways to do this. I would suggest storing the images outside of the template, otherwise all documents based on the templates would include all logo images, even if they are not shown, making the documents bigger than they need to be. That approach makes installing the template a bit harder, since you would have to copy the images to the clients as well as the template file.
To answer your question regarding addressing the images in the header - you can address then through the correct story range object. In my example I assume that they are in the primary header. To tell the different images apart, you can use the Name property or the index in the Item collection:
ThisDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item("Picture 1").Visible = msoTrue
ThisDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange.Item(1).Visible = msoFalse

Original Image Size

Using interop.powerpoint in VB.Net I am inserting an EMF file with the code:
' add picture shape to slide
AvailableSlide.Shapes.AddPicture(FileName:=file, LinkToFile:=MsoTriState.msoFalse, _
SaveWithDocument:=MsoTriState.msoTrue, Left:=SlideHorizontalPosition, Top:=SlideVerticalPosition)
' save original image size
mOriginalImageWidth = CurrentShape.Width
mOriginalImageHeight = CurrentShape.Height
' if EMF then crop the bottom
CurrentShape.PictureFormat.CropBottom = mCropValue
I have the original size saved before cropping. However, if I'm using a selected image in a PPT file I didn't save, I can't figure out where the original image size is stored in the selected "shape" object (5.49" by 4.13" in the image below). I assumed it would be in the PictureFormat somewhere.
dgp
Set the shape's .ScaleHeight and .ScaleWidth to 1 to return it to the original size.
This may or may not work but it's worth a shot (I can't test because I don't have Powerpoint). After a little bit of research on the PictureFormat interface, it has a few members that might be useful to you. I didn't exactly see anything that specified the picture's original size, but there were these four properties that seemed helpful: CropLeft, CropRight, CropTop, and CropBottom. They return the number of points cropped off each side respectively. A way to get the original size of the object would be to add the Width to CropLeft + CropRight and add the Height to CropTop + CropBottom. Try that and let me know if it works. Documentation is found here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.powerpoint.pictureformat_members(v=office.14).aspx
Try getting the shape's ScaleWidth and ScaleHeight properties. I couldn't find it in the documentation, but awhile ago, I had a student work with me to create an application to format powerpoint presentations that were imported into microsoft word. Here's the code that dealt with the scale size of the shape.

VBA deletes image in body instead of header in MS-Word

I have MS Word files that have a header with 2 Text Boxes and one image (as logo) and some tables, texts and images in body.
I'm trying to remove the image in header (logo) with this VBA code:
Dim tmp As Shape
Dim dShape As Shape
For Each tmp In ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes
If tmp.Type = msoPicture Then
Set dShape = tmp
End If
Next
dShape.Delete
In the first test it worked correctly! After that it removes body image instead of header image!!!
It seems we should first select the Shape we want to delete!!
I don't know why when I say shape1.Delete it thinks : let me see, mmmm... I think i is better to do shape2.Delete , so i'll do that.
any way this works:
dShape.Select
dShape.Delete
VBA is a fool (Just like its platform)!