07-14-2014 11:13 AM
Hello!
I have issues with the filetype VARIANT and the Excel function Excel_WorkbookSaveAs.
I am getting the following error:
159, 5 error: unexpected type name 'VARIANT': expected expression
Below you will finde an excerpt of my code. I am creating the VARIANT var to store the filename of an Excel Workbook. To concert a string to variant I am using CA_VariantSetCString. I assume the reason for the error is a missing header file where the type VARIANT is defined, but I couldn't figure out which one.
Does anyone has an idea how to fix this error?
Thanks!
#include "excelreport.h" #include "excel2000.h" #include <cvirte.h> #include <userint.h> ... VARIANT vFilename; CA_VariantSetCString(&vFilename,"Test_xlsx_xls"); Excel_WorkbookSaveAs(workbookHandle, NULL,vFilename, CA_DEFAULT_VAL, CA_DEFAULT_VAL, CA_DEFAULT_VAL, CA_DEFAULT_VAL, CA_DEFAULT_VAL, ExcelConst_xlNoChange, CA_DEFAULT_VAL,CA_DEFAULT_VAL,CA_DEFAULT_VAL,CA_DEFAULT_VAL);
07-15-2014 02:06 AM - edited 07-15-2014 02:07 AM
From the code excerpt you posted it's not possible to understand what that error means; neither we know on which line the error arises...
VARIANT type is defined in OAIdl.h file: nevertheless, I discourage you from including this file alone, since you may be missing some other necessary include. Additionally, simply including excel2000dem.h should be enough for the program to compile successfully. In my opinion the error is not in a missing include, especially if you are using VARIANT type elsewere in the code without similar errors: it possibly lies in some problem in the lines immediately preceding the offending one (e.g. a missing semicolon or something like that).
BTW, the filename you are using is missing the necessary extension: this can prevent automatically opening the file once created and could ultimately generate runtime errors while running the code (I don't remember how tolerant is Excel automation in this respect).
07-15-2014 10:00 AM
The error occurs during the declaration of the
VARIANT vFilename;
and it is the first time that I am using the VARIANT type. Before adding the posted lines, the code compiled without a problem, so I would assume the VARIANT caused the problem. (This was the reason why I posted just the small excerpt ;))
Thanks for the hint with the filepath! I really need to change that in the future. Without any extension Excel just opens the doc from My Documents.
07-15-2014 10:31 AM - edited 07-15-2014 10:42 AM
Does the VARIANT declaration work anywhere else in the project or in other projects?
[Edit] I made a mistake in my build that I posted previously. Take a look at the LabWindows/CVI ActiveX Excel Example. You can find in in the Help >> Find Examples by searching Excel, or by looking here:
C:\Users\Public\Documents\National Instruments\CVI2013\samples\activex\excel
It looks like this example does not have the VARIANT type, but it at least shows you how to use ActiveX for Excel in LabWindows/CVI.
07-15-2014 10:41 AM - edited 07-15-2014 10:43 AM
Hello,
There is a Data Types shipping for LabWindows/CVI. You can find it by going to Help >> Find Examples and searching "variant". The name of the example is: DataTypes.cws.
Also it can be found here: C:\Users\Public\Documents\National Instruments\CVI2013\samples\activex\servers\Datatypes
This example does use VARAINT.
07-16-2014 07:50 AM
PMFJI, but unless you have C99 extensions enabled, you can only declare automatic variables at the start of a block (i.e. immediately after a {). If variables are declared after an expression, then you will get exactly the error you describe.
Have you enabled C99 extensions (Options|Build Options...)?