Excel Graph to PowerPoint C++/CLI Exception from HRESULT: 0x800A03EC - excel

I am trying to copy excel graphs and then pasting them into a PowerPoint using C++/CLI although I have run into difficulties when trying to use the Copy() method. Here is a simplified version of my code right now:
Worksheet = dynamic_cast<Excel::Worksheet^>(WS[3]);
chartObjects = dynamic_cast<Excel::ChartObjects^>(Worksheet->ChartObjects(paramMissing));
existingChartObject = dynamic_cast<Excel::ChartObject^>(chartObjects->Item(2));
existingChartObject->Copy();
shapeRange = Slide->Shapes->Paste();
but when I try this it give me an error of:
An unhandled exception of type 'System.NullReferenceException' occurred in Excel To PPT.exe
Additional information: Object reference not set to an instance of an object.
Now my Excel workbook has 6 graphs on each sheet starting on sheet 3. And if I copy all the graphs on sheet 3 and past them onto a slide like this.
Call this attempt one
for(count = 1, count <= 6; ++count){
Worksheet = dynamic_cast<Excel::Worksheet^>(WS[2]);
chartObjects = dynamic_cast<Excel::ChartObjects^>(Worksheet->ChartObjects(paramMissing));
existingChartObject = dynamic_cast<Excel::ChartObject^>(chartObjects->Item(count));
existingChartObject->Copy();
shapeRange = Slide->Shapes->Paste();
}
Then there is no exception thrown. Now if I were try to do the same thing but start at graph 2 instead of 1 I get the exception thrown. Like this:
Call this attempt two
for(count = 2, count <= 6; ++count){
Worksheet = dynamic_cast<Excel::Worksheet^>(WS[2]);
chartObjects = dynamic_cast<Excel::ChartObjects^>(Worksheet->ChartObjects(paramMissing));
existingChartObject = dynamic_cast<Excel::ChartObject^>(chartObjects->Item(count));
existingChartObject->Copy();
shapeRange = Slide->Shapes->Paste();
}
When I step through it and I hover over existingCharObject on the second attempt it is not empty and looks the exact same as in attempt 1.
Why would the second attempt throw an exception but the first wouldn't? I should be able to copy any excel ChartObject I want? It is forcing me to start at ChartObject 1 in the whole workbook and work my way up and not allowing me to randomly copy ChartObjects.
Let me know if there is more information you need. I did not want to past my whole project into this, this is the only part of the project that is giving me issues.

Related

VBA - Using Average function in Sheets.Range

I've tried searching a few different threads but wasn't able to get a solution.
My macro is going to transfer data from one sheet to another. I need to use the AVERAGE function to get the average of a range (on the other sheet)
I'm getting a RUN TIME ERROR 438 error and can't figure out why. I tried using Application.WorksheetFunction but this doesn't bear the correct result and works on the MAX function but not the AVERAGE. I saw a few solutions that involve creating a loop and variables, but I assumed a simple solution should be possible.
Code below:
Option Explicit
Sub Step8CopytoLean()
Dim wblean As Workbook
Dim wbmaster As Workbook
Set wblean = Workbooks("SLA Reporting Lean.xlsx")
Set wbmaster = Workbooks("SLA Reporting MasterFile.xlsx")
Workbooks("Lean.xlsx").Activate
Worksheets("Data").Delete
Workbooks("MasterFile.xlsx").Activate
Worksheets("Data").Copy After:=Workbooks("Lean.xlsx").Sheets("Summary")
Workbooks("Lean.xlsx").Sheets("Summary").Activate
wblean.Sheets("Summary").Range("E4").Value = wbmaster.Sheets("Summary").Range("K20")
wblean.Sheets("Summary").Range("F4").Value = wbmaster.Sheets("Summary").Range("M20")
wblean.Sheets("Summary").Range("E5:E6").Value = wbmaster.Sheets("Summary").Average(Range("K9:K11")) 'line with error
wblean.Sheets("Summary").Range("F5").Value = wbmaster.Sheets("Summary").Max(Range("M8:M11")) 'line with error
Modify the below and try:
Application.WorksheetFunction.Average(ThisWorkbook.Worksheets("Sheet1").Range("A1:A10"))
Something like this:
wblean.Sheets("Summary").Range("E5:E6").Value = Application.WorksheetFunction.Average(wbmaster.Worksheets("Summary").Range("K9:K11"))
wblean.Sheets("Summary").Range("F5").Value = Application.WorksheetFunction.Max(wbmaster.Worksheets("Summary").Range("M8:M11"))

