Search code examples
c#console-applicationopenxml-sdk

Find words in document SP URL


I have a sample code, it works well if the document is locally, but if I point the way to the link, then immediately the error, how to win?

using DocumentFormat.OpenXml.Packaging;
using OpenXmlPowerTools;

using (WordprocessingDocument doc = WordprocessingDocument.Open(@"http://sp-test/sites/test/Documents/Base.docx", true))
TextReplacer.SearchAndReplace(wordDoc: doc, search: "Tags", replace: "Test", matchCase: false);

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in DocumentFormat.OpenXml.dll

Additional information: Could not find document


Solution

  • WordprocessingDocument.Open looks for the fileHandle or Stream but you are providing URL which doesn't make sense.

    you first need to use HttpClient to download file as Stream then process it with WordprocessingDocument.Open(stream) accordingly