Visual Studio Windows Forms C# Interop.Excel - Excel Class Object

Visual Studio Windows Forms C# Interop.Excel - Excel Class Object

I have created a C# Windows Form project in Visual Studio and I am trying to work with an Excel workbook via interop.excel. I have created a custom "excel class" and created an object of it in my Form1. What I am struggling with is whether it is possible to open an excel workbook via a button press, i.e. create the class object from a button press, and then be able to use that object in other button presses. Two versions of code are shown below. One works. One does not. The one that works just opens the Excel workbook when the program is launched. The other attempts to use a button press on the form to open the workbook after the program is launched. In the code that does not work, the object "does not exist in the current context". Any help on how to make the button press code work is most appreciated!

I have created a C# Windows Form project in Visual Studio and I am trying to work with an Excel workbook via interop.excel. I have created a custom "excel class" and created an object of it in my Form1. What I am struggling with is whether it is possible to open an excel workbook via a button press, i.e. create the class object from a button press, and then be able to use that object in other button presses. Two versions of code are shown below. One works. One does not. The one that works just opens the Excel workbook when the program is launched. The other attempts to use a button press on the form to open the workbook after the program is launched. In the code that does not work, the object "does not exist in the current context". Any help on how to make the button press code work is most appreciated!

This code works:

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

ExcelClass ex  = new ExcelClass(@"C:\path\TestBook.xlsx", 1);

private void ReadCell_Click(object sender, EventArgs e)
{
    ex.ReadCell();
}

...

This code does not:

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

public void OpenFile()
{
    ExcelClass ex  = new ExcelClass(@"C:\path\TestBook.xlsx", 1);
}

private void OpenWorkbook_Click(object sender, EventArgs e)
{
  OpenFile();
}

private void ReadCell_Click(object sender, EventArgs e)
{
    ex.ReadCell(); // "ex" does not exist in the current context 
}


c-sharp excel visual-studio

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Level-up your C++ productivity using Visual Studio

Come learn how you can improve your efficiency even while working from home. We’ll take a look at online development environments, Live Share, and a host of other new productivity features and tips.

Install Visual Studio 2019 Community – Best IDE for C#

Here is a straightforward tutorial on how to install Visual Studio 2019 Community onto Windows 10. Visual Studio 2019 is probably the best IDE ever for C# devs.

Modern C++ development with Visual Studio

Join us for a demo packed session on the latest improvements in Visual Studio 2019, including new C++20 features, cross-platform CMake integration, and support for Visual Studio Codespaces.

C++ Development with Visual Studio Code

If you’re looking for a fast and lightweight open-source code editor, Visual Studio Code has you covered.

Pixel Color Bot in C#, Visual Studio, .NET and WPF

Create a Pixel Color Bot in C# Visual Studio by using the .NET framework and WPF. This C# tutorial guides you through the development process of creating a pixel bot.