How to add image/text to a named placeholder object in powerpoint using vb scripting? - vbscript

I am trying to find a way to add images to existing placeholders in a slide with a custom layout using a standalone .vbs script. I am able to generate empty slides with the custom layout applied correctly. Now, I want to add text/image to specific placeholders that I have created in the layout. I would like to ideally use the name of the placeholder for each slide as set in the template powerpoint to add data. For example, I have 5 placeholders in my layout, namely, title, image1, image2, text1, text2. I would like to add image/text to these placeholders using their name. I found the AddPicture and Placeholder methods from documentation and various posts but I am not able to successfully add an image or text. The kind of solution I am looking for looks like this since it gives me the opportunity to use the placeholder by name (not necessarily a one liner):
Slide.Shapes.Placeholders("name_of_placeholder").AddPicture()
What I typically find online is something like this (or variations):
Slide.Shapes.Placeholders("name_of_placeholder").Select msoTrue
call oSlide.Shapes.AddPicture("image_path", msoTrue, msoTrue, left, top)
However, I am not able to make it work in any way so far. Can someone point me to the correct syntax for the above problem? I am using office 2019 and I am new to using vb scripting for powerpoint. So any help is much appreciated.
Thanks a lot.

After some further investigation, I found one solution and also an issue I was not aware of. The solution is to simply use the AddPicture and Text methods by iterating over all the shapes:
'Iterate over all shapes of a slide oSlide and get an individual shape oShp object.
'Use With keyword to access the current object and then use Shapes.AddPicture / Text method.
For Each oShp in oSlide.Shapes
With oShp
call oSlide.Shapes.AddPicture(image_path, .Left, .Top, .Width, .Height)
oShp.TextFrame.TextRange.Text = oShape.text
oShp.TextFrame.TextRange.Text = oShape.text
End With
Next
Now, what I actually wanted was to be able to use named placeholders to set data for each item in each slide. I checked (at least in my case) that when I create a slide and apply a layout that has named placeholders (image1, text1,...), the placeholders revert their names to generic names such as 'Text Placeholder 1' or 'Picture Placeholder 1' in the instantiated slide. That's why I couldn't access the placeholders by name in my slide instances. So before adding any text or image, if I check the name of the Shape, I am able to update everything correctly. In my case, I also change the placeholder names to my custom names after a slide has been instantiated and before it is updated. So 'Picture Placeholder 1' becomes 'image1'. An example given below.
if Instr(oShp.Name, "Picture Placeholder 1") > 0 Then
call oSlide.Shapes.AddPicture(oShape.text, .Left, .Top, .Width, .Height)
End If
So naming the placeholders in the master slide custom layout was not useful for me. Current solution works for me and can update one title, two images, and two text placeholders correctly.

Related

Inserting image to slide with empty placeholder (PowerPoint)

I want to insert a picture to my slide, but if there is an empty placeholder, the picture automatically gets inserted into this placeholder.
Is there a way to tell PowerPoint to avoid empty placeholders when inserting a picture?
Best regards
Jesper
Is there a way to tell PowerPoint to avoid empty placeholders when inserting a picture?
No, but you can work around it by inserting your picture, getting a reference to the inserted shape, duplicating it, then deleting the original inserted shape.
The duplicate will be a picture, not a placeholder containing a picture.
That works most of the time; if your needs are more complex and/or you need to preserve the original placeholder, you'll need to add content to any empty placeholders, then insert your picture, then delete the added "bogus" content.

VBA - Powerpoint 2010 ppShp.Fill.UserPicture does not update pictures

I have a Powerpoint presentation with a VBA script to update the images every week. Recently, I added 2 additional slides and scripting to update those images as well.
The new images do not update and do not throw an error. If I step through the procedure, it goes to the correct slide, selects the correct shape, but the
Call ppShp.Fill.UserPicture ("blah_blah.png")
does not replace the image.
If I copy the line to one of the slides where it does work, it does.
In the example below, the first one works, the second doesn't.
'FEOL *******************************************************************
ActiveWindow.View.GotoSlide (15)
Set ppShp = ActivePresentation.Slides(15).Shapes("FEOL")
ppShp.Select
Call ppShp.Fill.UserPicture("D:\MIT Trends\Images\Feol_Composite.png")
'LICONSEM *******************************************************************
ActiveWindow.View.GotoSlide (16)
Set ppShp = ActivePresentation.Slides(16).Shapes("LICONSEM")
ppShp.Select
Call ppShp.Fill.UserPicture("D:\MIT Trends\Images\LICONSEM.png")
Again, I can see it go to the correct slide and select the correct shape (named in the "arrange" panel). It just doesn't fill. I tried changing to shape ID, but the results are the same. I know the shape name and image name are correct. I even tried cut/paste to be sure.
Using the image for the first section to fill in the second does not work. Using the image from the second section to fill in the first does. I feel like I must be missing something obvious.
Please find my code in attached image.
First u need to create shape in PPT and then run code.

Resizing an image to place into a different sheet, using the default naming convention

