6

I'm working on a C# project and I need to open a word doc and do a search/replace on it and save the result for later editing within Word itself.

This is to be a stand alone application and not a Word plugin.

Is there any simple code to get me started?

I've searched and not found anything helpful.

EDIT:

Looks like the nuget package DocX will do what I need.

http://docx.codeplex.com/

http://nuget.org/packages/DocX

dovid
  • 6,354
  • 3
  • 33
  • 73
BoltBait
  • 11,361
  • 9
  • 58
  • 87
  • FYI, downvoting a question does *not* reduce one's rep. Did you look at [How to: Access Office Interop Objects by Using Visual C# Features (C# Programming Guide)](https://msdn.microsoft.com/en-us/library/dd264733.aspx) – crashmstr Jul 28 '15 at 18:01
  • This sounds like you're asking somebody to write the code for you. You have a search engine and can search for sample code if that's what you want. You need to make a basic attempt at solving the problem. – mason Jul 28 '15 at 18:01
  • Not write the code for me... just point me in the right direction. – BoltBait Jul 28 '15 at 18:02
  • 1
    You seem like you know your way around .NET. So you'll probably be aware that you need some library to allow you to manipulate the files. So the obvious place to start is to search for ".net docx library". – mason Jul 28 '15 at 18:05
  • 1
    All a .docx file is is a zip file with a bunch of other files. Use a library such as dotNetZip and open the file, then extract out the document.xml file. Do your search and replace, then rezip the file and voila! – Icemanind Jul 28 '15 at 18:22
  • Thank you, @mason, from that I found https://www.nuget.org/packages/DocX/ that looks like it will do what I need. – BoltBait Jul 28 '15 at 18:28
  • Thanks to everyone who tried to help. @mason, post that link as an answer and I'll select it as THE answer. – BoltBait Jul 28 '15 at 18:31
  • @BoltBait I'd rather not. I don't think your question was on topic, otherwise I would have just come out and pointed you directly to that library, as it's the one I use. As it is your question is too broad and you didn't do enough research to figure it out. That's why my comments were designed to lead you down the right path to self-help. So I don't want credit for this one. – mason Jul 28 '15 at 18:34
  • Anyone experiencing the error "the file can't be opened because their is a problem with its contents" when opening a dotx file; editing and saving to new folder? I'm seeing this in word 2010,2013 and 2016( Using DocX library) – dinotom Sep 03 '16 at 22:02

2 Answers2

3

If you save the doc as a .xml initially from within word you could open it as plain markup (as opposed to a binary) and do a (very rough) search and replace of the raw doc, you'll have to make sure you didn't mangle any tags containing the target words, but it would work.

You'll preserve all formatting and will be able to open/redistribute it as normal in word, the .xml is basically just an uncompressed .docx .

Edit: Giving this is a possible easy solution, not necessarily saying it's the best idea.

Kaelan Fouwels
  • 1,155
  • 1
  • 13
  • 28
0

with Open XML you can open and manipulate a word document.

dovid
  • 6,354
  • 3
  • 33
  • 73