Look at the end of this post for an addition to that problem with textboxes!
With this method I want to open a document, replace some text and then leave it alone. It works, thats something to be proud of. :D
public static void replaceInOpenXMLDocument(string pfad, string zuErsetzen, string neuerString)
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(pfad, true))
{
var res = from bm in doc.MainDocumentPart.Document.Body.Descendants()
where bm.InnerText != string.Empty && bm.InnerText.Contains(zuErsetzen) && bm.HasChildren == false
select bm;
foreach (var item in res)
{
item.InsertAfterSelf(new Text(item.InnerText.Replace(zuErsetzen, neuerString)));
item.Remove();
}
doc.Close();
}
}
But it only works on replacing without special characters. For example:
OS will be replaced with Windows over 9000
[OS] will be left as it is.
CASE 1:
In the document:
You use os for whatever purpose you've got.
replaceInOpenXMLDocument("C:\NSA\suspects.docx", "os", "Win 2000");
Will result in this:
You use Win 2000 for whatever purpose you've got.
CASE 2:
With special chars ...
You use [os] for whatever purpose you've got.
replaceInOpenXMLDocument("C:\NSA\suspects.docx", "[os]", "Win 2000");
... it just ignores me:
You use [os] for whatever purpose you've got.
I tried several special characters ()[]{} etc., but they're never replaced.
Is there something I forgot to do? Or is it simply not able to replace with special characters with this method? If so, I just need a simple workaround.
Is there anybody out to help with my desperation? :)
SOLUTION / ADDITION 1:
Thanks to Flowerking for that! This is the code I'm using right now:
public static void replaceInOpenXMLDocument(string pfad, string zuErsetzen, string neuerString)
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(pfad, true))
{
SimplifyMarkupSettings settings = new SimplifyMarkupSettings
{
NormalizeXml = true, // Merges Run's in a paragraph with similar formatting
};
MarkupSimplifier.SimplifyMarkup(doc, settings);
//zuErsetzen = new XElement("Name", zuErsetzen).Value;
var res = from bm in doc.MainDocumentPart.Document.Body.Descendants()
where bm.InnerText != string.Empty && bm.InnerText.Contains(zuErsetzen) && bm.HasChildren == false
select bm;
// bm.InnerText.Contains(zuErsetzen)
foreach (var item in res)
{
item.InsertAfterSelf(new Text(item.InnerText.Replace(zuErsetzen, neuerString)));
item.Remove();
}
doc.Close();
}
}
(This code will work for normal documents with normal text in it!)
SOLUTION / ADDITION 2: If you want to replace text in textboxes, I had to do a little workaround. Textboxes are declared as pictures, so the code above won't touch it.
I found an additional class (link) that searches even through textboxes. The ZIP-download includes an exmaple program, easy to understand.