在 vb.net 中打开和保存 Word 文档 - 可见性关闭时出错
我正在打开一个word文档并将其另存为html,代码如下。如果我设置 objWord.Visible = False ,我会收到错误: Exception from 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
I am opening a word document and saving it as html with the following code. If i set the objWord.Visible = False , i get an error: Exception from HRESULT: 0x800A1098 , which i believe indicates there is nothing to open.
If i set objWord.Visible = True, the code runs properly, but i dont want the end user to see word in action. Would appreciate any help.
thanks in advance
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试
一般情况下,我从来不知道当您以这种方式创建单词自动化对象时它们是可见的,除非已经加载了Word。
另外,您似乎将 ISVISIBLE 参数传递给 DOCUMENTS.OPEN 函数,但我无法判断它的值是什么。如果为 FALSE,则文档应加载到隐藏窗口中,即使 Word 已打开,用户也看不到该窗口。
You might try
Generally, I've never known word automation objects to be visible when you create them this way UNLESS Word was already loaded.
Also, you appear to be passing an ISVISIBLE argument to the DOCUMENTS.OPEN function, but I can't tell what it's value is. If it's FALSE, the document should be loaded in a hidden window the user can't see even if Word was already open.