vb.net pasting images into powerpoint
-
Hi everyone, I've written a visual basic gui program (using visual studio 2008 (.net)) and simply placed a button on it which, when pressed, activates the print screen of the active window and it then opens powerpoint, adds a slide, and it's then suppose to paste the image. A new slide gets created each time the button is pressed, and the image is supposed to be pasted to the new slide. The first iteration seems to work fine, but subsequent presses of the button yields the following error: "System.Runtime.InteropServices.COMException (0x80048240): Shapes (unknown member) : Invalid request. Clipboard is empty or contains data which may not be pasted here. at Microsoft.Office.Interop.PowerPoint.Shapes.Paste()" Other times the code goes an unlimited amount of button presses with no error, but when this occurs, the image is only updated every other button press. Any ideas?
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Core
Imports System.Drawing.ImagingImports System.Runtime.InteropServices
Imports System.DiagnosticsPublic Class Form1
' Start PowerPoint. Public ppApp As PowerPoint.Application ' Start counting number of times the take image button has been pressed by the user. Public num\_times\_pressed = 0 ' Add a new presentation. Public ppPres As PowerPoint.Presentation
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
num_times_pressed = num_times_pressed + 1If num\_times\_pressed = 1 Then ppApp = CreateObject("Powerpoint.Application") ' Make it visible. ppApp.Visible = True ppPres = ppApp.Presentations.Add(MsoTriState.msoTrue) ppApp.WindowState = PowerPoint.PpWindowState.ppWindowMinimized End If ' Add a new slide. Dim ppSlide1 As PowerPoint.Slide Dim SlideCount As Long SlideCount = ppPres.Slides.Count ppSlide1 = ppPres.Slides.Add(SlideCount + 1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank) Me.Hide() SendKeys.SendWait("%{Prtsc}") ppSlide1.Select() ppSlide1.Shapes.Paste() Me.Show() End Sub
End Class
Thanks!
-
Hi everyone, I've written a visual basic gui program (using visual studio 2008 (.net)) and simply placed a button on it which, when pressed, activates the print screen of the active window and it then opens powerpoint, adds a slide, and it's then suppose to paste the image. A new slide gets created each time the button is pressed, and the image is supposed to be pasted to the new slide. The first iteration seems to work fine, but subsequent presses of the button yields the following error: "System.Runtime.InteropServices.COMException (0x80048240): Shapes (unknown member) : Invalid request. Clipboard is empty or contains data which may not be pasted here. at Microsoft.Office.Interop.PowerPoint.Shapes.Paste()" Other times the code goes an unlimited amount of button presses with no error, but when this occurs, the image is only updated every other button press. Any ideas?
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Core
Imports System.Drawing.ImagingImports System.Runtime.InteropServices
Imports System.DiagnosticsPublic Class Form1
' Start PowerPoint. Public ppApp As PowerPoint.Application ' Start counting number of times the take image button has been pressed by the user. Public num\_times\_pressed = 0 ' Add a new presentation. Public ppPres As PowerPoint.Presentation
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
num_times_pressed = num_times_pressed + 1If num\_times\_pressed = 1 Then ppApp = CreateObject("Powerpoint.Application") ' Make it visible. ppApp.Visible = True ppPres = ppApp.Presentations.Add(MsoTriState.msoTrue) ppApp.WindowState = PowerPoint.PpWindowState.ppWindowMinimized End If ' Add a new slide. Dim ppSlide1 As PowerPoint.Slide Dim SlideCount As Long SlideCount = ppPres.Slides.Count ppSlide1 = ppPres.Slides.Add(SlideCount + 1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutBlank) Me.Hide() SendKeys.SendWait("%{Prtsc}") ppSlide1.Select() ppSlide1.Shapes.Paste() Me.Show() End Sub
End Class
Thanks!
If the error says Clipboard is empty, then most likely
SendKeys.SendWait("%{Prtsc}")
does not always correctly do what is supposed to. After the SendKeys bit, you could try checking the contents of the clip board first (My.Computer.Clipboard
), to see if the print screen was actually performed, and there is a picture to be pasted. If there isn't you just perform the SendKeys again, until the clipboard contains a picture.My advice is free, and you may get what you paid for.
-
If the error says Clipboard is empty, then most likely
SendKeys.SendWait("%{Prtsc}")
does not always correctly do what is supposed to. After the SendKeys bit, you could try checking the contents of the clip board first (My.Computer.Clipboard
), to see if the print screen was actually performed, and there is a picture to be pasted. If there isn't you just perform the SendKeys again, until the clipboard contains a picture.My advice is free, and you may get what you paid for.