01-31-2012 02:17 AM
Hi,
I want to protect the Excel report generated by my program and found this function Excel_WorksheetProtect() in excel2000.c
Is there any piece of example code which I can refer? I couldn't find any help from the CVI manual.
01-31-2012 09:45 PM
02-01-2012 12:30 PM
Here is a KB article that describes how to install the help for the Office ActiveX libraries.
http://digital.ni.com/public.nsf/allkb/8F5C9B290B68EBD286256FB700680D23?OpenDocument
This article is actually for Office 2003, but the same idea can be applied to 2007. More information can be found in the Microsoft KB as well.
http://support.microsoft.com/kb/q222101/en-us
02-06-2012 02:27 PM
They just don't want to give you a clear and exact answer, do they?
Here's how you save your Excel worksheet with password protection:
-----------------------------------------------------------------------------------------------------------------------
//... modified from excel2000dem.c shipped with your CVI
// ... assuming a current Excel file is opened, activated, and ready to be saved
// .... the workseet handle is "ExcelWorkbookHandle"
HRESULT error = 0;
VARIANT myVariant1, myVariant2;
char fileName[MAX_PATHNAME_LEN];
GetProjectDir (fileName); // must have directory's path
// save the current opened file to a different file name with password protection
strcat (fileName, "\\exceldem1.xls"); // The two backslashes "\\" must be included here
error = CA_VariantSetCString(&myVariant1, fileName);
if (error < 0) goto Error;
error = CA_VariantSetCString(&myVariant2, "xyz123"); // password is xyz123
if (error < 0) goto Error;
error = Excel_WorkbookSaveAs (ExcelWorkbookHandle, NULL, myVariant1,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
myVariant2, CA_VariantBool(VTRUE),
CA_DEFAULT_VAL, ExcelConst_xlNoChange,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, CA_DEFAULT_VAL);
if (error < 0) goto Error;
-----------------------------------------------------------------------------------------------------------------------
You'll see the password request when you open the file "exceldem1.xls".
02-06-2012 11:03 PM
Thanks all,
I just plugged in all unknown parameters with "CA_DEFAULT_VAL" and everything was done. 🙂
03-29-2012 02:49 AM
Does anyone know how to open an excel sheet with the password.
I would like to the file to have a password but when opened with my program it must not ask the user to enter the password. It must enter the password automatically.
04-05-2012 05:49 PM
When you open the Excel workbook in your program, use this function (as shown in the excel2000demo.prj):
CA_VariantSetCString(&myVariant2, "xyz123"); // password is xyz123
Excel_WorkbooksOpen (ExcelWorkbooksHandle, NULL, fileName,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
myVariant2, CA_VariantBool(VTRUE),
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
CA_DEFAULT_VAL, CA_DEFAULT_VAL,
&ExcelWorkbookHandle);
It will open the password protected file and enter the password automatically.