Word VBA - InsertFile Method RRS feed

  • Question

  • 1)  I have a document that I am creating dynamically from several word documents and a database.  In the routine, I am using the Range.Insertfile method to insert the contents of the file into the current cursor position of the document.  Prior to the first file being inserted, I create a new Section Break (BreakSectionNextPage) and setup a header and footer for the new section, which is not linked to the previous section.  Once the file is inserted, I create a new Section Break (BreakSectionNextPage) and setup a header and footer for the new section.  Again, the headers and footers are not linked to the prior section.

    The external documents load fine.  However, when the file is inserted, the header for this section is reset to that of the section just prior to the first file insertion point.  Why is this?  I need each new section to retian its own header/footer.

    This is the snippet of code that creates the new section and inserts the file:

            Call InsrtBreak(3)
            Set oSec = wDoc.Sections(wDoc.Sections.Count)
            oSec.PageSetup.LeftMargin = InchesToPoints(0.75)
            oSec.PageSetup.RightMargin = InchesToPoints(0.75)
            oSec.PageSetup.TopMargin = InchesToPoints(1#)
            oSec.PageSetup.BottomMargin = InchesToPoints(0.75)
            Call CreateNewSectionHeader("General Information")
            Call CreateNewSectionFooter("")
            Set Rng = ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range
            oSec.Range.InsertFile FileName:=sFile, Range:="Info"


    oSec is a Section object variable

    These are the sub routines:

    Private Sub InsrtBreak(BreakType As Integer)
         Dim sBreakType As String
         Select Case BreakType
              Case 1: wDoc.Range(oSec.Range.End - 1).InsertBreak wdPageBreak: sBreakType = "wdPageBreak"
              Case 2: wDoc.Range(oSec.Range.End - 1).InsertBreak wdSectionBreakContinuous: sBreakType = "wdSectionBreakContinuous"
              Case 3: wDoc.Range(oSec.Range.End - 1).InsertBreak wdSectionBreakNextPage: sBreakType = "wdSectionBreakNextPage"
              Case 4: wDoc.Range(oSec.Range.End - 1).InsertBreak wdSectionBreakEvenPage: sBreakType = "wdSectionBreakEvenPage"
              Case 5: wDoc.Range(oSec.Range.End - 1).InsertBreak wdSectionBreakOddPage: sBreakType = "wdSectionBreakOddPage"
              Case 6: wDoc.Range(oSec.Range.End - 1).InsertBreak wdColumnBreak: sBreakType = "wdColumnBreak"
         End Select
    End Sub

    Private Sub CreateNewSectionHeader(sHeaderText)
         With wDoc
              oSec.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
              oSec.Headers(wdHeaderFooterPrimary).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
              oSec.Headers(wdHeaderFooterPrimary).Range.Bold = True
              oSec.Headers(wdHeaderFooterPrimary).Range.Font.Name = "Times New Roman"
              oSec.Headers(wdHeaderFooterPrimary).Range.Font.Size = 14
              If sHeaderText <> "" Then oSec.Headers(wdHeaderFooterPrimary).Range.Text = sPlnTxt & vbCrLf & sHeaderText
         End With
    End Sub

    Private Sub CreateNewSectionFooter(sFtrTxt)
         With wDoc
            oSec.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
            If sFtrTxt <> "" Then
                oSec.Footers(wdHeaderFooterPrimary).Range.Text = sFtrTxt & vbTab & vbTab & vbTab
                Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
            End If
         End With
    End Sub

    2)  In this same document, when the files are inserted, some of the font formatting (Size, Bold) are changed from what it was set to in the document being inserted - generally changing the font size from 10 to 12, and the setting Bold = True.  How do I keep this from happening?

    3)  One of the documents being inserted has two text columns.  When I insert it, I loose the columns.  To get the columns back, I added text columns to this section, and the text for this section is formated to 2 columns.  However, when I do this, the BreakSectionNextPage is removed, and the text is repositioned up to the prior Section.  How can I prevent this?

    This is the snippet of code:

            Set Rng = ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range
            Rng.InsertFile FileName:=sFile, Range:="Info"
            Rng.PageSetup.TextColumns.Add Width:=InchesToPoints(3.25), Spacing:=InchesToPoints(0.25), EvenlySpaced:=False
            Rng.PageSetup.TextColumns.LineBetween = True 'REMOVES THE SECTION BREAK
            Rng.PageSetup.TextColumns.SetCount 2

    Thank You!

    • Moved by Jeff Shan Friday, November 6, 2009 3:44 AM vba question (From:Visual Basic General)
    Wednesday, November 4, 2009 10:23 PM


All replies