Trying to use a template and create tags by using following code. The document created is not opening. Gives prompt like Word experienced an error trying to open the file. Try some suggestions. Although it works for other templates.
Dim Doc As New Aspose.Words.Document
Dim builder As New Aspose.Words.DocumentBuilder(Doc)
Dim ps As Aspose.Words.PageSetup = builder.PageSetup
ps.PaperSize = Aspose.Words.PaperSize.A4
ps.TopMargin = ConvertUtil.InchToPoint(0.5)
ps.BottomMargin = ConvertUtil.InchToPoint(0.2)
ps.LeftMargin = ConvertUtil.InchToPoint(0.5)
ps.RightMargin = ConvertUtil.InchToPoint(0.5)
''ps.HeaderDistance = ConvertUtil.InchToPoint(0.2)
''ps.FooterDistance = ConvertUtil.InchToPoint(0.2)
For Each Member In MemberList
Dim TraineeName As String
If String.IsNullOrEmpty(Member.ArabicName) Then
TraineeName = Member.EnglishName
Else
TraineeName = Member.ArabicName
End If
Dim TempDoc As New Aspose.Words.Document(New IO.MemoryStream(Template))
TempDoc.Range.Replace("[TraineeName]", Aljazeera.ITMS.Globals.GetFirstLastName(TraineeName), True, False)
TempDoc.Range.Replace("[CourseName]", CourseName, True, False)
TempDoc.Range.Replace("[CourseDate]", CourseDate, True, False)
Dim Section As Aspose.Words.Section = TempDoc.ChildNodes(0).Document.ChildNodes(0)
Dim SectionBody As Aspose.Words.Body = Section.ChildNodes(0)
For Each Item In SectionBody.ChildNodes
Dim TempNode = Doc.ImportNode(Item, True)
Doc.LastSection.Body.InsertAfter(TempNode, Doc.LastSection.Body.LastChild)
builder.MoveToDocumentEnd()
'Doc.AppendChild(TempNode)
Next
Next
Dim Temp As New System.IO.MemoryStream
Doc.Save(Temp, Aspose.Words.SaveFormat.Docx)
@Arsalleo
It seems that you are encountering an issue where the Word document created from a template is not opening correctly, resulting in an error message. This can happen for several reasons, especially when working with templates and document manipulation.
Here are a few suggestions to troubleshoot and potentially resolve the issue:
-
Check the Template: Ensure that the template you are using is not corrupted. You mentioned that it works for other templates, so it might be worth comparing the problematic template with a working one to identify any discrepancies.
-
Document Structure: When importing nodes from the template document to the new document, ensure that the structure of the nodes is preserved. If the template has specific styles or formatting that are not being carried over correctly, it could lead to issues when opening the document.
-
MemoryStream Usage: You are using a MemoryStream
to load the template. Make sure that the Template
variable contains valid data and that the stream is properly initialized. If the stream is empty or improperly formatted, it could cause the document to fail to open.
-
Saving the Document: When saving the document, ensure that you are using the correct format. You are currently saving it as Docx
, which is appropriate, but double-check that the Doc
object is correctly populated before saving.
-
Error Handling: Implement error handling to catch any exceptions that may occur during the document creation process. This can help you identify where the issue might be occurring.
Here is a simplified version of your code with some additional checks:
Dim Doc As New Aspose.Words.Document()
Dim builder As New Aspose.Words.DocumentBuilder(Doc)
Dim ps As Aspose.Words.PageSetup = builder.PageSetup
ps.PaperSize = Aspose.Words.PaperSize.A4
ps.TopMargin = ConvertUtil.InchToPoint(0.5)
ps.BottomMargin = ConvertUtil.InchToPoint(0.2)
ps.LeftMargin = ConvertUtil.InchToPoint(0.5)
ps.RightMargin = ConvertUtil.InchToPoint(0.5)
For Each Member In MemberList
Dim TraineeName As String = If(String.IsNullOrEmpty(Member.ArabicName), Member.EnglishName, Member.ArabicName)
Dim TempDoc As New Aspose.Words.Document(New IO.MemoryStream(Template))
TempDoc.Range.Replace("[TraineeName]", Aljazeera.ITMS.Globals.GetFirstLastName(TraineeName), True, False)
TempDoc.Range.Replace("[CourseName]", CourseName, True, False)
TempDoc.Range.Replace("[CourseDate]", CourseDate, True, False)
Dim Section As Aspose.Words.Section = TempDoc.FirstSection
Dim SectionBody As Aspose.Words.Body = Section.Body
For Each Item In SectionBody.ChildNodes
Dim TempNode = Doc.ImportNode(Item, True)
Doc.LastSection.Body.InsertAfter(TempNode, Doc.LastSection.Body.LastChild)
Next
Next
Dim Temp As New System.IO.MemoryStream()
Doc.Save(Temp, Aspose.Words.SaveFormat.Docx)
Make sure to test the above code and see if it resolves the issue. If the problem persists, consider checking the Aspose documentation or forums for similar issues that might provide additional insights.
If you continue to experience difficulties, please provide more details about the template and any specific error messages you receive, as this will help in diagnosing the problem further.