Information on VBA Chart Exporting - excel

I usually export Excel charts with code like:
Sub SaveChart()
Dim ch As Chart
Set ch = ActiveChart
ch.Export Filename:="C:\TestFolder\tempxx.jpg", FilterName:="JPG"
End Sub
I would like to explore using other picture formats. I would like to know what are the valid values for FilterName. However VBA Help has nothing useful. Can anyone point me an online resource that gives this information??

GIF and PNG are the commonly used picture format which should work With Chart.Export. FilterName is The language-independent name of the graphic filter as it appears in the registry under HKEY_LOCAL_MACHINE\Software\Microsoft\Shared Tools\Graphics Filters.

For many years (beginning with Excel 2003) I used GIF but at some stage Excel became incapable of exporting my charts properly, giving the saved chart a black background that rendered in grey in some applications. Switching to PNG has restored the functionality

Related

How to make double line chart not rounded in the end (how to change cap style)?

When I create a line chart in Excel and change line style to double, I can see something similar to the image below:
As you can see the beggining and ending are rounded. I want to get rid of that, so it looks like this:
It can be done by changing line "cap type". I have tried exploring Series.Format.Line methods and properties but all I can do is modify beggining/ending arrows, there is no property/method for cap. I have also tried to use excel macro recorder to try and somehow expose this property/method but I have not been successful. I have also tried to search through various Excel/VBA forums but nothing helped.
Anyone able to help with my issue? I am looking for solution in VBA and VB.NET.
It seems that this property is not exposed in VBA, so probably you are out of luck.
According to https://social.msdn.microsoft.com/Forums/en-US/577b74db-9f9d-400c-a419-1999809922da/change-chart-data-series-line-format-cap-type-in-vba:
The cap type and join type are not exposed in the VBA object model in Excel 2007 and 2010 (I don't know about Excel 2013), so the only way to set them in VBA is using the notoriously flaky SendKeys to control the Format Data Series dialog.
The article talks about office 2010, but I coudn't find any clues that this changed later.
The object that should have this information is the Format.Line-property of a data series or data point (class is LineFormat), but it shows no cap-property.
The (not so) funny thing is that the enumeration for the possible values is available in VBA:
Try this sequence of formatting (example of formatting the first series):
Dim testSeries As Series
Set testSeries = ActiveChart.SeriesCollection(1)
With testSeries.Format.Line
.DashStyle = msoLineSquareDot
.DashStyle = msoLineSolid
.Style = msoLineThinThin
End With

How to position a chart copied from excel into powerpoint using CommandBars?

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.

Chart is not copying correctly from Excel to Access

Using data from Access, I have created a line chart in Excel. When I go to copy/paste the chart into a report my DB, the image is not the same. But, if I paste the same image into a work doc or outlook email, it looks fine. The same distorted version appears if I paste it into msPaint. Please see the attached screenshots of the charts. The big issue is the x-axis labels. Here is my copy/paste code.
Dim objChart As Chart
objChart.CopyPicture xlScreen, xlBitmap, xlScreen
DoCmd.OpenReport ReportName:="rptDeptWindowChart", View:=acViewDesign
ctlC = Reports!rptDeptWindowChart.Controls.Count
Do
For Each ctl In Reports!rptDeptWindowChart
ctl.Name = "ctlDelPic"
DeleteReportControl "rptDeptWindowChart", "ctlDelPic"
Next
ctlC = Reports!rptDeptWindowChart.Controls.Count
Loop Until ctlC = 0
DoCmd.RunCommand acCmdPaste
Access is not well-known for it's charting capabilities. If I were you, I'd stick to using Access for managing large data sets, and export final results of queries to Excel, and do your charting/plotting/graphing in Excel. Word is great for creating documents, PowerPoint is great for presentations, etc. You need to use the right tool for the right job.

Excel 2016 VBA - Problems upgrading from Excel 2013

I developed several userforms and macros for a project in Excel 2013, but when I try to test them in Excel 2016, I get multiple errors on basic VBA functions.
For example I get an "Object doesn't support this property or method" error from the following code:
Private Sub ShowImpact()
FormImpact.Show
End Sub
Another example, the following code gives me an error "Can't find project or library" on the [RIMS_tbl] table reference. This is a named table in the document:
With FormImpact.cboIndustry
.ColumnCount = 11
.ColumnWidths = "0;50;0;0;0;0;0;0;0;0;0"
.RowSource = "=RIMS!" & [RIMS_tbl].Address
.BoundColumn = 1
.TextColumn = 2
End With
As another example, I get the same error message on "Format" method in the following code:
Private Sub txtConLand_AfterUpdate()
txtConLand = Format(txtConLand.Value, "#,##0")
End Sub
Lastly, I have a label that I'm using as a background on the form, which works fine in Excel 2013, but for some reason it covers all of the other controls in 2016, and I can't send it to the back.
I'm really hoping that there is some setting in 2016 that will fix all of these problems, because I can't figure out why so many things would break between these two versions. Thanks for your help!
As mentioned in the comments above, the solution to this problem was in eliminating missing references between different version numbers. In the Visual Basic interface, go to Tools->References. Check for missing references in all versions supported. Thanks #TimWilliams and #Stefan for the solution to this problem.
I never did figure out how to fix the problem of the z-order for the background label. Even setting the v-order for that label programmatically didn't work. But I did come up with a workaround. First I made a small white bitmap file. Then under the page settings, I set the background picture to be the bitmap file. Then I set set the "PictureSizeMode" property "fmPictureSizeModeStretch," which stretched the bitmap to fit the entire page. Finally, I added a border around the entire form.
I had the same problem when migrating an VBA powered Excel from Excel 2010 to 2016. Deselecting the ATLEntityPicker 1.0 Type Library from the References list solved it.

Excel VBA "AxisBetweenCategories" property missing in ChartSpace

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

Resources