RGuillermo
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.InteropServ ices.COMEx ception: '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.E xcel
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.xlMaxi mized
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.Ope nTextFileR eader(ss_t mp_fdr & "XLS_FILENAME.TXT")
W_PATH = Trim(fileReader.ReadLine() )
W_FILENAME = Trim(fileReader.ReadLine() )
'W_FILENAME = Path.GetFileNameWithoutExt ension(W_F ILENAME)
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.Ope nTextFileR eader("c:\ TMP\ss_tmp _fdr.txt")
stringreader = Trim(fileReader.ReadLine() )
fileReader.Close()
Return stringreader
End Function
End Class
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.InteropServ
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.E
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.xlMaxi
Dim w2 As excel.Workbook = XL.Workbooks.Open(fn) 'open existing workbook
xlSheet = w2.Worksheets(1)
xlSheet.Cells.EntireColumn
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.Ope
W_PATH = Trim(fileReader.ReadLine()
W_FILENAME = Trim(fileReader.ReadLine()
'W_FILENAME = Path.GetFileNameWithoutExt
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.Ope
stringreader = Trim(fileReader.ReadLine()
fileReader.Close()
Return stringreader
End Function
End Class
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.
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
https://www.gemboxsoftware.com/spreadsheet
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you, Experts,
Sorry for the delay was traveling for work.
Sorry for the delay was traveling for work.
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.:)