Help me the same problem!

I used aspose.word to search and repalce a docx file Where I save the new file and open the file I got the message “The Office Open XML file test.docx cannot opened because there are problems with the contents” Here is my function

private void SearchAndReplace()
{
    string filePath = @"C:\tmp\EVN.docx";
    FileStream fileStream = File.OpenRead(filePath);
    // create new MemoryStream object MemoryStream memStream = new MemoryStream();
    memStream.SetLength(fileStream.Length);
    // read file to MemoryStream fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
    // Document doc = new Document(filePath);
    Document doc = new Document(memStream);
    NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
    bool DocumentNO = false;
    bool DocumentDate = false;
    bool DocumentNoAndDate = false;
    string SoKyHieu = "Số: xxx/yyy";
    string NgayVanBan = String.Format("Hà Nội, ngày {0} tháng {1} năm {2}", DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year);
    int i = 0;
    foreach (Paragraph para in paragraphs)
    {
        if (i > 15) return;
        else
        {
            i++;
            string content = para.Range.Text;
            //1. Trường hợp: Số, ngày, tháng, năm cùng thuộc một paragraph if (!DocumentNoAndDate){int idxOfNo = content.IndexOf("số", StringComparison.OrdinalIgnoreCase);
            int idxOfDay = content.IndexOf("ngày", StringComparison.OrdinalIgnoreCase);
            int idxOfMonth = content.IndexOf("tháng", StringComparison.OrdinalIgnoreCase);
            int idxOfYear = content.IndexOf("năm", StringComparison.OrdinalIgnoreCase);
            if ((idxOfNo + idxOfDay + idxOfMonth + idxOfYear) > 0)
            {
                string mySpace = "";
                //Khoảng trống giữa SÔ và Ngày tháng năm for (int j = 0; i < 100; j++)
                mySpace += " ";
                string rpString = String.Format("Số: {0}{1}Hà Nội, ngày {2} tháng {3} năm {4}", SoKyHieu, mySpace, DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year);
                doc.Save(memStream, SaveFormat.Docx);
            }
        }
        //2. Trường hợp: Số và ngày tháng năn nằm khác paragraph if (!DocumentNO){if (content.Contains("Số") || content.Contains("số")){DocumentNoAndDate = true;
        DocumentNO = true;
        string temp = para.Range.Text.Substring(0, para.Range.Text.Length - 1);
        para.Range.Replace(new Regex(temp), SoKyHieu);
        DocumentNO = true;
    }
}
if (!DocumentDate)
{
    int idxOfDay = content.IndexOf("ngày", StringComparison.OrdinalIgnoreCase);
    int idxOfMonth = content.IndexOf("tháng", StringComparison.OrdinalIgnoreCase);
    int idxOfYear = content.IndexOf("năm", StringComparison.OrdinalIgnoreCase);
    if (idxOfDay * idxOfMonth * idxOfYear > 0)
    {
        string temp = para.Range.Text.Substring(0, para.Range.Text.Length - 1);
        para.Range.Replace(new Regex(temp), NgayVanBan);
        DocumentDate = true;
    }
}
if (DocumentNO && DocumentDate)
{
    doc.Save(memStream, SaveFormat.Docx);
    FileStream outStream = File.OpenWrite(@"C:\tmp\test1.docx");
    memStream.WriteTo(outStream);
    outStream.Flush();
    outStream.Close();
}

Help me! Thanks! This message was posted using Email2Forum by Alexey Noskov.

Hi Nguyen,

Thanks for your inquiry. Did you manage to fix the issue on your side or are you still in need of assistance?

Best regards,

I had solved my problem! https://forum.aspose.com/t/56417Thanks!