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
Related
In the PowerPoint UI I can convert a graphic into AutoShapes using the Group>Ungroup command in the context menu. It is converted into a group of shapes.
Additionally it is possible to regroup them.
Why are these methods either missing (in case of conversion) or raising errors (in case of ungrouping) for msoShapeType = 28?
Here's an example that works here. Note that some of the shapes in ungrouped SVGs can be edited using Edit Points but the EMF version cannot. I don't know why that'd be. A limitation of EMF, perhaps.
Sub TestConvert()
' Ungroups the currently selected icon shape
Dim oSh As Shape
Dim oSl As Slide
Set oSh = ActiveWindow.Selection.ShapeRange(1)
Set oSl = oSh.Parent
' Copy the shape then paste it back as an EMF
oSh.Copy
ActiveWindow.View.PasteSpecial ppPasteEnhancedMetafile
' At this point you might want to oSh.Delete to remove the original shape
' get a reference to the newly pasted EMF
Set oSh = oSl.Shapes(oSl.Shapes.Count)
' Ungroup it
Set oSh = oSh.Ungroup(1)
' Ungroup it again
oSh.Ungroup
End Sub
Maybe I need to dig a little further:
The original use case was to export a graphic shape originally inserted from svg file back again into an svg file.
We were facing two problems:
Exporting a powerpoint shape to an svg file from the UI creates a vector graphic wrapper for a pixel graphic even if the shape was originally inserted from an svg file. If you insert the new svg file again into powerpoint it is still recognized as graphic but it does not behave as before. The vector benefits are lost. For example you cannot colour its frames.
No svg option was given to the almost not documented vba shape.export method.
For the first problem we found the workaround to "Convert To Shape" first. Exporting the group of shapes creates a proper svg file, which after inserting again into powperpoint behaves like the originally inserted svg file.
For the second problem we tried some third party libraries, but all exported the shape into a similar vector graphic wrapper for a pixel graphic as Powerpoint UI did before and working fine with prior converted group of shapes.
So the idea was to combine both workarounds and convert the graphic into a group of shapes and exporting it into an svg file with help of a third party library.
But converting a graphic (type=28) unfortunately cannot be converted by vba (see above).
Exporting to emf does not make a difference. It creates a vector graphic wrapper for a pixel graphic inside. The advantage of the svg file was that you can open it with a text editor and have a look what is inside.
Maybe someone has an idea, how to solve the original use case with a completely different approach. Thank you for reading ...
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
I'm automatically generating a powerpoint slide through VBA, User Forms, and Excel. You run the VBA script in excel, fill out the data, the data goes into cells in excel, then the VBA script pulls the data and puts it into textbox shapes in the slide.
My problem is I want to use different font sizes at different times, so for example 28 pt font for one part and 14 pt for the rest. The problem is that any property changes I make to the textbox applies to all of the text within the shape.
My current workaround is sloppy and is to just generate another textbox over the original and insert spacing in the original so it looks like the larger text is "in" the textbox while it's actually just sitting over a few empty lines set aside.
You can format specific substrings within a string, but it's very cumbersome, for example assuming shp is an object variable representing your textbox:
Sub foo()
Dim shp As Shape
Set shp = ActivePresentation.Slides(1).Shapes("TextBox 3")
shp.TextFrame.TextRange.Text = "Hello, world!"
shp.TextFrame.TextRange.Characters.Font.Size = 14 'applies uniform font to entire shape
shp.TextFrame.TextRange.Characters(1, 5).Characters.Font.Size = 28
End Sub
Example output:
The difficulty of course is working with mixed formats, and I do not think there is any easy solution. It will be up to you to determine what formats you need to "capture", and what subsequently implement the appropriate conditional logic to transfer those formats to the PowerPoint shapes.
One possible alternative -- and this is the route that I would go if I were you -- would be to copy the cell from Excel, and use this method to paste in to PowerPoint. I believe this will create a table consisting of a single cell, in the PowerPoint slide. You will have to make adjustments for size/position, but this should be an order of magnitude easier than trying to capture every possible variation of font formatting:
Sub foo2()
Dim shp As Shape
Dim xl As Object
'Get Excel and copy a specific cell
Set xl = GetObject(, "Excel.Application")
xl.Workbooks("Book35").Sheets("Sheet2").Range("B4").Copy
'Paste that cell in to PowerPoint as a table, preserving formats:
ActivePresentation.Slides(1).Select
Application.CommandBars.ExecuteMso "PasteSourceFormatting"
End Sub
Example output, as copied from the Excel cell:
No need to change the font in excel to reflect in Word. You can do it directly. Just paste the below mentinoed line in Word VBA : -
Activedocument.Shapes("sam").TextFrame.TextRange.Words(1).Font.Size = 28
I have a Visio drawing and I want to be able to Select shapes from it and paste them to other sheets depending on certain variables.
What is the code for selecting the different shapes on the page. I am trying this but it is not working.
Dim vsoSelection As Visio.Selection
vsoSelection.Select Visio.Shape(1), visSelect
What am I missing here?
and also is it possible to get the strings entered into text boxes into Visio?
I am assuming you're getting an error because your vsoSelection object is nothing. So you need to do:
Set vsoSelection = ActiveWindow.Selection
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)!