Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

VSTO fail to write the excel cell in vb.net

I am creating a Word 2013-2016 VSTO Add-in application using vb.net in Visual Studio 2017 Community. The function of this application is to write "True" or "False" to the Excel cells based on the user's selection. The Excel file is Embedded Resource and is accessed by the following code:

Module Module1
Public exeDir As New IO.FileInfo(Reflection.Assembly.GetExecutingAssembly.FullName)
    Public xlPath = IO.Path.Combine(exeDir.DirectoryName, "Database.xlsx")
    Public xlApp As Microsoft.Office.Interop.Excel.Application = Nothing
    Public xlBook As Microsoft.Office.Interop.Excel.Workbook
    Public xlSheet1 As Microsoft.Office.Interop.Excel.Worksheet
    Public xlSheet2 As Microsoft.Office.Interop.Excel.Worksheet 
End Module

Public Class Heater
Sub Setup_Htr_DB()
        'Output2
        xlSheet1.Range("C136").Value2 = Output2.Checked.ToString 'somehow cell won't change 
        xlSheet1.Range("C137").Value2 = Output2.Checked.ToString 'somehow cell won't change
        xlSheet2.Range("C36").Value2 = Output2.Checked.ToString
        'Output3
        xlSheet1.Range("C136").Value = Output3.Checked.ToString
        xlSheet1.Range("C137").Value = Output3.Checked.ToString
        xlSheet2.Range("C37").Value = Output3.Checked.ToString
End Sub
Private Sub PB_Preview_Click(sender As Object, e As EventArgs) Handles PB_Preview.Click
        xlApp = New Microsoft.Office.Interop.Excel.Application With {
            .DisplayAlerts = False,
            .Visible = False
                    }
        xlBook = xlApp.Workbooks.Open(xlPath)
        xlSheet1 = xlBook.Sheets(1)
        xlSheet2 = xlBook.Sheets(2)
    Setup_Htr_DB()
End Sub
End Class

Somehow, two cells in xlSheet1 cannot be written by some code while they can written by some other code.

Output 2 and Output 3 are two radio buttons in one panel, which is a container control. When Output2 is checked, xlsheet2 C36 is "True".However, xlsheet1 C136 and C137 is still "False" which is the initial value.

When Output3 is checked, all three cells are "True".

Comments