Consulting

Results 1 to 4 of 4

Thread: Delete extra space if any at end of document

  1. #1
    VBAX Newbie
    Joined
    Mar 2012
    Posts
    3
    Location

    Unhappy Delete extra space if any at end of document

    Hello ,

    I am using the below code to save every page into new document.

    Sub SplitIntoPages() 
        Dim docMultiple As Document 
        Dim docSingle As Document 
        Dim rngPage As Range 
        Dim iCurrentPage As Integer 
        Dim iPageCount As Integer 
        Dim strNewFileName As String 
         
        Application.ScreenUpdating = False 'Makes the code run faster and reduces screen _
        flicker a bit. 
        Set docMultiple = ActiveDocument 'Work on the active document _
        (the one currently containing the Selection) 
        Set rngPage = docMultiple.Range 'instantiate the range object
        iCurrentPage = 1 
         'get the document's page count
        iPageCount = docMultiple.Content.ComputeStatistics(wdStatisticPages) 
        Do Until iCurrentPage > iPageCount 
            If iCurrentPage = iPageCount Then 
                rngPage.End = ActiveDocument.Range.End 'last page (there won't be a next page)
            Else 
                 'Find the beginning of the next page
                 'Must use the Selection object. The Range.Goto method will not work on a page
                Selection.GoTo wdGoToPage, wdGoToAbsolute, iCurrentPage + 1 
                 'Set the end of the range to the point between the pages
                rngPage.End = Selection.Start 
            End If 
            rngPage.Copy 'copy the page into the Windows clipboard
            Set docSingle = Documents.Add 'create a new document
            docSingle.Range.Paste 'paste the clipboard contents to the new document
             'remove any manual page break to prevent a second blank
            docSingle.Range.Find.Execute Findtext:="^m", ReplaceWith:="" 
             'build a new sequentially-numbered file name based on the original multi-paged file name and path
            strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc") 
            docSingle.SaveAs strNewFileName 'save the new single-paged document
            iCurrentPage = iCurrentPage + 1 'move to the next page
            docSingle.Close 'close the new document
            rngPage.Collapse wdCollapseEnd 'go to the next page
        Loop 'go to the top of the do loop
        Application.ScreenUpdating = True 'restore the screen updating
         
         'Destroy the objects.
        Set docMultiple = Nothing 
        Set docSingle = Nothing 
        Set rngPage = Nothing 
    End Sub
    Code works really well. But some pages of original document are split into two pages due to space. How can i delete the space at end of each page before saving? Please advise.

    Many Thanks,
    vds1

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by vds1 View Post
    Code works really well. But some pages of original document are split into two pages due to space. How can i delete the space at end of each page before saving? Please advise.
    Surely the better approach is to produce pages that don't have all this unwanted content, rather than trying to clean it up afterwards. There is no good reason I can think of for having it. Your description suggests the document was created by someone who's never heard of manual page breaks and/or Next page Section breaks. That said, the clean-up may require nothing more than:
    With docSingle.Characters.Last
      While .Previous Like "[ " & Chr(11) & Chr(12) & Chr(160) & vbTab & vbCr & "]"
        .Previous.Text = vbNullString
      Wend
    End With
    inserted before 'docSingle.Close'
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Newbie
    Joined
    Mar 2012
    Posts
    3
    Location
    Thanks Paul.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Paul,
    To be complete, I suppose it wouldn't hurt to remove trailing EM, EN and 1/4 EN space:

    With docSingle.Characters.Last
      'Removes empty trailing lines, empty trailing paragraphs, trailing space, trailing non-breaking space _
      trailing Em space, traling En space, trailing 1/4 En space.
        While .Previous Like "[" & Chr(11) & Chr(12) & Chr(32) & Chr(160) & ChrW(8194) _
                             & ChrW(8195) & ChrW(8197) & vbTab & vbCr & "]"
            .Previous.Text = vbNullString
        Wend
    End With
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •