Link to home
Start Free TrialLog in
Avatar of RGuillermo
RGuillermoFlag for United States of America

asked on

Unhandled Exception: System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800AC472'

Hello experts,
I have created a very small program in visual basic 2017 less than a hundred lines...
its works fine but when reaching the line END the program shows the following error message

System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800AC472'

have tried it all... how can I solve this issue?
Regards,

Here is the program:

Imports Microsoft.Office.Core
Imports Microsoft.VisualBasic
Imports Excel = Microsoft.Office.Interop.Excel


Public Class Form1
    Dim ss_tmp_fdr As String

    Dim dt As Date, dts As String, W_PATH As String, W_FILENAME As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fn As String, ss_tmp_fdr As String
        Dim xlSheet As excel.Worksheet
        'My.Computer.FileSystem
        'My.Computer.FileSystem.
        dt = Date.Now
        dts = Mid(Str(10000 + dt.Year), 3, 4) & "-" & Mid(Str(100 + dt.Month), 3, 2) & "-" & Mid(Str(100 + dt.Day), 3, 2) & "_" & _
             Mid(Str(100 + dt.Hour), 3, 2) & "-" & Mid(Str(100 + dt.Minute), 3, 2) & "-" & Mid(Str(100 + dt.Second), 3, 2)

        ss_tmp_fdr = f_ss_tmp_fdr()
        read_open_xls(ss_tmp_fdr)
        fn = W_PATH & W_FILENAME
        Dim XL As excel.Application
        XL = New excel.Application                                         'Set to new instance of excel
        XL.Visible = True
        XL.WindowState = excel.XlWindowState.xlMaximized
        Dim w2 As excel.Workbook = XL.Workbooks.Open(fn)         'open existing workbook
        xlSheet = w2.Worksheets(1)
        xlSheet.Cells.EntireColumn.AutoFit()

        End   <<<<<<< ======= Error happens here. Always after doing it all correctly.

    End Sub

    Private Function read_open_xls(ss_tmp_fdr)
        Dim fileReader As System.IO.StreamReader

        fileReader = My.Computer.FileSystem.OpenTextFileReader(ss_tmp_fdr & "XLS_FILENAME.TXT")

        W_PATH = Trim(fileReader.ReadLine())
        W_FILENAME = Trim(fileReader.ReadLine())
        'W_FILENAME = Path.GetFileNameWithoutExtension(W_FILENAME)
        fileReader.Close()
        Return True
    End Function

    Private Function f_ss_tmp_fdr()
        Dim fileReader As System.IO.StreamReader
        Dim stringreader As String

        fileReader = My.Computer.FileSystem.OpenTextFileReader("c:\TMP\ss_tmp_fdr.txt")

        stringreader = Trim(fileReader.ReadLine())

        fileReader.Close()
        Return stringreader
    End Function

End Class
Avatar of Norie
Norie

Why do you have End in the code?

As far as I know that will end all code execution, which seems to be a little strange to be doing in the Load event of a form.:)
I agree with Norie - why do you have an End statement in the Load event of a form? That seems counter productive.

If you want to stop your application, just close the form instead of using End.
I recommend you use GemBox SpreadSheet (the free edition for this). It is easier than Interop etc
https://www.gemboxsoftware.com/spreadsheet
ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RGuillermo

ASKER

Thank you, Experts,
Sorry for the delay was traveling for work.