Monday, May 2, 2011

Merging Documents

Merging documents can be a bit tricky when you add watermarks into the equation. I had to implement a document merge function that merges a preface page and a main document. This package could then be multiplied several times with different data in the preface page and a watermark on all but the first main document.

oDoc is the active document.

Dim aCCDocuments AsArrayList
aCCDocuments = NewArrayList()
UnlinkAndCreateSectionBreak(oDoc)
AddWatermark("Copy", i)
s = GetTempPath() & "Watermarked_"& i & "_" & m_sDocumentName
oDoc.SaveAs(CType(s, Object))
DeleteWatermark(i)
m_oDoc.SaveAs(CType(m_sFullDocumentName,Object))

If Not GenerateRecipientLetter(m_sDocumentName, aCCDocuments) = False Then
    'Merge Documents and print
    MergeDocuments(aCCDocuments, s)
End If

ClearTempDocuments(s, aCCDocuments)
The first thing I do is to insert a new SectionBreakNextPage and then unlink the headers and footers in the main document. This is needed to allow us to have different headers and footers in the merged document later. The following code explains this process.

Private Sub UnlinkAndCreateSectionBreak(ByRef oDoc As Word.Document)
    Dim oParagraph As Microsoft.Office.Interop.Word.Paragraph
    Dim oRangeEnd AsMicrosoft.Office.Interop.Word.Range
    Dim oHeader As Word.HeaderFooter
    Dim oFooter As Word.HeaderFooter
    Dim oSection As Microsoft.Office.Interop.Word.Section

    oRangeEnd = oDoc.Range

    'Insert the SectionBreakNextPage
    With oRangeEnd
        .Collapse(Word.WdCollapseDirection.wdCollapseEnd)
        oParagraph = oDoc.Paragraphs.Add(CType(oRangeEnd, Object))
        oParagraph.Range.InsertParagraphAfter()
        oParagraph.Range.InsertBreak(Word.WdBreakType.wdSectionBreakNextPage)
    End With

    'Move to newly created section
    oSection = oDoc.Sections.Last
    With oSection
        'Unlink all headers from previous.
        For Each oHeader In .Headers
            oHeader.LinkToPrevious = False
        Next

        'Restart page count if there is one in the header.
        .Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).PageNumbers.RestartNumberingAtSection = True
        .Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).PageNumbers.StartingNumber = 1

        'Unlink all footers from previous.
        For Each oFooter In .Footers
            oFooter.LinkToPrevious = False
        Next

        'Restart page count if there is one in the footer.
        .Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).PageNumbers.RestartNumberingAtSection = True
        .Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).PageNumbers.StartingNumber = 1
    End With
End Sub

Next we use the AddWatermark method I described in the previous blog post to add a watermark to the document and then save the document in the users %TEMP% folder. GetTempFolder fetches %TEMP% and looks like this:
Private Function GetTempPath() AsString

    Dim sPath As String

    sPath = System.Environment.GetEnvironmentVariable("TEMP")

    If sPath = "" Then
        sPath = System.Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
    End If

    If NotsPath.EndsWith("/") Or NotsPath.EndsWith("\") Then
        sPath = sPath & "\"
    End If

    GetTempPath = sPath
End Function

Once the document is saved we Delete the watermark again and resave the original document. The next step involves creating the preface documents. I will not include any code for this since this part highly depends on your specific needs. The result however is that I create each preface document saves it in the user %TEMP% folder and also adds an entry to an ArrayList (aCCDocuments). This leads upp to the actual merge part.
Private Sub MergeDocuments(ByValaCCDocuments As ArrayList,ByVal sWaterMarked AsString)

    Dim i As Integer

    i = 0

    For Eachfile In aCCDocuments
        ' Insert the files to our template
        oSection = oDoc.Sections.Last
        oSection.Range.InsertFile(file)
        oSection = m_oDoc.Sections.Last
        oSection.Range.InsertFile(sWaterMarked)
        i += 1
    Next

    ' Save the merged document.
    oDoc.Save()
End Sub

This part is quite simple. We iterate over the preface documents and insert, after the original main document, the preface document and then a watermarked copy of the original document, then the next preface document and then again a watermarked copy of the original and so on until there are no more preface document. The final thing we do is to save the merged document.

Now all that remains is to clean up a bit. We did create a watermarked copy of the original document. We also created a number of preface documents. These need to be deleted and that is what I do in ClearTempDocuments()

No comments:

Post a Comment