Hi,
As I am using below code to insert doc file into a parent doc.
Document document1 = new Document();
DocumentBuilder builder = new DocumentBuilder(document1);
string DemoDoc = "E:\\AnnexDocReport_IssueFile.docx";
Document srcDoc = new Document();
srcDoc = new Document(DemoDoc);
builder.InsertDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
Document saveDocFile = document1.Range.ToDocument();
saveDocFile.Save("E:\\PDfConversionFile\\DemoFile", SaveFormat.Docm);
but after save doc and open the file below error is coming.
Below file is also attached which is inserting in main doc
AnnexDocReport_IssueFile.docx (25.3 KB)
So, please suggest how can resolve error which is coming after open the file.
@RiteshAML10
It seems you are encountering an issue when trying to insert a document into a parent document using Aspose.Words. The code you provided looks mostly correct, but there are a few things to consider that might help resolve the error you are experiencing after saving and opening the document.
-
Ensure Document Structure: When you insert a document, make sure that the source document (srcDoc
) has a valid structure. A valid Word document must contain at least one section. If the source document is empty or improperly formatted, it may lead to issues when saving.
-
Use the Correct Import Method: Instead of using document1.Range.ToDocument()
, you can directly save document1
after inserting the document. The InsertDocument
method should handle the merging of the documents correctly. Here’s a revised version of your code:
Document document1 = new Document();
DocumentBuilder builder = new DocumentBuilder(document1);
string demoDocPath = "E:\\AnnexDocReport_IssueFile.docx";
Document srcDoc = new Document(demoDocPath);
builder.InsertDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
document1.Save("E:\\PDfConversionFile\\DemoFile.docm", SaveFormat.Docm);
-
Check for Errors: If you are still encountering errors after making these adjustments, ensure that you check the specific error message you receive when opening the document. This can provide clues about what might be going wrong.
-
Document Compatibility: Ensure that the version of Aspose.Words you are using is compatible with the document formats you are working with. Sometimes, issues arise from using features not supported in certain document formats.
If you continue to face issues, consider sharing the specific error message you receive when opening the saved document, as this can help in diagnosing the problem further.
Feel free to reach out if you have more questions or need further assistance!
thanks @Professionalize.Discourse for solution.
But using same code as you have provided we are facing same error. As provided below
@RiteshAML10 I cannot reproduce the problem on my side. If save the document with correct extension:
saveDocFile.Save(@"C:\Temp\out.docm", SaveFormat.Docm);
The problem only occurs when document extension is incorrect:
saveDocFile.Save(@"C:\Temp\out.docx", SaveFormat.Docm);
So, please make sure the file is saved with correct extension.