I am trying to copy some charts (embedded) in different worksheets of an excel workbook using vbscript into different slides of a powerpoint. I would like to keep the link between the excel sheet and the powerpoint while doing so and therefore I used the below piece of code that allows me to keep formatting and link (instead of a simple paste or PasteSpeical. Is there any other way?) :
For i = 1 to TotalNumWorkSheets 'I iterated with indices.
Set pptSlide = pptPres.Slides.Add(i, 11) 'There is one opening slide before this.
set ws = wb.Worksheets(i)
ws.ChartObjects(1).Chart.ChartArea.Copy
pptApp.CommandBars.ExecuteMso("PasteExcelChartSourceFormatting")
pptApp.CommandBars.ReleaseFocus
With pptSlide
.Shapes.Title.TextFrame.TextRange.Text = objCurSheet.Name
'Adding some more textboxes here. Working fine. Position checks out in PPT.
.Shapes(.Shapes.Count).Left = 20 'Doesn't work for all slides.
End With
Next
The code works and copies all the charts, creates titles, adds new text as expected, but I am not able to position the charts on individual slides because after the ExecuteMso command, I don't know how to access the reference to the chart. I read in one of the SO posts that pasting using this method looses the chart selection but you can access the last .Shapes object since pasting always adds the object to the end of the list. Is that always the case? I tried accessing and positioning my chart by accessing the last object but it only works for the first slide of the loop (i.e. the first chart pasted is shifted to Left=20). The rest all charts in other slides are centered. Can someone explain where and how to add the formatting chart options? I ran into an even weirder problem. If I increase the number of worksheets, even the first plot looses it's Left formatting. The above code is the only place where I add formatting so I don't know what is happening. I am sure I am not formatting it correctly.
Many thanks for your suggestions.
Edit: One additional thing which I tested. I am using
WScript.Sleep 500
code before the For loop ends since it gives enough time for earlier operations to finish (at least that's what I understood from many other google searches).
So after some more searching and testing, I found a solution (for my case at least).
Moved the WScript.Sleep 1000 (500 didn't work for me) statement just below CommandBars.ReleaseFocus.
It makes some sense now since it is the Chart copying, pasting and linking from excel that needs time. Especially with source formatting. After that there is only text generation which I believe is not so heavy.
Cleared all the set Object variables when not used. Especially the ones associated with the "With" keyword.
set obj = CreateObject()
With obj
'Do something here.
End With
set obj = Nothing
Not clearing them, apparently, can also prevent you from closing the applications even after you use the .Close and .Quit method. At least that's what I observed. I found PowerPoint.exe running in the task manager when nothing was opened.
After doing the above, I am able to copy with format the charts and also able to set the position of the charts. The weird problem of larger number of worksheets also disappeared. Hopefully, it might help others. If someone thinks the observations are incorrect or troublesome, please correct.
Related
I have a macro that is intended to copy a chart from excel into a word document in the same way as manually copy and pasting using "Keep Source Formatting and Embed Workbook." Below is the code that, to my understanding, should accomplish this.
Set reductionChart = graphWorksheet.ChartObjects("reduction")
reductionChart.Copy
masterReport.Paragraphs.Last.Range.PasteAndFormat wdChart
graphWorksheet is the worksheet that contains the graphs, masterReport is the word document.
The issue I am having is error 4605 command not available on the PasteAndFormat line. I happened to discover that manually copying the graph then running the line worked without issue. Thinking that maybe right clicking copied in a different way than .copy so I record a macro of the action and ended up with:
ActiveSheet.ChartObjects("Reduction").Activate
ActiveChart.ChartArea.Copy
Even substituting this in the error still occurs. What is happening here?
After some additional testing I am thinking that possibly when using .copy the chart is sort of only stored within excel and not the clipboard so when paste and format looks for something it see an empty clipboard and has an error, but right clicking copy stores it to the clipboard hence why is available still even after I run ActiveChart.ChartArea.Copy again.
I've attempted to create new workbook with a single sheet and chart. Also tried using late binding instead on the off chance that did something. This is the full code still giving the same issue
Sub test()
Dim masterWord As Object
Dim masterReport As Object
Set masterWord = CreateObject("Word.Application")
Set masterReport = masterWord.Documents.Add
masterWord.Visible = True
ThisWorkbook.Worksheets(1).ChartObjects("Chart 1").Copy
masterReport.Paragraphs.last.Range.PasteAndFormat wdChart
End Sub
After much testing this is the best workaround I'm managed to figure out.
reductionChart.Chart.ChartArea.Copy
masterReport.Paragraphs.Last.Range.PasteAndFormat wdFormatOriginalFormatting
masterReport.InlineShapes(masterReport.InlineShapes.Count).LinkFormat.BreakLink
wdFormatOriginalFormatting and wdPasteDefault both work and don't seem to make a difference to the outcome as far as I have seen.
This has one issue that I've found which is that on ActiveDocument.Fields.Update an information box will popup warning that the linked file is unavailable. This will occur for each unlinked item. I attempted to use Application.DisplayAlerts = False but this did not prevent the popup. This may come back to bite me, but I simply removed this line as it was unnecessary for my purposes.
I vainly try to set the AxisBetweenCategories property to False in a Userform ChartSpace. It is a clusteredBarChart. I did the same manually in a normal chart and it worked. Recording a Marco generated the code with the AxisBetweenCategories property. Why can't I use it in the Userform ChartSpace.
me.ChartSpace1.Charts(0).Axes(0).AxisBetweenCategories = False 'doesn't work
What do I miss?
Thanks
The "Chart" inside a ChartSpace object is of type ChChart and the same properties and methods you use on a Chart (worksheet, or chartsheet) do not translate directly so unfortunately the macro recorder won't be of much help, you will have to turn to good old fashioned debugging, trial & error.
I use the Locals window to examine the objects for a hint at the object model (that's how I observe the type is ChChart, etc.)
You can then look at the available properties and Google usually can point you in the right direction, like this example. Exploring the intellisense, I see that there are really limited options that are not the same as the Chart objects on a worksheet.
After all of that, this is probably not the answer you want to hear, but it can't be done the way you want it to be done.
This seems to verify that observation and suggests that while some things are the same, others can simply not be rendered the same way.
[Chartspace Charts do] not have the refined axis crossing as Excel
does. Features between the two will be similar in certain area and
not in others
And a similar example from Microsoft:
This example sets the category axis to cross the value axis at value zero (0) in the chart workspace.
Sub SetCrossingValue()
Dim chConstants
Dim axValueAxis
Dim axCategoryAxis
Set chtContants = ChartSpace1.Constants
Set axValueAxis = ChartSpace1.Charts(0).Axes(chConstants.chAxisPositionValue)
Set axCategoryAxis = ChartSpace1.Charts(0).Axes(chConstants.chAxisPositionCategory)
axValueAxis.CrossingAxis = axCategoryAxis
axCategoryAxis.CrossesAtValue = 0
End Sub
I've researched this to death and feel like I'm the only person it's ever happened to.
I have some VBA that:
Creates a copy of 3 sheets to a new wb
In the new wb converts to values, deletes objects (shapes and controls) and all but 3 ranges
Opens an existing third file and sets the contents of three ranges in that to match the new wb
Closes the existing file (saved)
Closes the new wb (saved)
Gives a message box saying complete
At the end of all this, something weird happens with the state of the windows. The selected cell does not appear selected. If I try and click a control afterwards, it selects the object (hence users could drag them). It shouldn't and this is the big problem.
I've tried selecting a cell through code, it throws an error. I had limited success by forcing drawing mode off using Call CommandBars("Drawing").Controls("Select Objects").Execute and activating a specific sheet & selecting a cell. However, even then if I even click on a few cells afterwards, the next time I select a control it will select it as an object rather than click the thing.
I have no idea why and can't find anyone who's seen this before.
Any ideas on what I can do?
Thanks,
Basil
I didn't figure it out entirely, but I did find a fix. Hopefully it works for anyone else who finds this problem.
At the end of the code I added this:
ActiveSheet.Shapes.Range("ctrlExportPrices").Select
ActiveSheet.Range("B8").Select
So it forced a control on the sheet to be selected, and then a cell.
The next time I select the control manually, it clicks it rather than selecting the drawing object.
Just to let you all know, I'm a beginner to VBA and I'm using Excel and PowerPoint 2013.
The aim: I'm writing code in Excel VBA to paste a chart from Excel into PowerPoint. Then when it's pasted, move it to a set place within the slide.
The code I used: The framework of the code I started with is in http://peltiertech.com/Excel/XL_PPT.html#chartppt, the part called 'Paste the Active Excel Chart into the Active PowerPoint Slide (Early Binding)'.
The main difference is around the 'Paste' part, this is instead a 'PasteSpecial' and my code is as follows:
.
Charty.CopyPicture
ppApp.ActivePresentation.Slides(SlideNumber).Shapes.PasteSpecial (ppPasteEnhancedMetafile)
' Setting the correct position on the slide
ppApp.ActiveWindow.Selection.ShapeRange.Top = ChartTop
ppApp.ActiveWindow.Selection.ShapeRange.Left = ChartLeft
(Note. ChartTop and ChartLeft have been defined earlier)
The issues: I'm aware that after the PasteSpecial it doesn't have a select statement. However, (1) When I run the above code I get a runtime error '-2147188160' saying the Selection.ShapeRange is an invalid request (in the 'ChartTop' line), ie. it isn't selected so it can't be moved. (2) When I add a select statement to the 'Paste.Special' line I get an 'Object required' error on the new 'PasteSpecial' line, ie. it doesn't recognise it as a paste select statement.
The odd part: When I run the same macro on other people's computers using the exact same Excel and PowerPoint files it works (without the select statement in the 'PasteSpecial' line). I've looked at 'Tools' => 'References', and both of our computer have the same settings.
The questions: Is there a setting that I have overlooked on my computer which prevents the code from working? Is there a way after I've pasted it to reselect it before it is moved (I should tell you that the macro is to paste in many graphs into ppt simultaneously)? How come for other people at my work it works on their computers without the 'select' part of the PasteSpecial statement? How come when I use the Shapes.PasteSpecial (ppPasteEnhancedMetafile).Select statement, it doesn't recognise it as an object?
I hope that's clear.
Thanks in advance.
Dim oSh as Shape
Set oSh = ppApp.ActivePresentation.Slides(SlideNumber).Shapes.PasteSpecial (ppPasteEnhancedMetafile)(1)
Now you don't need to select anything (which is best avoided whenever possible). Just manipulate the pasted shape, oSh, directly.
The curious syntax (the (1) at the end) is because Shapes.Paste or .PasteSpecial returns a shaperange, not a shape; the (1) causes it to assign the first shape in the range to the oSh variable. Since there's only one shape in the range, that suits us just fine.
I have the following code that works in all other circumstances except in a single where it returns the error Can't move focus because it is invisible, not enable, or type that does not accept focus. The data in sheet consists only basic numbers and words. My objective is to select a range from one work book and paste it to another. It appear that excel does not recognise anything to be in the cells, although there in fact is. Does anyone know why this may be happening?
Set Users = Application.Workbooks.Open(PathA)
With Prices
.Sheets("Sheet").Range("A:AJ").Select
Selection.Copy
End With
'Set Risk = Application.Workbooks.Open(PathX)
With Risk
.Sheets("Sheet").Range("A1:AJ1048576").PasteSpecial Paste:=xlPasteAll
.Save
' .Close
End With
Users.Close
it looks like someone else had the same issue and was able to resolve it on an MSDN forum
http://social.msdn.microsoft.com/Forums/office/en-US/3263b079-7e4f-452c-8dcc-92c682b8370b/excel-form-cant-move-focus-to-the-control-because-it-is-invisible-not-enabled-or-of-a-type-that?forum=exceldev
maybe this fix will apply to your situation too.
... OK so on that page, one guy has a kludge that seemed to work for him:
Error was:
Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus.
How to Fix
1. Select the object with the problem, e.g. the Form or Control.
2. In the properties windows, select the name of the object and rename adding an "x" to the end, e.g. Rename CPanel to CPanelx.
3. Save the Form.
4.Now rename the CPanelx back to CPanel.
5. Run your form.
6. Problem solved.
Something screwy with Excel VBA, not sure what !, but this will get you working again.
Steve D.
and then someone else described the underlying problem, but his solution involved using design mode, which seems like it is maybe defeating the purpose of automating in vba, but the moderator seemed to like his answer better, so here that is as well
In normal use activeX controls are intended only to be activated, not selected, there's a difference. If you partifcularly want to Select the control (manually or with code), try putting Excel into Design mode first.
See also the topic "Why can't I select form and ActiveX controls?" in Excel's help (not VBA's help)
Peter Thornton
"Run-time error '-2147352565 (8002000b)': Can't move focus to the control because it is invisible, not enabled or of a type that does not accept the focus" Can be overcome but sequentially defining the area you wish to select. Obviously some may do this in any case, but it this specific instance it is needed to overcome the issue. This is an example for copying from one workbook to another and pasting where U is the sheet excel finds to be "invisible".
Dim U As Workbook
Dim Us As Worksheet
Set U = Application.Workbooks.Open(Path)
Set Us = U.Worksheets("sheet")
With Us
.Range("A:AJ").Select
Selection.Copy
End With
U.Close SaveChanges = True
With DestinationWorkbook
.Sheets("sheet").Range("A:AJ").PasteSpecial Paste:=xlPasteAll
.Save
End With
Just to help in case you didn't find the answer till now :
In my case this message came out when I redimensioned the application window while it was maximized :
e.g. Application.width = 100 (on Excel 2003 or 2007)
The solution in this case, is to first bring the application window to NORMAL.
e.g. ActiveWindow.WindowState = xlNormal