0
votes

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:

  1. Append example.doc from the beginning to the beginning of the TAG.
  2. Append 123456789.doc
  3. Append the rest of example.doc from the end of the tag to the end of the document.
1
I define a range in document 1. 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 XII
If I set the destination range the end of the document object 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

1 Answers

0
votes

It appears like you are close to a solution. When you find the tag and select it in the document, you can use the WordApp.Selection.InsertFile() method to insert contents from another document into that selection.

It is important to make sure that there is a selection made first.

Example method :

        /// <summary>
        /// This method adds a file to the Word.ApplicationClass object's current selection.
        /// Has been tested with word (*.doc)documents but not with other office files.
        /// Other files may work also with this method.
        /// Tested with the Microsoft 9.0 Object Library ( COM )
        /// </summary>
        /// <param name="WordApp">The Word.ApplicationClass object</param>
        /// <param name="DirLocation">A string that contains the file directory location.</param>
        /// <param name="FileName">A string that contains the file name within the directory location.</param>
        public static void InsertFile(Word.ApplicationClass WordApp, string DirLocation, string FileName)
        {
            try
            {
                object j_NullObject = System.Reflection.Missing.Value;

                WordApp.Selection.InsertFile(DirLocation + FileName, ref j_NullObject, ref j_NullObject, ref j_NullObject, ref j_NullObject);
            }

            //An Exception will occur when the Directory location and filename does not exist.
            //An Exception may also occur when the Word.ApplicationClass has no Selection to add
            //the file to.
            catch (Exception e)
            {
                //show the user the error message
                MessageBox.Show(e.Message);

                //log the error
                //ErrorLog.LogError(e);
            }
        }