How can I open Microsoft Excel in Visual Basic.NET

Rajendra Deshpande 20 Reputation points
2024-06-02T16:30:14.32+00:00

I am using Visual Studio 2023 for using Visual Basic. I have latest version of Microsoft Office. I want to open Excel using Visual Basic (not VBA). Please tell me how to go about it. It is very important for me.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,532 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,632 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,652 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 27,666 Reputation points Microsoft Vendor
    2024-06-03T07:53:34.8033333+00:00

    Hi @Rajendra Deshpande ,

    Try using Microsoft.Office.Interop.Excel.

    Check the following steps and code.

    • Add a reference to the Microsoft Office Interop Excel library:
      1. Right-click on your project in the Solution Explorer and select "Add" > "Reference..."
      2. In the Reference Manager, go to "Assemblies" > "Extensions".
      3. Look for "Microsoft.Office.Interop.Excel" and check it. Then click "OK".
    Imports Excel = Microsoft.Office.Interop.Excel
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ' Create a new instance of Excel application
            Dim excelApp As New Excel.Application
    
            ' Make the Excel application visible
            excelApp.Visible = True
    
            ' Add a new workbook
            Dim workbook As Excel.Workbook = excelApp.Workbooks.Add()
    
            ' Optional: Access the first worksheet
            Dim worksheet As Excel.Worksheet = CType(workbook.Sheets(1), Excel.Worksheet)
            worksheet.Name = "MySheet"
    
            ' Optional: Write some data to the worksheet
            worksheet.Cells(1, 1).Value = "Hello, Excel!"
        End Sub
    End Class
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Rajendra Deshpande 20 Reputation points
    2024-06-03T04:48:35.4533333+00:00

    Yes. I want to start Excel and create a new blank work book using Visual Basic. I tried creating project reference in VS for Microsoft Excel Object Library and Microsoft Office Object libraries.

    I havefollowe the instructions given in

    https://learn.microsoft.com/en-us/previous-versions/office/troubleshoot/office-developer/automate-excel-from-visual-basic-net

    But It is not working.

    I

    0 comments No comments

  2. tejaswini 0 Reputation points
    2024-06-04T11:32:21.2666667+00:00

    Hello Rajendra Deshpande,

    Microsoft Excel in Visual Basic .NET is by using the Process class to start Excel as an external application. Here's how you can do it:

    Imports System.Diagnostics

    Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        Dim excelProcess As Process = New Process()
    
        ' Specify the path to Excel executable
    
        excelProcess.StartInfo.FileName = "excel.exe"
    
        ' Start Excel
    
        excelProcess.Start()
    
    End Sub
    

    End Class

    In this code:

    We import the System.Diagnostics namespace.

    Inside the button click event handler, we create an instance of the Process class.

    We specify the path to the Excel executable using StartInfo.FileName.

    We start Excel by calling the Start() method of the Process instance.

    This method directly starts Excel as an external application without accessing its object model. It's simpler but doesn't provide direct control over Excel's features like manipulating workbooks and worksheets.

    0 comments No comments