I am trying to use Word Interop to try to replace a TAG inside a Word Document with the content of another document.
For example, I have to process example.doc
which has a tag like #TAG#123456789#
then I have to copy 123456789.doc
and paste the content in example.doc
replacing the tag #TAG#123456789#
Document 123456789.doc
has its tables, images, and whatever.
So far, I can find the tag inside example.doc and fetch the 123456789.doc
but I didn't find a working approach on how to anything but text replacement.
I am thinking of a new approach, generating a new document by appending parts of documents using ranges like this:
- Append
example.doc
from the beginning to the beginning of the TAG. - Append
123456789.doc
- Append the rest of
example.doc
from the end of the tag to the end of the document.
Microsoft.Office.Interop.Word.Range rng = document.Range(ref docStart, ref docEnd);
I select and copy the entire second document:object start = SubDoc.Content.Start; object end = SubDoc.Content.End; SubDoc.Range(ref start, ref end).Copy();
I copy the content in range 1:rng.Paste();
And save the document:document.Save();
However, I don't get to copy IMAGES, what can I do? Sorry for the answer but couldn't manage to add return lines in a comment... - Ptolomeo XIIobject docStart = document.Content.End - 1; object docEnd = document.Content.End;
when I paste ´Microsoft.Office.Interop.Word.Range rng = document.Range(ref docStart, ref docEnd); rng.Paste();´ It pastes all including images. If I choose range to substitute a word ej:object docStart = document.Content.Words[i - 8].Start; object docEnd = document.Content.Words[i].End;
when I do the same paste, it conly pastes the first words. - Ptolomeo XII