I have a macro that takes a range of cells, copies them as an image, and places them in a separate sheet. I would like to take the image, resize it, and place the correctly sized image into a different sheet.
Sub heatmapToJPEG()
Range("G1:V33").Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
'places image of heat map into Setup tab
Sheets("Setup").Select
Range("M1").Select
ActiveSheet.Paste
End Sub
I would like to re-size the image one it is in the Setup tab, but when excel places it there, it gives it the default name of Picture 1, Picture 2, Picture 3, or whatever iteration I am on before closing and reopening the workbook.
Is there a way to assign a permanent variable name to the image found in the Setup tab? If so, my thinking was to assign it to the variable name, and pass it off to another sub for resizing and placement. I am sure there are better ways, but am hitting this roadblock.
It's bad practice all around but just add a .Name property at the end -
ActiveSheet.Paste
Selection.Name = "name"

Convert string automatically to inline image

I'm using InDesign's data merge to generate playing cards for my game. Is it possible to convert a specific string to an inline image?
"You may roll :red_die: and add the rolled valued to this card's value"
For example the :red_die: in the text above would be automatically converted to an inline icon of a red coloured die.
No you can't this way. But you can place images with datamerge to the condition some fields of your source is set as an image one meaning having a "#" prefix. InDesign will process fields like #image as an image to place. It's up to you to add such a field in your source. However you can't nest it within another datamerge tag so it may not work eventually.
Other solution is using F/C dialog to replace :red_die: with clipboard content (your image) or to use scripting. You may also consider variable content plugin like EasyCatalog.

Load Picture to Microsoft Access 2010 Ribbon from Table

I'm working with Access 2010's new ribbon feature, and I'm trying to figure out the simplest way to load an image to the ribbon from a table. I know that I could do this by using a GDI API function, but from what I can see the code looks convoluted and appears to utilize extra dlls that I would just as well avoid calling if I don't need to. I also know that I could create a control on a form, load the image to the form, and then load the image from the form to the ribbon image variable. While using the form is doable, it seems sloppy. Eventually, I will resort to one of the two preceding methods if I have to, but it just seems like there should be some simple way to load an image from a table directly into an image variable for the ribbon.
One thing to note is that I'm willing to use whatever image format is easiest. These icon files are very small and so I'm not concerned about file size and I also don't care whether or not they are capable of transparency. So if there's a method that only works with bitmap file types that is fine with me.
Here's the code I have for the button in the ribbon XML:
<button id="CCTrans" label="Credit Card Trans." getImage="GetMnuIcn" onAction="=OpenCCTrans()" />
And here's the code I have for retrieving the image:
Sub GetMnuIcn(control As IRibbonControl, ByRef Image)
Set Image = DLookup("Img", "LtblImg", "Lbl=""" & control.ID & """")
End Sub
I've tried storing the image both as an OLE Object and as an attachment. Either way, when I try to retrieve it, it gives me the error "Type Mismatch". For simplicity, I've been storing it as a bitmap, but I also tried .gif and .png in my first attempts.
Note: Apparently the ribbon icons are IPictureDisp objects. If I can just figure out how to load an image from a table into one of those object types, I should be able to use it for the ribbon.
Also, there's a lot of good information on Microsoft's website, but I haven't been able to piece it together into a solution yet. Here's a link: http://msdn.microsoft.com/en-us/library/bb187398.aspx
Well after tedious days of trying to find a better solution, I'm forced to pick between what I feel are various mediocre approaches. I really wanted to just load it directly into a variable, but the only method for doing that requires references to a bunch of dlls and has code that is pretty complicated. To avoid any extra dependencies and to keep my code simpler, I decided to go the route of assigning the table containing the attached images to a form, opening that form in hidden mode and then assigning the picture variables from the form.
For some reason the code that I saw elsewhere that loaded the pictures on demand via this method caused my program to crash and so I had to load the pictures into variables when the program opened and then have the menu bar set the icons from those variables when the menu is selected.
My code is as follows:
Dim Icn(15) As IPictureDisp, IcnLbl(15) As String
Function SetMnuIcn()
Dim Img As Recordset, i As Byte
DoCmd.OpenForm "frmImg", acNormal, , , acFormReadOnly, acHidden
Set Img = Forms!frmImg.Recordset
While Not Img.EOF
i = i + 1
Set Icn(i) = Forms!frmImg!Img.PictureDisp
IcnLbl(i) = Img!Lbl
Img.MoveNext
Wend
DoCmd.Close acForm, "frmImg", acSaveNo
End Function
Sub GetMnuIcn(control As IRibbonControl, ByRef Image)
Set Image = Icn(GetAryLoc(IcnLbl, control.ID, 15))
End Sub
Function GetAryLoc(varValues As Variant, varMatch As Variant, NumOfValues As Integer) As Integer
Dim i As Integer
For i = 1 To NumOfValues
If varValues(i) = varMatch Then GetAryLoc = i
Next
End Function
Note: To ensure that the icons have been loaded prior to when they are needed, I specified SetMnuIcn to run on ribbon load:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="=SetMnuIcn()">

Resources