在vb.net中打开并保存word doc - 关闭可见性时出错

时间:2011-06-11 01:58:09

标签: asp.net vb.net ms-word

我打开一个word文档并使用以下代码将其保存为html。如果我设置了objWord.Visible = False,我得到一个错误:来自HRESULT的异常:0x800A1098,我相信这表明没有什么可以打开。  如果我设置objWord.Visible = True,代码运行正常,但我不希望最终用户看到行动中的单词。非常感谢任何帮助。

提前致谢

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim objWord As Microsoft.Office.Interop.Word.ApplicationClass = New ApplicationClass()
    If Not (fUpload.HasFile) Then
        lblMessage.Text = "Please choose file to upload"
    Else
        Try
            Dim strFileName As String = fUpload.FileName
            Dim strSep As String() = fUpload.FileName.Split("."c)
            Dim arrLength As Integer = strSep.Length - 1
            Dim strExt As String = strSep(arrLength).ToString().ToUpper()
            'Save the uploaded file to the folder

            strPathToUpload = Server.MapPath("Uploaded")
            strPathToConvert = Server.MapPath("WordToHtml")
            Dim FileName As Object = (strPathToUpload & "\") + fUpload.FileName

            Dim FileToSave As Object = (strPathToConvert & "\") + Left(fUpload.FileName, Len(fUpload.FileName) - 4) & ".htm"
            If strExt.ToUpper().Equals("DOC") Then
                fUpload.SaveAs((strPathToUpload & "\") + fUpload.FileName)
                lblMessage.Text = "File uploaded successfully"

                'open file in word
                objWord.Documents.Open(FileName, missing, [readOnly], missing, missing, missing, missing, missing, missing, missing, missing, isVisible)

                objWord.Visible = False 'error occures here if False

                Dim oDoc As Microsoft.Office.Interop.Word.Document = objWord.ActiveDocument

                If oDoc.Application.Version = "12.0" Then ' Word 2007 version
                    oDoc.SaveAs2(FileToSave, fltDocFormat, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
                Else ' word 2003 version
                    oDoc.SaveAs(FileToSave, fltDocFormat, missing, missing, missing, missing, missing, missing, missing, missing, missing)
                End If

            Else
                lblMessage.Text = "Invalid file selected!"
            End If

            'Close/quit word 
            objWord.Quit(missing, missing, missing)
        Catch ex As Exception
            objWord.Quit(missing, missing, missing)
            Response.Write(ex.Message)
        End Try
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

您可以尝试

Dim objWord As Microsoft.Office.Interop.Word.ApplicationClass = CreateoBject("Word.Application")

一般来说,除非已经加载了Word,否则在创建文字自动化对象时,我从未知道它们是可见的。

此外,您似乎将ISVISIBLE参数传递给DOCUMENTS.OPEN函数,但我无法分辨它的价值。如果为FALSE,则应将文档加载到用户无法看到的隐藏窗口中,即使Word已经打开。