Home > OS >  How do I get the current excel filename in c#
How do I get the current excel filename in c#

Time:10-07

I am writing a VTSO application that should work across multiple excel document templates. I want the add in to check the document and attempt to select the one the user is using, however I cannot figure out how to grab the current filename in c# using VTSO

CodePudding user response:

Try this example:

using System;
using Excel = Microsoft.Office.Interop.Excel;

namespace Foo
{
    class Program
    {
        static void Main(string[] args)
        {
            var excelApplication = (Excel.Application)CustomMarshal.GetActiveObject("Excel.Application");
            var currentWorkbook = excelApplication.ActiveWorkbook;
            Console.WriteLine("Current active Excel Workbook: "   currentWorkbook.Name); // or FullName to get full path to opened file
            Console.ReadKey();
        }
    }
}

CustomMarshal and GetActiveObject() explanations can be found here: No definition found for GetActiveObject from System.Runtime.InteropServices.Marshal C#

  • Related