Run-time error '438' Object doesn't support this property or method - At rowcount 0

I wrote a VBA script that runs through a bank statement finding payments of a certain value. This code ran when I wrote it, but a couple of months later I get Run-time error '438' Object doesn't support this property or method; here is an extract of the code.
Set bankWB = ActiveWorkbook
Set bankWS = ActiveSheet
For rowcount = 0 To 250
skipline = False
If bankWS.Cells(rowcount, paidcol) = 5 Then
payee = Trim(bankWS.Cells(rowcount, payeecol))
paid = "5"
The error occurs on the 'if' line.
I've found various examples that reference cell values in the same way and this worked previously. If you Google this error there are lots of results and I see that using 'range' is more common than using 'cell' to reference a single cell, but it's not obvious to me how to easily translate my (x,y) cell variables/loop into a range "A1" type value so I've not tried rewriting the code to use range instead yet.
I assume that the object is bankWS.Cells and it's the property rather than the method that is the problem. I want to know what the value of the object is (=property), I'm not trying to do anything with the object (=method)
I've seen lots of different ways of writing the cell object, and tried various permutations, none of which make a difference. eg.
If worksheet.bankWS.Cells(rowcount, paidcol) = 5 Then
If bankWS.Cells.item(rowcount, paidcol) = 5 Then
If bankWS.Cells(rowcount, paidcol).value = 5 Then
I'm thinking that the code is ok, and that something else has changed.
Solved
Thanks to #99moorem my row count loop should have started at 1, not 0, there will be no cell(0,1).

Add sparklines to excel with matlab

With MATLAB I can start a COM server and programmatically write to an Excel workbook. However, I can't figure out a way to add sparklines (suggestions appreaciated):
% Open new workbook
excel = actxserver('excel.application');
excel.visible = 1;
wrkbook = excel.Workbooks.Add();
sheet = wrkbook.Sheets.Item(1);
% Write some data
sheet.Range('B1:Z1').Value = rand(1,25);
Here is the problem:
% Add column sparklines to 'A1', type 'xlSparkColumn' and DataSource: 'B1:Z1'
sheet.Range('A1').SparklineGroups.Add('xlSparkColumn','B1:Z1')
I get the following error:
Error using Interface.Microsoft_Excel_15.0_Object_Library.SparklineGroups/Add
Error: Object returned error code: 0x800A03EC
Close/cleanup
% Close without saving
wrkbook.Saved = 1;
wrkbook.Close
excel.Quit
delete(excel)
Reference to SparklineGroup Object (Excel). I am on win7 64bit, R2013a and Excel 2013.
Try:
xlSparkColumn = 2;
sheet.Range('A1').SparklineGroups.Add(xlSparkColumn,'B1:Z1')
In the future, if you want to figure out the corresponding value for a certain constant/enum, use the IL DASM tool as shown in these posts.
EDIT
Ok it turns out that the enumeration xlSparkColumn was not the real issue here, you could either specify it as a string argument or pass the underlying integer value for the enum.
The problem as you mentioned in the comments is that you had the R1C1 reference style set instead of the default A1 reference style, thus the range specified in your call was not valid in that format.
Either of these will work:
excel.ReferenceStyle = 'xlR1C1';
sheet.Range('A1').SparklineGroups.Add('xlSparkColumn','R1C2:R1C26')
excel.ReferenceStyle = 'xlA1';
sheet.Range('A2').SparklineGroups.Add('xlSparkColumn','B1:Z1')

Excel error 1004 "Unable to get .... property of WorksheetFunction class" appearing inconsistently

