i need some help with my code. I wrote a macro which copies many tables as pictures from different Excel files to PowerPoint, but sometimes it works perfectly, and sometimes appears Error 1004 - CopyPicture of method class failed. Here is my code:
Sheets("List1").Select
' group chart
ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
' select cells to be copied to PowerPoint:
Range("A1:X83").Select
' copy cells:
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
' paste to PowerPoint:
pptSld.Shapes.Paste.Select
I found that for someone was helpful inserting Sheets("List1"). before Range("A1:X83").Select, but it didn't help.
I have this problem at work, where I'm using Office 2010. At home I have Office 2016 and code works perfectly.
Do you have any ideas, how to make it work without any errors?
Lose the habit of using .Select for everything; that's what macro-recorder code does, but only because macro-recorder code mimicks every manual action - when automating Office, you don't actually need to reproduce every mouse click you'd do when doing the task manually - instead you work with the Office application's object model.
</rant>
So you work off Worksheets("List1"), and copy a specitic Range as a picture:
Application.Worksheets("List1").Range("A1:X83").CopyPicture xlScreen, xlPicture
This code is much more robust than anything that relies on Selection: you're calling .CopyPicture off a specific Range object.
So all that's left to do is to paste:
pptSld.Shapes.Paste
Note that Paste is a method that doesn't return anything, so you can't legally do .Select on it - but why would you want to .Select it anyway?!
If you need to access the newly-created Shape object, you can retrieve it from the pptSld.Shapes collection:
Dim excelScreenshot As Shape
Set excelScreenshot = pptSld.Shapes(pptSld.Shapes.Count)
And then whatever you wanted to so with Selection, you can now do with excelScreenshot - and as a bonus you get IntelliSense and auto-completion for working with a strongly-typed object reference, as opposed to Object, which makes every member call a late-bound call - in other words you'll get better performance, however unnoticeable it might be.
Check your references to make sure that you're calling the correct library.
Tools > References
Not sure which on you need, but make sure you have Microsoft Powerpoint xx.x blah blah blah checked.
Related
Please read this fully and understand that this program was working fine until I changed the way I was hiding the workbook.
I have a program that worked great while I was using Application.Visible = False and only showing the user form. I came to realize that this would hide all Excel windows and not just the one I was using. This is going to be distributed throughout the department and hiding all Excel windows was unacceptable.
I started using ActiveWindow.Visible = False, but I am now getting Error 91 anytime I search a worksheet for a value (Cells.Find).
Modifying the worksheet is not an option and the value for which I'm searching can move around the sheet depending on what has been added or removed.
Cells.Find worked out great for this reason. I need to either find another way to search the page, or find another way to hide the worksheet. Please help
When the window is not visible, the Cells reference is not qualified to a worksheet object (unless qualified, Cells refers to ActiveSheet.Cells and there is no ActiveSheet), so you can do like:
Sheets("sheetname").Cells.Find ' modifying "sheetname" as needed
This may also fail (with the same error), or it could also yield incorrect results if there are other open workbooks, so it's best to qualify to a workbook fully, e.g.:
Workbooks("workbookname").Sheets("sheetname").Cells.Find(...
It is still a good idea to test the result of Find before performing additional method/property calls against an object which could be Nothing, as per this answer:
Find command giving error: "Run-time Error '91': Object variable or With block variable not set"
Having some struggles here.. Pretty new to coding and VBA, wrote a code using 2013 without realising I'd have issues moving backward to run on 2010 versions... Derp..
I'm having a bizarre issue.
I'm using worksheets("...").Activate to move between sheets (This was done to reduce the amount of "worksheets("...")." before every line of my code).
Anyway, the macro has no issue activating all the sheets except one. The sheet in question is where the original button is to run the code. I also can't seem to use the activex commandbutton (which is probably the source of my issue).
Note: I have already tried the "delete x files" which was caused by a windows update - this isn't the source of the issue, I can still add new activex controls etc
Edit: I've resolved the issue by changing from the ActiveX control to a button that calls a macro which calls the userform. It seems that something to do with having the ActiveX control on that sheet prevented it from being able to activate through a module. Anyone have an explanation for this?
I don't have an answer as to why you couldn't activate the sheet, and don't think I could answer it without seeing the workbook in question. But, I can help you to avoid using activate in the first place ;)
From your description it sounds like you're using Worksheet(index).Activate so then you can use ActiveSheet.SomeMember to work with the worksheet object.
If this is so, you could save yourself time and make your program more efficient by using a with block.
E.g.:
With Worksheets("...")
Debug.Print "working with """ & .Name & """."
Debug.Print "which belongs to """ & .Parent.Name & """."
End With
And if you need to reference it in multiple places, I'd recommend assigning the object to a variable.
E.g.:
Dim MySheet as Worksheet
Set MySheet = Worksheets("...")
With MySheet
'Do something
End With
MySheet.range("A:A").Copy Destination:= WorkSheets("Someothersheet").Range("B:B")
I have a presentation with 6 slides. Slide 5 contains vba / macro codes for rotatechart, change angle etc., The codes work well thru' vba editor 'RUN'in active document.(.ppt) but when put in 'Slideshow" mode codes don't work. Dbug msg: "Invalid request. no active document present". Pl help with code.
Sub MoveItInSlideShow()
With SlideShowWindows(1).Presentation.Slides(5).Shapes("Picture 2")
ActiveWindow.Selection.SlideRange.Shapes("Picture 2").Select
Application.Run "'Hora.ppt'!Slide7.CreateSpirograph"
end with
end sub
Thanks
This line is unnecessary, since you've already set a reference to the shape in the With statement previous:
ActiveWindow.Selection.SlideRange.Shapes("Picture 2").Select
But since you can't select anything in slideshow view, any attempt to .Select anything will cause an error, which is probably what you're seeing.
If the code here requires a selected shape, you'll need to rewrite it:
Hora.ppt'!Slide7.CreateSpirograph
I recorded the following macro :
Sheets("Rejets Techniques TGC").Select
ActiveSheet.ChartObjects("Graphique 1").Activate
ActiveChart.Axes(xlCategory).Select
ActiveChart.SeriesCollection(1).Values = "='Données'!$EU$68:$IJ$68"
ActiveChart.SeriesCollection(1).XValues = "='Données'!$EU$1:$IJ$1"
However when I try to lauch it I get this error (translated from french):
Execution error '-2147024809 (80070057)'
There is no element with this name
How can this be? if there was no graph named this way I wouldn't have been able
to record it.
(yes I'm running it from the good sheet)
Thanks.
Here's what it comes down to: Your chart is not an object on the sheet, it is the sheet.
So while you use ActiveSheet.ChartObjects("Graphique 1").Activate to start your code, there are no ChartObjects found in your sheet, because the sheet is the Chart. So here's how you get around it:
Dim CO As Variant
Set CO = ActiveSheet
CO.Axes(xlCategory).Select
CO.SeriesCollection(1).Values = "='Données'!$ET$68:$IJ$68"
CO.SeriesCollection(1).XValues = "='Données'!$ET$1:$IJ$1"
And this should work just fine. I noticed that when I looked at the chart tab, I couldn't get into any cells. This is not abnormal, but it is not the most common way (that I see) to create the chart. To verify, I added a watch on the ActiveSheet and saw that it was indeed a chart (of type Object/Graph2) with all the normal chart methods available to it.
From there, I just plugged in your code, converting to the CO variable (but yours should still work using ActiveSheet across the board), and ran with no errors.
As a side note, using ActiveSheet is not always effective, and it is generally better to explicitly call the sheet, i.e. Set CO = ThisWorkbook.Sheets("Rejets Techniques TGC")
1 - Check if the active sheet is the one that contaisn the chart. Or use the sheet name in code to run it from any sheet.
2 - Check if the good sheet contains the chart with exact "Graphique 1" name. Maybe there's an underline, like "Graphique_1", or no space "Graphique1"...
Seems there is some bug. Can't resolve this problem, all code is running fine and I am able to see the AutoShape is getting copied from Excel file but it is not adding it to PowerPoint. Popping up an error Run-time error '-2147188160(80048240) View.Pastespecial : Invalid Request. The specified data type is unavailable
If Range("H" & i).Value = 1 And Range("B" & i).Value = "FRONT" Then
objPPT.Presentations(1).Slides(9).Select
objPPT.ActiveWindow.View.PasteSpecial DataType:=ppPasteEnhancedMetafile
Your code will be faster and possibly more reliable if you don't rely on selecting anything:
With objPPT.Slides(9).Shapes
Set objShape = .PasteSpecial(ppPasteEnhancedMetafile)(1)
With objShape
' set coordinates and such here
End With
End With
As to why you're getting the error message, try stopping the code after you've put something on the clipbard. Then switch to PowerPoint, use Paste Special to see what paste options are available. If EMF isn't one of them, that's your problem ... you're not putting anything in EMF format on the clipboard.
I had a similar issue, but I found a different solution; it may be specific to what I was doing though.
I setup a program where I would:
(manual) Copy an entire webpage that was a report on several performance metrics
(manual) Pasted it in to excel
Run the program to extract the values I want and then clear contents of the sheet I pasted them on.
Eventually after many tests, it would fail with this same automation error when I tried to access the sheet:
Sheets("PDX Paste").Activate
I was able to activate every other sheet except that particular one, even using the index value instead of the direct name reference. After googling to no success I found out that the copy and paste from the website was also pasting invisible controls. When I found this out I had 1,300+ shapes when I only expected 1 (the button I use to trigger the program). It was actually only apparent when a glitch - presumably due to so much memory being used to store these controls - displayed for a few seconds.
I ran the following code independently and then appended it to the end of my program when I do the cleanup of the data. The code goes through the sheet and deletes any shape that isn't the same type as my button. It would have to be adapted if the shapes you want to delete are the same type as the shapes you want to keep. It also becomes simpler if you don't have any shapes to keep.
Dim wsh As Worksheet
Set wsh = ActiveSheet
Dim i As Integer
For i = wsh.Shapes.Count To 1 Step -1
If wsh.Shapes(i).Type <> wsh.Shapes("UpdateDataButton").Type Then
wsh.Shapes(i).Delete
End If
Next i
I'm not sure this would solve this problem, but hopefully this can help others and prevent loss of time figuring out what may be causing this relatively vague error message.