Deleting shape in PowerPoint programatically and undoing causes shape to be "lost" - powerpoint

(This seems mostly to be a PowerPoint 2007 specific problem, which I can't easily reproduce in PPT 2010)
Easy way to reproduce locally is to:
1) Insert a shape into blank slide
2) Run command: ActivePresentation.Slides(1).Shapes(1).Delete in immediate window in Visual Studio. (You can alternatively delete through C#)
3) Undo the deletion in the PowerPoint presentation (do this non-programatically)
For some reason, you cannot access the shape again using calls like:
ActivePresentation.Slides(1).Shapes(1) //Does not allow any methods/properties to work
The only thing I've remotely gotten is that through Selection.ShapeRange, you can kind of get a reference to the item, but most of the Properties/Methods throw ComExceptions when trying to using that object.
Does anyone know how I can re-get the shape or somehow refresh the presentation to get some clean Com Objects?

I can confirm in Ppt2007 SP3. Try as workaround .cut and .paste. Afterwards I was able to access the other methods/properties. - cheers, www.MSO-dlx.com
ActivePresentation.Slides(1).Shapes(1).Delete
Application.CommandBars.ExecuteMso "Undo" 'or manually Undo
ActiveWindow.Selection.SlideRange(1).Shapes(1).Cut
ActiveWindow.Selection.SlideRange(1).Shapes.Paste
ActiveWindow.Selection.SlideRange(1).Shapes(1).TextFrame.TextRange.Text = "ABC"

so, I can confirm that this is an issue; even in 2010 and found a "cheaper" alternative:
Public Sub arf()
Dim arf As Slide
Dim shape As shape
Dim shapes As shapes
Set shapes = ActivePresentation.Slides(1).shapes
Set shape = shapes(2)
shape.Select
shape.Delete
Application.CommandBars.ExecuteMso "Undo"
MsgBox ("shape: " & shape.Name & ",Type: " & shape.Type)
Set shapes = ActivePresentation.Slides(1).shapes
Set shape = Nothing
Set shape = shapes(2)
' Cut and paste makes this work, but not required...
'shape.Select
'shape.Cut
'shapes.Paste
'Set shape = Nothing
'Set shape = shapes(2)
Set arf = shape.Parent
MsgBox ("slide: " & arf.Name)
End Sub

Just now I have successfully solved the problem, share the web page which helped a lot : https://www.add-in-express.com/creating-addins-blog/2014/06/24/exception-hresult-0x800a01a8/
The critical point is releasing the object after deleting it,which is exactly as Skovly and sharkTwo did.However,I don't know how to do this using C#,and the link gave me the answer.
Marshal.ReleaseComObject(titleShape); titleShape = null;
Just like this.

Related

How to replace text in Powerpoint with activeX textbox value?

I'm sorry if I can't explain this very well. Language barrier and all that. Might explain why I wasn't able to google a solution... Anyway.
I'm trying to make an interactive Powerpoint where in the first slide the user writes their name in an ActiveX textbox and the name then repeats in several other slides. Because the name would be in the middle of different texts my solution was to add a placeholder word that would then be replaced with the textbox value. I was able to Frankenstein a code that replaces one name with another that works well enough but I can't get the textbox to work with it. Best I can do is replace the placeholder name with some variation of TextBox1.Value and while I know what causes that issue I can't work out a solution.
Not sure if it makes a difference but the plan is to add some sort of "OK" button next to the active textbox that runs the macro.
Sub Findandreplace()
Dim sld As Slide
Set sld = ActivePresentation.Slides(1)
Dim shp As Shape
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasTextFrame Then
If shp.TextFrame.HasText Then
shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "word1", "word2")
End If
End If
Next shp
Next sld
End Sub
ActiveX objects have a different set of properties/methods than regular PPT shapes. Assuming you have a reference to the AX textbox in a variable oSh,
osh.OLEFormat.Object.Text
will give you the text in the ActiveX textbox

How do I add a hyperlinks to image using VBA?

I have a script that will run on GPO on Windows Server 2012.
It's pretty simple stuff, but I cannot fathom how to add a hyperlink around an image! I have:
objSelection.InlineShapes.AddPicture "linktoimage.html"
This works a dream, I can see the image and there is no issue. But how do I add a hyperlink to this image so that when folks click on it they are taken to my desired hyperlink location. I know adding an image is simply a line of code, hoping for same for adding a hyperlink.
I am not doing this in excel or anything of the kind, just Notepad++
Assuming this is about Word, you need to save a reference the shape you just created and then use that reference as an argument to Document.Hyperlinks.Add
Option Explicit
Sub LinkImageTest()
Dim oSelection As Selection
Dim oDocument As Document
Dim oShape As InlineShape
Set oSelection = Application.Selection
Set oDocument = oSelection.Document
Set oShape = oSelection.InlineShapes.AddPicture("https://yt3.ggpht.com/-Pde_zs2tuj0/AAAAAAAAAAI/AAAAAAAAAAA/iBq9KSwTTLk/s88-c-k-no-mo-rj-c0xffffff/photo.jpg")
oDocument.Hyperlinks.Add oShape, "http://www.microsoft.com"
End Sub