I have a VBA function within a spreadsheet which operates on another spreadsheet that is opened in an earlier stage of my macro. The macro used to work fine but just recently has started causing a 1004 error ("Unable to get RoundDown property of the WorksheetFunction class") when it runs.
I believe I understand what the error would be caused by (a problem running RoundDown) but I cannot see why it is getting triggered in my macro and the odd part is that when I go into Debug mode and step through the code in the VBE the error does not recur (despite nothing obviously changing).
Does anyone have a similar experience of this sort of error occuring inconsistently and know what I could do to resolve it?
I'm reasonably VBA/Excel-savvy, but any suggestions on further steps to diagnose it would be appreciated. I am wondering if there is some issue with the opened spreadsheet not being ready but I cannot see how.
The code is here. The error occurs on the line marked with a comment.
Public Function GetDatesA(sWorkbookname As String, sSheetname As String, sCell As String) As Variant
Dim vDateList() As Variant
Dim currentCell As Range
Dim n As Long
Set currentCell = Workbooks(sWorkbookname).Worksheets(sSheetname).Range(sCell)
n = 0
Do
If Trim(currentCell.Value) = "" Then
Exit Do
Else
ReDim Preserve vDateList(0 To 1, 0 To n)
vDateList(0, n) = WorksheetFunction.RoundDown(currentCell.Value, 0) 'error occcurs on this line
vDateList(1, n) = currentCell.Column
'Debug.Print currentCell.Value
End If
Set currentCell = currentCell.Offset(0, 1)
n = n + 1
Loop While currentCell.Column < XL_LAST_COLUMN
GetDatesA = vDateList
End Function
Other details are:
Excel version: 2010
File being opened resides locally on my C: drive; my macro is in a spreadsheet on the network
File format for both files is .xls (i.e. Excel 2003) - I don't have the option of changing this
Windows 7 (not that I think it would be relevant)
Two points I've tried already are:
Substitute a different worksheet function (e.g. Min(currentCell)) and that also causes the same problem
Having the file open already seems to stop the problem - I wonder if there is some way that the workbook which is being opened (rather than my main workbook with the macro in it) is not enabled for macros and this is interfering. But even if this is the cause I'm not sure how to get around it!
Any ideas?
This error occurs often when any argument passed to the worksheet function is not of the correct type or simply doesn't make sense.
For example, I've had this problem when calling WorksheetFunction.Asin with an argument bigger than 1. In your case, I'd guess currentCell.Value is a non-numeric value or one not according to your region settings regarding numbers.
Yes, the error message is really misguiding.
I got the "Unable to get * property of WorksheetFunction Class" error using Transpose, MMult,MDterm, and MInverse functions.
I was able to get my code to run by putting "Option Base 1" in the Declarations (before the actual code) section of the particular Module in the Editer.
Excel assumes "Option Base 0" which will add an extra row and column of empty cells. This will cause the error to occur and isn't immediately obvious to see.
I have come accross this before, and for me it was becase the criteria range made no sense, as Andre said above.
See example formula below:
.Cells(11, i).Formula = Application.WorksheetFunction.CountIfs(Sheets("Sheet1").Range("AC8:C" & n), "S")
Have a look at the Range... it makes no sense. Amended the range from "AC8:C" to "AC8:AC" and it will work perfectly

Setting VBA Excel Chart Legend Position Property in MS Access not working

This is a very strange issue I am facing for a while now, when creating some Excel worksheets programmatically from MS Access 2003.
Using this VBA-Code snippet I am not able to set the Position property of an Excel's Legend object (variable definitions and so on are left out to ease understanding).
...
Set ChartObject = myWorksheet.ChartObjects.Add(myRange.Left, myRange.Top, myRange.width, myRange.Height)
Set Chart = ChartObject.Chart
Chart.HasLegend = True
'This line raises an error:
Chart.Legend.Position = -4107 '=xlLegendPositionBottom
...
MS Access always raises the Error 1004:
"Unable to set the Position property of the Legend class"
It confuses me that I can use the exact same code from within Exel VBA and it just works. What confuses me even more is that the property HasLegend can be set whithout any error ocurring.
Someone has a hint to solve this issue?
After building a generic version of my code I found that in Office/Excel 2003 I have to populate my Series of data to the chart before changing the Legend's position. Probably the object hasn't been initiated before. My Code should therefore look like this:
...
Set ChartObject = myWorksheet.ChartObjects.Add(myRange.Left, myRange.Top, myRange.width, myRange.Height)
Set Chart = ChartObject.Chart
Chart.HasLegend = True
...
Chart.SeriesCollection.Add myRange, 1 '=xlRows
'Now this works:
Chart.Legend.Position = -4107 '=xlLegendPositionBottom

Resources