Free Support Forum - aspose.com

The file cannot be opened because there are problems with the contents

I am doing a mailmerge with Aspose.Words, but when I open the output file with MS Word, I get some messages/errors:

"The file cannot be opened because there are problems with the contents. Details: The file is corrupt and cannot be opened."

I click "OK", then:

"We found unreadable content in the "file name". Do you want to recover the contents of this document?"...

I clicked "Yes", and the document opens.

Can something be done so I don't the error messages?

This is the code I am using (see last 3 lines of code):

Dim values() As Object = Nothing

Dim columnNames() As String

Dim dbConn As SqlConnection = Context.Session("DBConnection")

Dim cmd_proc As New SqlCommand("SELECT data.* FROM dbo.fn_WordMerge(1) AS data", dbConn)

cmd_proc.CommandType = Data.CommandType.Text

Dim dr As SqlDataReader = cmd_proc.ExecuteReader(Data.CommandBehavior.SingleRow)

Dim Flds As New List(Of String)

For I As Integer = 0 To dr.FieldCount - 1

Flds.Add(dr.GetName(I))

Next

columnNames = Flds.ToArray()

Dim RetValues As New ArrayList()

If dr.Read() Then

ReDim values(dr.FieldCount - 1)

dr.GetValues(values)

End If

dr.Close()

cmd_proc.Dispose()

Dim doc As New Document("C:\Temp\TestMerge6.docx")

doc.MailMerge.Execute(columnNames, values)

doc.Save(Response, "test_out.docx", ContentDisposition.Inline, Nothing)

Hi Michael,


Thanks for your inquiry. This is actually not an issue in Aspose.Words. I think, to resolve the problem with DOCX you should simply add Response.End() after sending document to client browser. This will resolve the problem. For some reasons when you save a DOCX document to client browser, content of web page is added at the end of the document, you can see this if open your DOCX document in any binary editor. That is why MS Word cannot open this document. If you add Response.End(), content of web page will not be added and document will be valid. Please let me know if this helps.

Best Regards,

Thank You, that’s what it was…