New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextReplacer.SearchAndReplace cannot work on new WordprocessingDocument #267

Open
dkddkk opened this Issue Dec 28, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@dkddkk
Copy link

dkddkk commented Dec 28, 2018

using (WordprocessingDocument xxDoc = WordprocessingDocument.Create(dstPath, WordprocessingDocumentType.Document))
{
MainDocumentPart objMainDocumentPart = xxDoc.AddMainDocumentPart();
objMainDocumentPart.Document = new Document(new Body());

           //add paragraphs here

           TextReplacer.SearchAndReplace(xxDoc, tTarget, tKeyVP.Value, true);

}

SearchAndReplace will throw an exception. _exceptionMethod = {System.Xml.Linq.XDocument GetXDocument(DocumentFormat.OpenXml.Packaging.OpenXmlPart)}

It should be xxDoc cannot build the OpenXmlPart, what should I do? Please give me a method.

ps:
for example:
var inputDoc = WordprocessingDocument.Create("ttt.docx", WordprocessingDocumentType.Document);

        MainDocumentPart inputMainPart = inputDoc.AddMainDocumentPart();
        inputMainPart.Document = new Document(new Body(new 
       DocumentFormat.OpenXml.Wordprocessing.Paragraph(new Run(new Text("Hello,world."))), new                       DocumentFormat.OpenXml.Wordprocessing.Paragraph(new Run(new Text("xxxx.")))));
        inputDoc.Save();

        TextReplacer.SearchAndReplace(inputDoc, "xxx", "ccc", false);

I got a null exception, whenever I save it then open the doc or I create it in memory like above.

Please tell me what should I do?

The word is here:

Hello,world.xxxx.
@ahmet269

This comment has been minimized.

Copy link

ahmet269 commented Jan 4, 2019

i suggest before you call search and replace , put a break point and check if your inputDoc has MainDocumentPart or not? you dont want this to be null before you calling textreplacer.
and what is it that you are trying to achieve here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment