none
Working with VBA and Sections in Word Document RRS feed

  • Question

  • Hello. I am somewhat inexperienced with VBA so I would greatly appreciate someone's assistance.

    The following code does exactly what I need it to, by printing all of the text within the section in the document identified in the code (Sections 1 and 2), and none of the text in the subsequent section (Section 3) that happens to fall on the same physical page as Section 2:

    Sub PrintSections()

    Dim r As Range
    Dim RangeStart As Long
    Dim RangeEnd As Long

     RangeStart = ActiveDocument.Sections(1).Range.Start
     RangeEnd = ActiveDocument.Sections(2).Range.End
        
     Set r = ActiveDocument.Range(RangeStart, RangeEnd)

     r.Select
     
     ActiveDocument.PrintOut Range:=wdPrintSelection
     
    End Sub

    My challenge is this:  I wish to print Section 4 of the document the same way.  So, how can I use the code above to print Sections 1, 2 and 4 (skip Section 3)?  Again, I cannot use the Print function in Word by entering "s1, s2, s4" because it prints the text in Section 3 that falls on the same physical page as Section 2, and doing so also fails to print the text is Section 4 that happens to fall on the same physical page as Section 3.

    I hope I've explained that well.  Please let me know if I have not.

    Thank-you for your assistance.

    Monday, September 28, 2015 4:43 AM

Answers

  • You could hide section 3:

    Sub PrintSections124()
        Dim RangeStart As Long
        Dim RangeEnd As Long
        Dim PrintHidden As Boolean
        ' Save current setting of Print Hidden Text
        PrintHidden = Application.Options.PrintHiddenText
        ' Hide section 3
        ActiveDocument.Sections(3).Range.Font.Hidden = True
        ' Find start and end of range to be printed
        RangeStart = ActiveDocument.Sections(1).Range.Start
        RangeEnd = ActiveDocument.Sections(4).Range.End
        ' Select range
        ActiveDocument.Range(RangeStart, RangeEnd).Select
        ' Print it
        ActiveDocument.PrintOut Range:=wdPrintSelection
        ' Unhide section 3
        ActiveDocument.Sections(3).Range.Font.Hidden = False
        ' Restore original setting
        Application.Options.PrintHiddenText = PrintHidden
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by R.Shaw Monday, September 28, 2015 2:02 PM
    Monday, September 28, 2015 8:03 AM

All replies

  • You could hide section 3:

    Sub PrintSections124()
        Dim RangeStart As Long
        Dim RangeEnd As Long
        Dim PrintHidden As Boolean
        ' Save current setting of Print Hidden Text
        PrintHidden = Application.Options.PrintHiddenText
        ' Hide section 3
        ActiveDocument.Sections(3).Range.Font.Hidden = True
        ' Find start and end of range to be printed
        RangeStart = ActiveDocument.Sections(1).Range.Start
        RangeEnd = ActiveDocument.Sections(4).Range.End
        ' Select range
        ActiveDocument.Range(RangeStart, RangeEnd).Select
        ' Print it
        ActiveDocument.PrintOut Range:=wdPrintSelection
        ' Unhide section 3
        ActiveDocument.Sections(3).Range.Font.Hidden = False
        ' Restore original setting
        Application.Options.PrintHiddenText = PrintHidden
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by R.Shaw Monday, September 28, 2015 2:02 PM
    Monday, September 28, 2015 8:03 AM
  • Working wonderfully.  Thanks so much for the prompt reply, the solutions and for the comment lines - helps me add to it if I need to.
    Monday, September 28, 2015 2:03 PM
  • Oops - I forgot one essential line: temporarily don't print hidden text regardless of the user's current setting. It should have been:

    Sub PrintSections124()
        Dim RangeStart As Long
        Dim RangeEnd As Long
        Dim PrintHidden As Boolean
        ' Save current setting of Print Hidden Text
        PrintHidden = Application.Options.PrintHiddenText
        ' Turn off printing hidden text
        Application.Options.PrintHiddenText = False
        ' Hide section 3
        ActiveDocument.Sections(3).Range.Font.Hidden = True
        ' Find start and end of range to be printed
        RangeStart = ActiveDocument.Sections(1).Range.Start
        RangeEnd = ActiveDocument.Sections(4).Range.End
        ' Select range
        ActiveDocument.Range(RangeStart, RangeEnd).Select
        ' Print it
        ActiveDocument.PrintOut Range:=wdPrintSelection
        ' Unhide section 3
        ActiveDocument.Sections(3).Range.Font.Hidden = False
        ' Restore original setting
        Application.Options.PrintHiddenText = PrintHidden
    End Sub

    Sorry about that!


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Monday, September 28, 2015 2:14 PM
  • No problem!  Thank-you for updating.  Would it be complex to save it in the same fashion, as a separate document, instead of printing?
    Monday, September 28, 2015 2:33 PM
  • You could use ActiveDocument.SaveAs2 to save the document under a new name, then deletes sections beyond section 4, and finally delete section 3.

    Or create a blank new document, and copy sections 1,2 and 4 to the new document.


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Monday, September 28, 2015 4:31 PM
  •  

    Thanks again.  Here is code that works to eliminate Section 3, but it falls down on "ActiveDocument.SaveAs2".  Can you assist?

    Sub DeleteSection3()

    Dim oSec As Word.Section

        Set oSec = ActiveDocument.Sections(3)
        oSec.Range.Delete

    ActiveDocument.SaveAs2 FileName:="Test.doc", FileFormat:=wdFormatDocument

    End Sub

    The error I receive is Run-time 438: "Object doesn't support this property or method."

    Again, many thanks.


    • Edited by R.Shaw Monday, September 28, 2015 5:51 PM
    Monday, September 28, 2015 5:45 PM
  • SaveAs2 was introduced in Word 2010, I think. You can use SaveAs instead.

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Monday, September 28, 2015 6:49 PM