Can you "unroll" an animation in Powerpoint to slides?

I am currently preparing a "final report" document in Powerpoint that requires me not to include animations. However, the various source documents to be combined include animations.
I am searching for a way to automatically generate a slide for each animation step. Basically the same functionality as exporting the slides to PDF with animation steps, with the difference that the animation steps remain powerpoint slides.
Is there a way to achieve this automatically, or do I have to duplicate the slides and prepare the animation steps myself?
Use the PPspliT add-in for MS Office by a man named Massimo Rimondini. It does exactly what you want. I checked in Office 2013 and it works fine.
Bonus:
In case that plugin fails to remove all the animations and convert into separate slides (which is very unlikely), you can disable any remaining animations for slide show, or remove them altogether using a macro.
Disabling animations:
You can turn off the animations by going to "Setup Slide Show" and under "Show Options" tick the "Show without animation" option and click OK. Now run the show and it will display without the animations.
Removing animations altogether:
If you really want to delete all the animations in a single sweep then you will need to run this macro.
Sub StripAllBuilds()
Dim I As Integer: Dim J As Integer
Dim oActivePres As Object
Set oActivePres = ActivePresentation
With oActivePres
For I = 1 To .Slides.Count
If Val(Application.Version) < 10 Then
' Older versions of PowerPoint 97/2000
' In each slide set the animation property
' of the Shape object to FALSE
For J = 1 To .Slides(I).Shapes.Count
.Slides(I).Shapes(J).AnimationSettings.Animate = msoFalse
Next J
Else
' New versions support the Timeline object
For J = .Slides(I).TimeLine.MainSequence.Count To 1 Step -1
.Slides(I).TimeLine.MainSequence(J).Delete
Next J
End If
Next I
End With
Set oActivePres = Nothing
End Sub

How to Save Embedded Images in Powerpoint 2013 To Local Disk Through VBA?

I have lot of pictures embedded in the Microsoft Powerpoint 2013 presentation. In Powerpoint 2013, I can right click on a picture object and then use "Save As Picture" to save the image to local disk. But this is manual way. I want to know if there is any way of automating the same through VBA? I am able to loop through the Shapes collection and then determine the type of the object. But I am not able to figure out which properties or methods are relevant for storing the picture to the local disk? Following is code snippet of iterating through the Shapes collection and determining if the Shape is of type msoPicture
For currentSlideIndex = 1 To totalSlidesCount
Set currentSlide = ActivePresentation.Slides(currentSlideIndex)
For Each mediaShape In currentSlide.Shapes
mediaCounter = mediaCounter + 1 'for each shape in the slide
If currentSlideIndex = 3 Then
If mediaShape.Type = msoPicture Then
MsgBox ("Shape Type Picture is " + CStr(mediaShape.Type) )
End If
End If
Next mediaShape
mediaCounter = 0 'Reset to 0 for the new slide
Next
You can export the current shape to a picture using the shape's .Export method.
This is normally hidden, so you won't get intellisense help on it by default, so:
Go into the IDE
Press F2 to open the object browser
Rightclick in the pane to the right of Classes, where you see "Members of ..." at the top.
Put a check next to Show Hidden Members
Now you'll get intellisense guidance when you type .Export on a slide shape.
Note also that if a shape is Type 14 (placeholder), it can contain a picture, so you may want to add something like:
If mediaShape.Type = msoPlaceholder Then
If mediaShape.PlaceholderFormat.ContainedType = msoPicture Then
' it's also a picture
End If
End if
And to add further entertainment value you might also have .Type = msoLinkedPicture, and even in some cases msoEmbeddedOLEObject that happens to be a picture.

Use system tray & icons in VBA (Access)

I've found a few tutorials that explain how to use the windows API to get a custom icon in the system tray.
These are all for Visual Basic, and they don't seem to be scaling to VBA well.
I'm following this short tutorial:
http://atchoo.org/vb/systray.php
Basically, you have to set the hIcon value (a 'long' variable) but it does not work.
I've tried to use the LoadPicture() function, which does not give me any errors, but also fails to add a new icon.
I can't supply Me.Icon, nor can I set it on Form_Load.
Does anyone have any experience with this?
Using loadpicture was the right approach, but not directly.
I had to define a new variable first, and load that.
Like this:
Dim myPicture As IPictureDisp
strPath = "F:\Databank\Icons\stone.ico"
Set myPicture = LoadPicture(strPath)
And then, somewhere along the way, I could set hIcon without problems:
.hIcon = myPicture
When I change the trayicon (like, say, adding a balloontip) I have to supply the icon information again, too.

Resources