From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Multiple application/workbook handles necassary?

Solved!
Go to solution

I justed started working with CVI and Excel and I am Using the Excel Report Library.

Basically, what I am doing is: I open existing Excel Templates, edit the docs and save and close it (see code below).

 

Currently I have a workbook handle for each workbook that I open, but I am using only 1 application handle for all the workbooks. It works so far but I am not sure whether I will run into troubles later.

 

1. Is it necessary to create multiple application handles for each workbook?

2. When talking about the "application", what does it refer to? The Excel Application? If yes, another application would be Word or another instance of Excel?

 

/*Declaration Handles*/
	static CAObjHandle workbookHandle2;  
	static CAObjHandle workbookHandle3;      
	static CAObjHandle workbookHandle4;
	static CAObjHandle applicationHandle; 

/*Create new app*/
	ExcelRpt_ApplicationNew(VTRUE,&applicationHandle);

/*open existing wb*/
	ExcelRpt_WorkbookOpen(applicationHandle,"C:\\testfile123.xltx", &workbookHandle2);
	ExcelRpt_WorkbookOpen(applicationHandle,"C:\\testfile123.xlt", &workbookHandle3);
	ExcelRpt_WorkbookOpen(applicationHandle,"C:\\testfile123.xltm", &workbookHandle4);

/*save templates*/
	ExcelRpt_WorkbookSave(workbookHandle2, "testfile2.xlsx", ExRConst_DefaultFileFormat);	
	ExcelRpt_WorkbookSave(workbookHandle3, "testfile3.xlsx", ExRConst_DefaultFileFormat);	                   
	ExcelRpt_WorkbookSave(workbookHandle4, "testfile4.xlsx", ExRConst_DefaultFileFormat);    

 

Thanks,

ZerMahlMer

0 Kudos
Message 1 of 4
(4,617 Views)
Your method is correct.
Single application handle is just fine.

The important point is, do not forget to discard your handles when you do not need them (after you close the Excel app).
If you do something wrong you can see the Excel.exe process in the Task Manager even if you close it from CVI (at least, this is what happens to me when I close the app when there are still undiscarded handles in memory)

Good luck 😉
S. Eren BALCI
IMESTEK
0 Kudos
Message 2 of 4
(4,607 Views)

Thx man! That is just the confirmation I needed!

0 Kudos
Message 3 of 4
(4,586 Views)
Solution
Accepted by topic author ZerMahlMeer

Good, I am glad you are good to go! 😉

 

I see I have conflicted with myself in the second paragraph of my post.

 

"The important point is, do not forget to discard your handles when you do not need them (after you close the Excel app)."

 

Corrected version:

You should discard your workbook/worksheet (not the application handle of course) handles before you quit Excel.

Otherwise Excel does not disappear from task manager even if it looks like it is terminated.

S. Eren BALCI
IMESTEK
Message 4 of 4
(4,580 Views)