LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to protect Excel document with ActiveX

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.

0 Kudos
Message 1 of 7
(3,672 Views)

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

National Instruments
0 Kudos
Message 3 of 7
(3,654 Views)

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".

0 Kudos
Message 4 of 7
(3,631 Views)

Thanks all,

 

I just plugged in all unknown parameters with "CA_DEFAULT_VAL" and everything was done. 🙂

 

0 Kudos
Message 5 of 7
(3,624 Views)

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.

Help share your knowlegde
0 Kudos
Message 6 of 7
(3,587 Views)

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.

 

0 Kudos
Message 7 of 7
(3,555 Views)