Nimish Saxena

Nimish Saxena

  • 1.1k
  • 597
  • 82.8k

Microsoft.Office.Interop.Word.Document Related Issue

May 2 2023 7:44 AM

   public void SetContentBodyofSNote(ref string path_folder, string content, string web_root)
        {
            var orig_fileName = "SN_Info" + Guid.NewGuid().ToString() + ".docx";
            var sourceFile = path_folder;
            var destFile = System.IO.Path.Combine(web_root, "Uploads");
            destFile = Path.Combine(destFile, orig_fileName);
            System.IO.File.Copy(sourceFile, destFile, true);

            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            wordApp.Visible = true;
            // Open the document
            object fileName = destFile;
            object missing = Type.Missing;
            Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref fileName, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing);

            // Get the original text of the document with formatting
            string originalText = content;

            string key = "«contentbody»";
            Microsoft.Office.Interop.Word.Range range = doc.Range();
            range.Find.Execute(FindText: key);

            // Insert the WordOpenXML at the location of the key or placeholder
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(originalText);

            XmlNodeList nodeList = xmlDocument.GetElementsByTagName("w:p");
            if (nodeList.Count > 0)
            {
                XmlNode firstNode = nodeList[0];
                XmlNode parentNode = firstNode.ParentNode;
                parentNode.RemoveChild(firstNode);
            }
            range.InsertXML(xmlDocument.DocumentElement.OuterXml);
            doc.Close();
            wordApp.Quit();
            path_folder = destFile;
        }

when use this code i am getting error like highlight issue in document either we open doc in share point drive or open in desktop mode


Answers (1)