VS2010编写VB出现在 System.Runtime.InteropServices.COMException 中第一次偶然出现的“*.exe”类型

rapsilen 2016-04-05 04:06:19
我的环境:
Microsoft Visual Studio 2010
版本 10.0.30319.1 RTMRel
Microsoft .NET Framework
版本 4.6.01038 RTMRel
===================================分割线=============
部分源码:
Imports Microsoft.Office.Interop.Excel
Imports System.Text.RegularExpressions

Module HT_FT_Loader

Sub Main()
Call SearchFile()
'Call LoadFile(xmlFile:="Y:\TmpFolder\ST1_HWC03076_OSG60R1K8DFA_FT_FA5Z-3563_201603040317_P_002_20160310_044545.xls")
’同样的sub,在Main中单独调用功能正常
‘debug 打印如下:
’Y:\TmpFolder\ST1_HWC03076_OSG60R1K8DFA_FT_FA5Z-3563_201603040317_P_002_20160310_044545.xls length of arg is 90
’Alerts Init
‘Visible Init
’Open Completed!
‘Sheets Test Item Completed!
’Init Completed!
‘Col header load completed!!
’Upload Lot Info Complete
‘ 20 Fail Item Location
’Paremeter Info Complete

End Sub

Sub SearchFile()
SubFolder = "d:\Users\XXX\Documents\VBA\Try\"
Dim FileName = Dir(SubFolder)
Do While InStr(FileName, "rar") > 0
Debug.Print("Y:\TmpFolder\" & Replace(FileName, ".rar", ".xls") & " , in search: length of arg is " & _
Str(Len("Y:\TmpFolder\" & Replace(FileName, ".rar", ".xls"))))
Call LoadFile(xmlFile:="Y:\TmpFolder\ST1_HWC03076_OSG60R1K8DFA_FT_FA5Z-3563_201603040317_P_002_20160310_044545.xls")
‘我的问题:在循环中调用LoadFile为何出错?看起来是“ wbXL = appXL.Workbooks.Open(xmlFile)”有问题,那么正确的写法是什么?
‘debug打印值如下:
‘Y:\TmpFolder\ST1_HWC03076_OSG60R1K8DFA_FT_FA5Z-3563_201603040317_P_002_20160310_044545.xls , in search: length of arg is 90
’Y:\TmpFolder\ST1_HWC03076_OSG60R1K8DFA_FT_FA5Z-3563_201603040317_P_002_20160310_044545.xls length of arg is 90
‘Alerts Init
’Visible Init
‘在 System.Runtime.InteropServices.COMException 中第一次偶然出现的“XX.exe”类型的异常
’Exception Found: Y:\TmpFolder\ST1_HWC03076_OSG60R1K8DFA_FT_FA5Z-3563_201603040317_P_002_20160310_044545.xls would be closed!!

FileName = Dir()
Loop
End Sub
'读取文件
Sub LoadFile(ByVal xmlFile As String)
'读取Excel必须
Dim appXl As New Application
Dim wbXL As Workbook
Dim shXL As Worksheet

Try
appXL.DisplayAlerts = False
Debug.Print("Alerts Init")
appXL.Visible = False
Debug.Print("Visible Init")
wbXL = appXL.Workbooks.Open(xmlFile)
Debug.Print("Open Completed!")
shXL = wbXL.Sheets("Test Item")
Debug.Print("Sheets Test Item Completed!")
'Lot Info区域获取开始
'Lot Info 区域行数
Debug.Print("Init Completed!")
‘省略其余代码
Catch ex As Exception
Debug.Print("Exception Found: " + xmlFile + " would be closed!!")
appXL.Quit()
appXL = Nothing

End Try

本人新手接触VB.net, 希望各位大大多多指教,谢谢
...全文
618 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
rapsilen 2016-04-06
  • 打赏
  • 举报
回复
引用 10 楼 qbilbo 的回复:
你的意思是文件后缀名是:xls,但内容是xml? 如果是这样的话,可以用System.Xml.XmlDocument来读取。如果文件格式固定,还可以用System.Xml.Serialization.XmlSerializer的反序列化的方法来获取数据
是的,xml格式的xls文件,我在试xmldoc了,学习ing,谢谢大神
qbilbo 2016-04-06
  • 打赏
  • 举报
回复
你的意思是文件后缀名是:xls,但内容是xml? 如果是这样的话,可以用System.Xml.XmlDocument来读取。如果文件格式固定,还可以用System.Xml.Serialization.XmlSerializer的反序列化的方法来获取数据
rapsilen 2016-04-06
  • 打赏
  • 举报
回复
引用 7 楼 qbilbo 的回复:
Sub LoadFile(ByVal xmlFile As String)
        '读取Excel必须
        Dim appXl As New Application
        Dim books As Workbooks = appXl.Workbooks
        Dim wbXL As Workbook
        Dim shXL As Worksheet

        Try
            appXl.DisplayAlerts = False
            Debug.Print("Alerts Init")
            appXl.Visible = False
            Debug.Print("Visible Init")
            wbXL = books.Open(xmlFile)
            Debug.Print("Open Completed!")
            shXL = wbXL.Sheets("Test Item")
            Debug.Print("Sheets Test Item Completed!")
            'Lot Info区域获取开始
            'Lot Info 区域行数
            Debug.Print("Init Completed!")
            '省略其余代码
        Catch ex As Exception
            Debug.Print("Exception Found: " + xmlFile + " would be closed!!")
            Debug.Print(ex.Message + vbNewLine + ex.StackTrace)
        Finally
            If shXL IsNot Nothing Then
                System.Runtime.InteropServices.Marshal.ReleaseComObject(shXL)
            End If
            If wbXL IsNot Nothing Then
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wbXL)
            End If
            books.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(books)
            appXl.Quit()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(appXl)
        End Try
End Sub
这样看看报不报错,如果报错的话看下message和stacktrace。
这位大大,非常感谢指点,你的代码比我的好多了,又get到技能点了 现在打印结果是: 在 System.Runtime.InteropServices.COMException 中第一次偶然出现的“HT_FT.exe”类型的异常 Exception Found: Y:\TmpFolder\ST1_HWC03076_OSG60R1K8DFA_FT_FA5Z-3563_201603040317_P_002_20160310_044545.xls would be closed!! 异常来自 HRESULT:0x800A03EC 在 Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad) 在 HT_FT.HT_FT_Loader.LoadFile(String xmlFile) 位置 \Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\HT_FT_Loader.vb:行号 73 貌似还是Open的问题,因为是敏感的测试数据不能上传附件;但我大概比较了下,这些都是xml格式的文件,打不开报错的都是源文件大小超过30M的。size小的处理都没有问题。不知道用。net处理xml有没有什么好的办法?需要用第三方插件?我用openXML试过,打开需要好久好久
qbilbo 2016-04-06
  • 打赏
  • 举报
回复
Try块内的代码如果出错会进入Catch块,不管有没有错,最后都会进入Finally块,一般释放资源之类的代码都会放在Finally块内。
rapsilen 2016-04-06
  • 打赏
  • 举报
回复
引用 8 楼 qbilbo 的回复:
我总觉得是com对象没释放干净引起的,代码中的books变量不要用appXl.Workbooks代替。
大神,确实是没释放干净,你给的code我加在前面搞定了,另外加了个sleep,放在finally里面有时appXL.quit会出错。另请教finally到底有啥作用?
            '关闭Excel应用,重要!!内存释放
            wbXL = Nothing
            shXL = Nothing
            System.Runtime.InteropServices.Marshal.ReleaseComObject(books)
            appXl.Quit()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(appXl)
            System.Threading.Thread.Sleep(1000)

        Catch ex As Exception
            Debug.Print("Exception Found: " + xmlFile + " would be closed!!")
            Debug.Print(ex.Message + vbNewLine + ex.StackTrace)

        End Try
qbilbo 2016-04-05
  • 打赏
  • 举报
回复
我总觉得是com对象没释放干净引起的,代码中的books变量不要用appXl.Workbooks代替。
qbilbo 2016-04-05
  • 打赏
  • 举报
回复
Sub LoadFile(ByVal xmlFile As String)
        '读取Excel必须
        Dim appXl As New Application
        Dim books As Workbooks = appXl.Workbooks
        Dim wbXL As Workbook
        Dim shXL As Worksheet

        Try
            appXl.DisplayAlerts = False
            Debug.Print("Alerts Init")
            appXl.Visible = False
            Debug.Print("Visible Init")
            wbXL = books.Open(xmlFile)
            Debug.Print("Open Completed!")
            shXL = wbXL.Sheets("Test Item")
            Debug.Print("Sheets Test Item Completed!")
            'Lot Info区域获取开始
            'Lot Info 区域行数
            Debug.Print("Init Completed!")
            '省略其余代码
        Catch ex As Exception
            Debug.Print("Exception Found: " + xmlFile + " would be closed!!")
            Debug.Print(ex.Message + vbNewLine + ex.StackTrace)
        Finally
            If shXL IsNot Nothing Then
                System.Runtime.InteropServices.Marshal.ReleaseComObject(shXL)
            End If
            If wbXL IsNot Nothing Then
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wbXL)
            End If
            books.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(books)
            appXl.Quit()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(appXl)
        End Try
