I have generated Word/docx document in C# with OpenXML. There was added/merged other one docx document with altChunk. When you open that document it looks great. But further I need to process that document with C# code. Than I saw that there remains altChunk that actually has pointer to binary serialized content of the merged document. If you do in Word - SaveAs and save the document to the new file and reflect that document (OpenXML Tools or VS extension) than you will find that Word engine did conversion of altChunk to Paragraph(s). That's great. But I can not accomplish that from C# code.
If I copy to the new file and do Save, nothing happens :
public static void SaveAgain(string masterDocumentPath)
{
string newDocumentPath = @"F:\test.docx";
using (Stream original = new FileStream(masterDocumentPath, FileMode.Open))
{
using (FileStream fs = File.Create(newDocumentPath))
{
original.CopyTo(fs);
}
}
using (WordprocessingDocument myDocNew =
WordprocessingDocument.Open(newDocumentPath, true))
{
myDocNew.MainDocumentPart.Document.Save();
}
}
I have used this post (How to Use altChunk for Document Assembly) when building my code. What I am missing ?
UPDATE
I have succeed to do this with Interop. Here is the snippet :
public static string SaveAsWithInterop(string masterDocumentPath)
{
string directoryPath = Path.GetDirectoryName(masterDocumentPath);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(masterDocumentPath);
string fileExtension = Path.GetExtension(masterDocumentPath);
string filenNameToSave = string.Concat(fileNameWithoutExtension, "Saved", fileExtension);
object savedFilePath = Path.Combine(directoryPath, filenNameToSave);
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
object missing = Missing.Value;
object isReadOnly = false;
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(masterDocumentPath);
doc.SaveAs(ref savedFilePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref isReadOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
doc.Close();
return savedFilePath.ToString();
}
I am still wondering, is it possible to do this with OpenXML. I would like to avoid Interop.