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


Rajkiran SwainPosted May 2, 2023, 8:11 AM
The error you're encountering suggests that the document you're trying to manipulate is already open in a separate program, such as SharePoint or another instance of Word. This can cause conflicts and errors with the `Microsoft.Office.Interop.Word` library.
To avoid this issue, you should make sure that the document is not open in any other programs before attempting to manipulate it. You could also try explicitly setting the `ReadOnly` parameter of the `Documents.Open` method to `false`, to ensure that the document is not opened in read-only mode.
Additionally, it's worth noting that using `Microsoft.Office.Interop.Word` can be resource-intensive and may not be the most efficient or reliable way to manipulate Word documents. Depending on your requirements, it may be worth considering alternative libraries or approaches for working with Word documents, such as OpenXML or third-party libraries like Aspose.Words.