Example Code

How to programmatically close an Excel Workbook

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview

Using invoke nodes you can programmatically close a Microsoft Excel workbook via LabVIEW. This example demonstrates how to first save and then close an open Excel workbook using its name.

 

Description
Before running the VI, open any Excel workbook that is already existing as a file or create a new workbook in Excel. Keep Excel and the workbook open when running the attached VI and use only the workbook name which equals the Excel file name if the file already exists (e.g. "Test file.xls"). If the workbook is only existing in memory and was never saved before use its name which can be found in the excel title bar (e.g. "Book1") without file extension.
The example then tries to open a reference for this workbook (Open Workbook Ref.vi). If the reference was opened successfully, it is used by invoke nodes to save and close the workbook. The example also uses the DisplayAlters property of Excel to prevent any user interaction when saving and closing the workbook.

Requirements

 Software

  • LabVIEW Base Development System 2012 (or compatible)
  • Microsoft Excel 2013 (or compatible)

 Hardware

  • No hardware is necessary to use this example VI

 

Steps to Implement or Execute Code

  1. Download and open the attached ZIP-file
  2. Open the [Main] ...VI and follow the instructions in the frontpanel

 

Additional Information or References

[Main] Programmatically close Excel worksheet LV2012 NIVerified.vi - Front Panel.png

 

[Main] Programmatically close Excel worksheet LV2012 NIVerified.vi - Block Diagram.png

 

 

Note: You cannot use this example on RT targets. ActiveX is a Windows only technology and not available on RT targets.

 

Workbook.SaveAs Method (Excel)

https://msdn.microsoft.com/de-de/library/office/ff841185.aspx

 

Workbook.Close Method (Excel)

https://msdn.microsoft.com/de-de/library/office/ff838613.aspx

 

Application.DisplayAlerts Property (Excel)

https://msdn.microsoft.com/de-de/library/office/ff839782.aspx

 

**The code for this example has been edited to meet the new Community Example Style Guidelines. The edited copy is marked with the text ‘NIVerified’. Read here for more information about the new Example Guidelines and Community Platform.**
Many thanks for using the NI forums and I look forward to your reply.

Kind regards

David

Applications Engineer
National Instruments UK

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
javier.reyes
Member
Member
on

I have tried to do this in my application, but when I check on Windows Task Manager, Microsoft Excel is still opened.

What can I do for avoid Microsoft Excel still be opened (but not visible from the Windows Task Bar?

Ing. Electrónico - Director Proyectos - NECSYS S.A.S.
Cor_Kuin
Member
Member
on

Same Question for me as above. Excel seems to be closed after running my application but is still opened in the task manager and I can't open Excel again to read the file because it is still in use. I use the Dispose Report.Vi from the toolbox.