End Sub
这样看看报不报错,如果报错的话看下message和stacktrace。
rapsilen 2016-04-05
  • 打赏
  • 举报
回复
引用 5 楼 qbilbo 的回复:
Try
    ....
Catch ex As Exception
     Debug.Print("Exception Found: " + xmlFile + " would be closed!!")
Finally
    appXL.Quit()
End Try
前面没仔细看代码,这样试试。
谢谢,还是有之前相同的exception
qbilbo 2016-04-05
  • 打赏
  • 举报
回复
Try
    ....
Catch ex As Exception
     Debug.Print("Exception Found: " + xmlFile + " would be closed!!")
Finally
    appXL.Quit()
End Try
前面没仔细看代码,这样试试。
rapsilen 2016-04-05
  • 打赏
  • 举报
回复
补充一下,不是每个文件都会出错。好像只有处理30M以上文件时才会有问题。再请教下用。net打开xml格式的文件是否要对大文件做特殊处理?
rapsilen 2016-04-05
  • 打赏
  • 举报
回复
引用 2 楼 qbilbo 的回复:
把:Catch ex As Exctpion 改成: Finally
这位大大,finally是不是说如果没有exctpion也会执行的code?试了下,在main中调用LoadFile也会出错了。 要不您再想想~
qbilbo 2016-04-05
  • 打赏
  • 举报
回复
把:Catch ex As Exctpion 改成: Finally
rapsilen 2016-04-05
  • 打赏
  • 举报
回复
自己顶起~

16,721

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