0

I have a template .docx file where I have to replace the placeholder. I've used the code from c# word interop find and replace everything to replace the name in my word file. That works just fine.

object fileName = GetFilePath();
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true };
Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(fileName, ReadOnly: false, Visible: true);

aDoc.Activate();

FindAndReplace(wordApp, "firstname", "Max");

aDoc.Save();

This implementation does open the word file. My question is, if there is any way to replace the text without opening the file?

Community
  • 1
  • 1
deadpool_Y7
  • 139
  • 2
  • 11
  • When you say "without opening the file", do you mean without opening the file at all, or do you just not want the user to see that the word file has been opened? (I.e. it shouldn't appear in the task bar etc) – Bassie Dec 16 '16 at 12:57
  • @Bassie I don't want the user to see that the word file has been opened as Andrejs Mivreniks explained that there is no way without opening it. – deadpool_Y7 Dec 16 '16 at 13:25
  • I added my own answer is that what you were after? – Bassie Dec 26 '16 at 14:31
  • 1
    @Bassie I haven't had time to check your answer. I will check and if correct accept it as soon as possible. Happy New Year! – deadpool_Y7 Jan 03 '17 at 08:36

2 Answers2

0

To partially change any file you do need to open it first, there is no other way except completely rewriting it every time with contents stored elsewhere like in your application's memory for example.

Andrey M.
  • 21
  • 1
  • 7
0

If you just want to hide the fact that it is open for the user, simply change this parameter:

Visible: false

The file will still be opened, but it will not display a window.

Note that this will still affect the file in the same way as when it is opened normally (other users may not be able to edit it etc).

Bassie
  • 9,529
  • 8
  • 68
  • 159