none
Word automation: copying the format from a section to a whole document RRS feed

  • Question

  • I am working on a VB.net program involving mail merge through Word automation.  The program creates a mail merged document and then strips out the individual sections (the separate letters) and pastes them into new documents which are then saved.  This all works fine, but the resultant documents end up with extra blank pages because the final paragraph marks are pushed down to far.  The only solution I've found is to delete the section break that is copied over with the letter but this also deletes the formatting for the copied text.  Is there a way to copy the text and its formatting into the new documents without having to carry over the section breaks aswell?  Thanks in advance.
    • Moved by edhickey Tuesday, July 12, 2011 10:09 PM (From:.NET 3.0/3.5 Windows Workflow Foundation)
    Tuesday, July 12, 2011 9:44 PM

Answers

  • Oh ... just the margins are different? Margins aren't controled by any indicator in a paragraph, that's a document layout.  Paragraphs and characters control everything about the formatting of text. If the text formatting is good ... you're almost home! :-)

    All you have to do is apply the source document's Page Setupsettings to the new document. If you use code similar to below you can grab the essential items from the source and then after creating the new document immediately set the values.

    Sub DocPageSetup()
    
     Dim SourceDoc As Word.Document
    
     Dim NewDoc As Word.Document
    
     Dim Srng As Word.Range
    
     Dim Nrng As Word.Range
    
     Dim S As Word.PageSetup
    
     
    
     Set SourceDoc = Word.ActiveDocument
    
     Set Srng = sourcedoc.Range
    
     Set S = Srng.PageSetup
    
     
    
     Set NewDoc = Word.Documents.Add
    
     Set Nrng = NewDoc.Range
    
     
    
     With Nrng.PageSetup
    
      .LineNumbering = S.LineNumbering
    
      .Orientation = S.Orientation
    
      .TopMargin = S.TopMargin
    
      .BottomMargin = S.BottomMargin
    
      .LeftMargin = S.LeftMargin
    
      .RightMargin = S.RightMargin
    
      .Gutter = S.Gutter
    
      .HeaderDistance = S.HeaderDistance
    
      .FooterDistance = S.FooterDistance
    
      .PageWidth = S.PageWidth
    
      .PageHeight = S.PageHeight
    
      .FirstPageTray = S.FirstPageTray
    
      .OtherPagesTray = S.OtherPagesTray
    
      .SectionStart = S.SectionStart
    
      .OddAndEvenPagesHeaderFooter = S.OddAndEvenPagesHeaderFooter
    
      .DifferentFirstPageHeaderFooter = S.DifferentFirstPageHeaderFooter
    
      .VerticalAlignment = S.VerticalAlignment
    
      .SuppressEndnotes = S.SuppressEndnotes
    
      .MirrorMargins = S.MirrorMargins
    
      .TwoPagesOnOne = S.TwoPagesOnOne
    
      .BookFoldPrinting = S.BookFoldPrinting
    
      .BookFoldRevPrinting = S.BookFoldRevPrinting
    
      .BookFoldPrintingSheets = S.BookFoldPrintingSheets
    
      .GutterPos = S.GutterPos
    
     End With
    
    End Sub
    
    
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by BPD Wednesday, July 13, 2011 8:58 PM
    Wednesday, July 13, 2011 8:24 PM

