Question Open Excel File for editing.

Status
Not open for further replies.

Rick_Stanich

New member
Joined
Sep 19, 2025
Messages
2
Programming Experience
1-3
I have a userform with a Button, I am trying to open an existing excel file for editing. I keep getting this error (See image).
Any help is appreciated.

Form1.vb
VB.NET:
Expand Collapse Copy
Imports System.IO

Imports Microsoft.Office.Interop.Excel

Imports Excel = Microsoft.Office.Interop.Exce

Code:
VB.NET:
Expand Collapse Copy
 Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
     'MsgBox("Load Data") 'for testing
     MsgBox(TextBox1.Text) 'for testing
     '"V:\CMM Data Files\Excel Files\Shoe GDT\#MSA-Masters\#Master-Shoe-MSA 32.xlsx")
     Dim xlApp As Excel.Application
     Dim xlWorkBook As Excel.Workbook
     Dim xlWorkSheet As Excel.Worksheet

     xlApp = New Excel.ApplicationClass
     xlWorkBook = xlApp.Workbooks.Open("V:\CMM Data Files\Excel Files\Shoe GDT\#MSA-Masters\#Master-Shoe-MSA 32.xlsx")
     xlWorkSheet = xlWorkBook.Sheets("MSA")
     xlApp.Visible = True
     'display the cells value B2
     MsgBox(xlWorkSheet.Cells(1, 15).value)
     'edit the cell with new value
     'removed
     xlWorkBook.Close()
     xlApp.Quit()

     releaseObject(xlApp)
     releaseObject(xlWorkBook)
     releaseObject(xlWorkSheet)
 End Sub
 Private Sub releaseObject(ByVal obj As Object)
     Try
         System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
         obj = Nothing
     Catch ex As Exception
         obj = Nothing
     Finally
         GC.Collect()
     End Try
 End Sub
 

Attachments

  • Screenshot 2025-09-19 072128.jpg
    Screenshot 2025-09-19 072128.jpg
    28.2 KB · Views: 1
Object variable or with block variable not set.
Seems to happen at this line.
xlWorkBook = xlApp.Workbooks.Open("V:\CMM Data Files\Excel Files\Shoe GDT\#MSA-Masters\#Master-Shoe-MSA 32.xlsx")
 
Please post in VB.Net forum instead, this place is for C# code.
 
Status
Not open for further replies.
Back
Top Bottom