0 Replies - 1367 Views - Last Post: 20 January 2016 - 05:14 PM

#1 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,886
  • Joined: 12-December 12

Check if Excel is Running and Workbook Open

Posted 20 January 2016 - 05:14 PM

This code will check if Excel is already running and use this instance if it is, otherwise it will start a new instance of Excel.

If Excel is running, it will also check if a particular workbook is open, otherwise it will open it.



If the intention is simply to open the workbook then the code is fine. If further automation of the file is intended then, personally, my preference would be just to request that the user exit Excel or, at least, to close the file.



Don't forget to Add Reference.. to the Excel Object Library.

(I'm using a WinForm and a Button but this is not significant.)
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; // Add Reference..

namespace WindowsFormsApplication4 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            Excel.Application xlApp;
            Excel.Workbooks xlBooks;
            Excel._Workbook xlBook = null;

            bool runningExcel = false;
            string fullname = @"C:\Users\Andrew\Documents\AndysData7.xlsx";

            try {
                // check if Excel is running..
                xlApp = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
                runningExcel = true;
            } catch (System.Runtime.InteropServices.COMException ex) {
                // start Excel..
                xlApp = new Excel.Application();
            }
            xlBooks = xlApp.Workbooks;

            if (runningExcel) {
                try {
                    // try and reference the book
                    xlBook = xlBooks.get_Item(Path.GetFileName(fullname));
                } catch (System.Runtime.InteropServices.COMException ex) {
                    // workbook is not open
                }
            } else {
                // new instance is not visible by default
                xlApp.Visible = true;
            }
            if (xlBook == null) {
                // open the book, whether from a new or existing Excel instance
                xlBook = xlBooks.Open(fullname);
            }
            xlBook.Activate();

            ReleaseObject(xlBook);
            ReleaseObject(xlBooks);
            ReleaseObject(xlApp);
        }
        
        private void ReleaseObject(object obj) {
            // http://www.siddharthrout.com/2012/08/06/vb-net-two-dot-rule-when-working-with-office-applications-2/
            try {
                Marshal.ReleaseComObject(obj);
                obj = null;
            } catch (System.Runtime.InteropServices.COMException ex) {
                obj = null;
            } finally {
                GC.Collect();
            }
        }
    }
}


Before any of this code, check whether File.Exists. There is no point starting the automation if the file isn't there.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1