All replies

  • It sounds like the section break is sitting on the same line as the text you are copying and thus it contains all of the formatting. So, at the end of the text/start of the section break insert a paragraph mark. Now all of the formatting will be contained in the paragraph mark and not the section break.

    Hope this helps


    Kind Regards, Rich ... http://greatcirclelearning.com
    Tuesday, July 12, 2011 10:57 PM
  • Thank you for your reply.  I must be doing something wrong because when I insert a paragraph at the end of the text/start of the section break it deletes my text leaving me with only the new paragraph mark and the paragraph mark for the document's default ending paragraph.  Any advice would be most appreciated.
    Wednesday, July 13, 2011 3:34 PM
  • You have to collapse the range, otherwise when you insert the paragraph it replaces the content.  This example should help.

      If rng.Information(wdWithInTable) Then
        rng.EndOf unit:=Word.WdUnits.wdTable, Extend:=Word.wdExtend
        rng.Collapse Word.WdCollapseDirection.wdCollapseEnd
        rng.InsertParagraph
        rng.MoveStart unit:=Word.WdUnits.wdTable, count:=-1
        rng.Copy
      End If
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Wednesday, July 13, 2011 4:15 PM
  • Thank you for the example.  This seems to be on the right track; I can insert the paragraph without erasing my text.  However, the paragraph apparently isn't picking up the section formatting (or at least not all of it).  When I paste the copied text the formatting is slightly different.

    Wednesday, July 13, 2011 6:28 PM
  • Mmmm... what formatting is not coming over?

    Also, maybe we should backup. When your automation created this original document, is there a way to insert the section break on its own paragraph?

    And is there a way for you to post a link to a small sample of the souce document that I could download and experiment with?


    Kind Regards, Rich ... http://greatcirclelearning.com
    Wednesday, July 13, 2011 7:02 PM
  • "Mmmm... what formatting is not coming over?"

    It looks as though the page margins are smaller (not by much but enough so that the pasted text is obviously different from the source).

    "Also, maybe we should backup. When your automation created this original document, is there a way to insert the section break on its own paragraph?"

    I'm not sure, the source document is created through a mail merge with a .csv file.  To create the individual documents I was copying out the sections that the mail merge created.

    "And is there a way for you to post a link to a small sample of the souce document that I could download and experiment with?"

    Sorry, I'm afraid I don't currently have access to a file-share.  I could post some of my actual code if you'd like. 

    Thank you for all of your help.  I really appreciate it.  


    Wednesday, July 13, 2011 7:46 PM
  • Oh ... just the margins are different? Margins aren't controled by any indicator in a paragraph, that's a document layout.  Paragraphs and characters control everything about the formatting of text. If the text formatting is good ... you're almost home! :-)

    All you have to do is apply the source document's Page Setupsettings to the new document. If you use code similar to below you can grab the essential items from the source and then after creating the new document immediately set the values.

    Sub DocPageSetup()
    
     Dim SourceDoc As Word.Document
    
     Dim NewDoc As Word.Document
    
     Dim Srng As Word.Range
    
     Dim Nrng As Word.Range
    
     Dim S As Word.PageSetup
    
     
    
     Set SourceDoc = Word.ActiveDocument
    
     Set Srng = sourcedoc.Range
    
     Set S = Srng.PageSetup
    
     
    
     Set NewDoc = Word.Documents.Add
    
     Set Nrng = NewDoc.Range
    
     
    
     With Nrng.PageSetup
    
      .LineNumbering = S.LineNumbering
    
      .Orientation = S.Orientation
    
      .TopMargin = S.TopMargin
    
      .BottomMargin = S.BottomMargin
    
      .LeftMargin = S.LeftMargin
    
      .RightMargin = S.RightMargin
    
      .Gutter = S.Gutter
    
      .HeaderDistance = S.HeaderDistance
    
      .FooterDistance = S.FooterDistance
    
      .PageWidth = S.PageWidth
    
      .PageHeight = S.PageHeight
    
      .FirstPageTray = S.FirstPageTray
    
      .OtherPagesTray = S.OtherPagesTray
    
      .SectionStart = S.SectionStart
    
      .OddAndEvenPagesHeaderFooter = S.OddAndEvenPagesHeaderFooter
    
      .DifferentFirstPageHeaderFooter = S.DifferentFirstPageHeaderFooter
    
      .VerticalAlignment = S.VerticalAlignment
    
      .SuppressEndnotes = S.SuppressEndnotes
    
      .MirrorMargins = S.MirrorMargins
    
      .TwoPagesOnOne = S.TwoPagesOnOne
    
      .BookFoldPrinting = S.BookFoldPrinting
    
      .BookFoldRevPrinting = S.BookFoldRevPrinting
    
      .BookFoldPrintingSheets = S.BookFoldPrintingSheets
    
      .GutterPos = S.GutterPos
    
     End With
    
    End Sub
    
    
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by BPD Wednesday, July 13, 2011 8:58 PM
    Wednesday, July 13, 2011 8:24 PM
  • That fixed it.  Everything seems fine now.  Thank you very much for your time and help.

    Wednesday, July 13, 2011 8:59 PM