以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  WebBrowser当打开过doc或docx文件后,再次打开别的文件会提示未将对象引用设置到对象的实例。附件提取失败,可能并不存在附件!  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=177538)

--  作者:cnsjroom
--  发布时间:2022/5/26 11:26:00
--  WebBrowser当打开过doc或docx文件后,再次打开别的文件会提示未将对象引用设置到对象的实例。附件提取失败,可能并不存在附件!

WebBrowser当打开过doc或docx文件后,再次打开别的文件会提示未将对象引用设置到对象的实例。附件提取失败,可能并不存在附件!

麻烦老师指导下,怎么避免这个错误呢?
依次弹出如下两个提示:

---------------------------
提示
---------------------------
未将对象引用设置到对象的实例。
---------------------------
确定  
---------------------------

---------------------------
提示
---------------------------
附件提取失败,可能并不存在附件!
---------------------------
确定  
---------------------------

确定后,后续打开的doc文件就不显示了  web控件直接是空白

如果打开doc文件 然后打开其他非doc文件之后 在打开doc文件 又偶尔不会出现上述错误提示……


代码如下:

Dim zs1 As Integer = Tables(e.form.name & "_table2").Count

If zs1 >0 Then
    Dim wb As WinForm.WebBrowser = e.Form.Controls("WebBrowser1")
    Dim tb As WinForm.TabControl = e.Form.Controls("TabControl1")
    Dim pc As WinForm.PictureBox = e.Form.Controls("PictureBox1")
   
    Dim lst As List(of String)
    lst = Tables(e.form.name & "_table2").Current.DataRow.Lines("文件名")
    \'msgbox("第一个文件为:" & lst(0))
    vars("文件名")=lst(0)
    Dim dr As DataRow = Tables(e.form.name & "_table2").Current.DataRow
    Dim fl As String =  ProjectPath & "下载文件\\" & lst(0)
    If Tables(e.form.name & "_table2").Current Is Nothing Then
        wb.Address=Nothing
        pc.Image = GetImage(Nothing)
        Return
    End If
    If FileSys.FileExists(fl) AndAlso CRCCheckFile(fl) = dr.SQLGetValue("CRC值") Then \'如果本地存在同名文件且CRC校验值相同
        \'则直接使用本地文件
    Else \'否则从数据库提取文件
        If dr.SQLLoadFile("附件",fl) = False Then \'如果提取文件失败
            Messagebox.Show("附件提取失败,可能并不存在附件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return
        End If
    End If
    Dim bbb As String =dr("文件类型")
    If bbb.Contains("bmp")  Or bbb.Contains("jpg") Or bbb.Contains("gif") Or bbb.Contains("png") Then
        wb.Address = Nothing
        pc.Image = GetImage(fl)
        tb.SelectedIndex=1
    Else  If bbb.Contains("xls") Or bbb.Contains("xlsx")  Or bbb.Contains("doc") Or  bbb.Contains("docx") Or bbb.Contains("txt") Or bbb.Contains("pdf") Then
        wb.Address = fl
        wb.OfficeToolBar = True
        pc.Image = GetImage(Nothing)
        tb.SelectedIndex=0
    End If
    vars("文件")=fl
    If Forms("全屏预览").opened Then
        Dim bt As WinForm.Button = Forms("全屏预览").Controls("Button1")
        bt.PerformClick
    End If
   
End If

[此贴子已经被作者于2022/5/26 11:30:33编辑过]

--  作者:有点蓝
--  发布时间:2022/5/26 11:57:00
--  

Dim zs1 As Integer = Tables(e.form.name & "_table2").Count

If zs1 >0 Then
    Dim wb As WinForm.WebBrowser = e.Form.Controls("WebBrowser1")

wb.Address=Nothing

    Dim tb As WinForm.TabControl = e.Form.Controls("TabControl1")

……


--  作者:cnsjroom
--  发布时间:2022/5/26 16:19:00
--  回复:(有点蓝)Dim zs1 As Integer = Tables(e.form...

wb.Address=Nothing

按照老师的指导前置后,还是会出现一楼的两个提示……


--  作者:有点蓝
--  发布时间:2022/5/26 16:33:00
--  
那就要用kill了:http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=88255&skin=0,看17楼

--  作者:cnsjroom
--  发布时间:2022/5/26 16:51:00
--  回复:(有点蓝)那就要用kill了:http://www.foxtabl...

对应的资源地址

内容如下:

web控件.Address = Nothing

GC.Collect()

 

Dim ps As  System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("excel")
For Each p As System.Diagnostics.Process In ps

    msgbox(p.MainWindowTitle)
    If p.MainWindowTitle = Nothing Then  p.kill
Next

 

Dim ps As  System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("WINWORD")
For Each p As System.Diagnostics.Process In ps

    msgbox(p.MainWindowTitle)
    If p.MainWindowTitle = Nothing Then  p.kill
Next

 

使用kill后,会杀掉所有包含winword或excel的进程,如果在web控件之外还有其他打开的word或excel文件会被一并关闭,

有没有办法实现:只关闭web控件内已经打开的word或excel文件呢?

 


--  作者:有点蓝
--  发布时间:2022/5/26 17:09:00
--  
好像浏览器里的文件是没有标题的,自己调试分析一下

msgbox(p.MainWindowTitle)

--  作者:cnsjroom
--  发布时间:2022/5/26 17:33:00
--  回复:(有点蓝)好像浏览器里的文件是没有标题的,自...

---------------------------
错误
---------------------------
编译错误:未声明“p”。它可能因其保护级别而不可访问。

 

错误代码:msgbox(p.MainWindowTitle)
---------------------------
确定  
---------------------------

 

 

 

Dim wb As WinForm.WebBrowser = e.Form.Controls("WebBrowser1")
msgbox(wb.MainWindowTitle)

---------------------------
错误
---------------------------
编译错误:“MainWindowTitle”不是“WinForm.WebBrowser”的成员。

 

错误代码:msgbox(wb.MainWindowTitle)
---------------------------
确定  
---------------------------


--  作者:有点蓝
--  发布时间:2022/5/26 17:35:00
--  
看5楼。不要闭着眼睛用