none
Content in docx files not being included when inserting into a new docx file. RRS feed

  • Question

  • I found one other similar thread asking about this, but it's not quite the same.  Right now, I am creating a new blank docx file based on a dotx template.  I am then taking x number of docx files in a list, and spooling through that list - using the InsertFile (as shown below) to insert them into the new blank document.  The problem is, all the images are missing.  I look at the individual documents before merging them into the new one, and they contain all their embedded images - but when I load up the new document - they are broken.  Inside of the docx file, a media folder hasn't even been created, and none of the content has been copied into the new document from the originals.  Any way to fix this, or any way around it?  Here is a snippet of my InsertFile use:

     

     

            public void MergeNewDocument(string fileToMerge)
            {
                if (!_wordDoc.Equals(null))
                {
                    Word.Selection selection = _wordApp.Selection;
                    selection.InsertFile(fileToMerge, ref _missing, ref _missing, ref _missing, ref _missing);
                    //Insert page break after this file
                    selection.InsertBreak(ref _pageBreak);
                }
            }

     

    Thursday, March 17, 2011 12:54 PM

Answers

  • Well, I fixed it.  By hacking it to death.  In case anyone wants to know what I did - I ended up taking the generated Word documents, opening them in the background, then saving them as Word 97-2003 format, inserting those into the dot template, then finally opening that dot template created file and saving it back as a docx.  This preserves all the content.

     

            public void MergeNewDocument(string fileToMerge)
            {
                if (!_wordDoc.Equals(null))
                {
                    string originalFileName = fileToMerge;
                    string modifiedFileName = fileToMerge.Replace("docx""doc");
                    object FileFormat = Word.WdSaveFormat.wdFormatDocument97;
                    object FileNameO = originalFileName;
                    object FileNameM = modifiedFileName;
     
                    object IsVisible = false;
                    Word.Document tempDoc = new Word.Document();
     
     
                    tempDoc = _wordApp.Documents.Open(ref FileNameO, ref _missing, ref _isReadOnlyFalse, ref _missing, ref _missing,
                        ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref IsVisible, ref _missing,
                        ref _missing, ref _missing, ref _missing);
                    tempDoc.SaveAs(ref FileNameM, ref FileFormat, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing,
                        ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing);
                    tempDoc.Close(ref _missing, ref FileFormat, ref _missing);
                    File.Delete(originalFileName);
     
                    Word.Selection selection = _wordApp.Selection;
                    selection.InsertFile(modifiedFileName, ref _missing, ref _missing, ref _missing, ref _missing);
                    //Insert page break after this file
                    selection.InsertBreak(ref _pageBreak);
                }
            }

     

     

    • Marked as answer by MikeyV27Work Thursday, March 17, 2011 6:15 PM
    Thursday, March 17, 2011 6:13 PM