Error while try to save document and display
Server Error in '/' Application.
The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Runtime.InteropServices.COMException: The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))
Source Error:
Line 22: 'Open the word document in background.
Line 23: Dim applicationclass As _Application = New Application
Line 24: applicationclass.Documents.Open(fileSavePath)
Line 25: applicationclass.Visible = False
Line 26: Dim document As Document = applicationclass.ActiveDocument
Source File: C:\Users\Makumbi\Downloads\WordToHTML_ASP.Net\VB.aspx.vb Line: 24
Stack Trace:
[COMException (0x80010105): The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))]
Microsoft.Office.Interop.Word.Documents.Open(Object& FileName, Object& ConfirmConversions, Object& ReadOnly, Object& AddToRecentFiles, Object& PasswordDocument, Object& PasswordTemplate, Object& Revert, Object& WritePasswordDocument, Object& WritePasswordTemplate, Object& Format, Object& Encoding, Object& Visible, Object& OpenAndRepair, Object& DocumentDirection, Object& NoEncodingDialog, Object& XMLTransform) +0
_Default.Upload(Object sender, EventArgs e) in C:\Users\Makumbi\Downloads\WordToHTML_ASP.Net\VB.aspx.vb:24
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9796650
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +211
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1696
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4494.0
Imports System.IO
Imports Microsoft.Office.Interop.Word
Imports System.Text.RegularExpressions
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
Dim documentFormat As Object = 8
Dim randomName As String = DateTime.Now.Ticks.ToString
Dim htmlFilePath As Object = Server.MapPath("~/Temp/") & randomName + ".htm"
Dim directoryPath As String = Server.MapPath("~/Temp/") & randomName + "_files"
Dim fileSavePath As Object = Server.MapPath("~/Temp/") & Path.GetFileName(FileUpload1.PostedFile.FileName)
'If Directory not present, create it.
If Not Directory.Exists(Server.MapPath("~/Temp/")) Then
Directory.CreateDirectory(Server.MapPath("~/Temp/"))
End If
'Upload the word document and save to Temp folder.
FileUpload1.PostedFile.SaveAs(fileSavePath.ToString)
'Open the word document in background.
Dim applicationclass As _Application = New Application
applicationclass.Documents.Open(fileSavePath)
applicationclass.Visible = False
Dim document As Document = applicationclass.ActiveDocument
'Save the word document as HTML file.
document.SaveAs(htmlFilePath, documentFormat)
'Close the word document.
document.Close()
'Read the saved Html File.
Dim wordHTML As String = System.IO.File.ReadAllText(htmlFilePath.ToString)
'Loop and replace the Image Path.
For Each match As Match In Regex.Matches(wordHTML, "<v:imagedata.+?src=[""'](.+?)[""'].*?>", RegexOptions.IgnoreCase)
wordHTML = Regex.Replace(wordHTML, match.Groups(1).Value, ("Temp/" + match.Groups(1).Value))
Next
'Delete the Uploaded Word File.
System.IO.File.Delete(fileSavePath.ToString)
dvWord.InnerHtml = wordHTML
End Sub
End Class