Link to home
Start Free TrialLog in
Avatar of 1015148997
1015148997

asked on

Office Automation. How to insert a Word doc into another Word doc?

Hi,

I am trying to automate a Word Process.  I have a base file which contains a header, which basically is a letterhead.  I have a body file which contains some contents which is populated based on a template.  Both of these document are generated programatically by VB.NET.  I close and save the body file.  And then Insert it into the base file using the InsertFile method.  My code looks like this.

Public Overloads Function CreateWordDocument(ByVal relatedForm As Form, ByVal fileName As String, _
        ByVal headerTemplateName As String, ByVal templateName As String, _
        ByVal sourceData As System.Data.DataSet, ByVal key As System.Guid) _
        As Word.Document Implements [Interface].Client.IPT_MicrosoftOffice.CreateWordDocument

        Dim baseWord As MSWordApplication = New MSWordApplication(relatedForm)
        Dim bodyWord As MSWordApplication = New MSWordApplication(relatedForm)
        Dim baseDoc As Word.Document = baseWord.CreateHeaderDocument(fileName, headerTemplateName)
        Dim pt_TemplateInterface As IPT_Template = StartClass.Current.GetPT_TemplateInterface()
        Dim bodyFileName As String = pt_TemplateInterface.GetWorkingDirectory() + "Body" + key.ToString() + ".doc"
        bodyWord.CreateBodyDocument(bodyFileName, templateName, sourceData, key)
       
        baseWord.Application.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = False
        baseWord.Application.Selection.InsertFile(bodyFileName)

        baseDoc.SaveAs(fileName)
        File.Delete(bodyFileName)
       
        Me.AddToHash(baseDoc.FullName, baseWord)
        Return baseDoc

    End Function

Now, the insertion more or less work But...Despite what I have done, the body file always get inserted into the first page, pushing my base file to the second.  I think it is because my cursor is not set at the right position.  I tried to move it by doing this before the insertion.

baseWord.Application.Selection.EndOf(Word.WdUnits.wdStory, Word.WdMovementType.wdExtend)

This only works if the base file contains something in the content, If my base file only contains something in the header area, this still does not work.  It took me 2 days already.  I need a Word/VB.NET expert!  
Avatar of abbdan
abbdan

I know this isn't vb.net code but I felt I would post it in hopes of guiding you to the right efforts needed to move to the end of your document prior to pasting.
I just recently wrote some vb6 code using the Microsoft Word 9.0 object library reference and I think it might jog someones knowledge or at least give you some keywords to find the correct syntax for the vb.net equivalent.

  'Make a Range at the end of the Word document.
        Set new_range = WordDoc.Range
        new_range.Collapse wdCollapseEnd

I'm more a Word/VB6 expert but I hope this helps you.
Avatar of 1015148997

ASKER

Nope...Try that.  Doesn't work.  Thanks anyway
To narrow down the helping a bit, I have tried the following:

move / EndOf / extend the cursor to the end of the WdUnits.wdStory,
Select the whole content / range / selection and collapse the content / range / selection on it.
Reverse the insert sequence.  
Do a cut and paste trick on the content / range / selection
Insert a header to the body file (the header still shows up starting from 2nd page)
Open a third file and insert the 2 files separately.

When the base document shows up, it will be at the end of the document. If not, well, it does not show up at all.

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of abbdan
abbdan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have solved part of this question.  Instead of inserting at the file level, I insert and merge my template to become one template, simply by open the base template and then insert the body template then perform a mail merge to stuff data into the body template.  This will work as long as I don't have anything at the base template other than the header and footer area.  

I don't know why it works in .dot but not .doc but still something is wrong.  If I have something typed in the paragraph area, it will still be merged to the end of the file.  I have tried the move / EndOf / extend the cursor to the end of the WdUnits.wdStory.  I have tried collapse the content / range / selection on it and the list of above tricks where applicable.  For now, I am asking my client to avoid it manually by only have header / footer information at the base template.  Still looking forward to an answer.
I have tried bookmark too.  It looses the header as well.  Don't know why.
I don't know why either because I added a header and a footer to a .dot template and placed a bookmark at the location where I wanted to add text and it did add text where I wanted without deleting the header or footer.  Might want to check your .dot or .doc or word.exe for settings/glitches/errors.  You may need to change the view of the .doc/.dot to view "Print Layout" instead of "Normal".  This would cause the headers/footers to dissappear after an edit.
I just want you to know that I am trying your idead, abbdan.  I did tried template before but that was when I am inserting files.  The header was lost on the first page but it comes back up in the second.  So I think it might still be a wrong position somehow.  I will get back to you for results.
This is my final code that seems to have solve my problem.  Although the merging is occuring in the Template, not the final document as I first started with.  I have tried the same trick on the final document and that does not work.  This solution limits my base template to have no datasource attached to it, as the 2 merged together will share 1 datasource link or perhaps i would try headerdatasource later?!  Anyway, i will take this for now.

 ' Create a Document with a base Template and a regular template
    Public Overloads Function CreateWordDocument(ByVal relatedForm As Form, ByVal fileName As String, _
        ByVal headerTemplateName As String, ByVal templateName As String, _
        ByVal sourceData As System.Data.DataSet, ByVal key As System.Guid) _
        As Word.Document Implements [Interface].Client.IPT_MicrosoftOffice.CreateWordDocument

        ' Create the Body DS file into the working directory
        ' so it's available for the inserting template
        Me.CreateMailMergeDataFile(sourceData, key)

        ' Merge the 2 templates together
        Dim baseTempWord As MSWordApplication = New MSWordApplication(relatedForm)
        baseTempWord.Application.Visible = False
        Dim baseTemplate As Word.Document = baseTempWord.Application.Documents.Open(headerTemplateName)
        baseTemplate.Application.Selection.EndOf(Word.WdUnits.wdStory, Word.WdMovementType.wdMove)
        baseTemplate.Application.Selection.InsertBreak(Word.WdBreakType.wdLineBreak)
        baseTempWord.Application.Selection.InsertFile(templateName)
        baseTemplate.SaveAs(headerTemplateName)
        baseTemplate.Close(False)
        baseTempWord.Application.Quit(False)
        File.Delete(templateName)

        ' Perform Mailmerge to create a new document based on this new template
        Return Me.CreateWordDocument(relatedForm, fileName, headerTemplateName, sourceData, key)

    End Function
I did not use bookmarks or collapse range as abbdan has suggested, but the suggestion of working on the template occur about the same time I start to do my present solution, so I give credited of what was advised.