Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 07-15-2015, 04:47 PM
BrotherDude BrotherDude is offline Headers - Find text, Select, Pass value to Varable Windows 7 64bit Headers - Find text, Select, Pass value to Varable Office 2010 64bit
Novice
Headers - Find text, Select, Pass value to Varable
 
Join Date: Jul 2015
Posts: 3
BrotherDude is on a distinguished road
Default Copy Page with Formatting

Hello all,

I am splitting a large document into individual documents by for each page. In the code below I am losing formatting. Is there a way to copy the formatting exactly? Do I need to copy the entire doc and delete all pages but the one I want? Do I need to use a template? Any help is appreciated.

Code:
Sub SplitDoc()
    Dim docMultiple As Document
    Dim docSingle As Document
    Dim rngPage As Range
    Dim iCurrentPage As Integer
    Dim iPageCount As Integer
    Dim strNewFileName As String
    Dim strInvoice 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.PasteAndFormat Type:=wdFormatOriginalFormatting
        '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")
        'strNewFileName = "Whatever" & ".doc" ' Usually a sting from differan Sub
        strNewFileName = "whatever" & ".PDF"
        docSingle.SaveAs strNewFileName, Word.WdSaveFormat.wdFormatPDF 'save the new single-paged document
        iCurrentPage = iCurrentPage + 1 'move to the next page
        docSingle.Close SaveChanges:=False '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
Any help is appreciated!

-Jeff
Reply With Quote
 

Tags
copy and paste, find, format, headers footers all pages, page count, range, select text, text



Similar Threads
Thread Thread Starter Forum Replies Last Post
Microsoft Word macro to find text, select all text between brackets, and delete helal1990 Word VBA 4 02-05-2015 03:52 PM
Macro for find/replace (including headers and footers) for multiple documents jpb103 Word VBA 2 05-16-2014 04:59 AM
Headers - Find text, Select, Pass value to Varable How to find and select text in a document? mkhuebner Word VBA 8 02-04-2014 08:04 PM
Nested vlookup with varable tables! Dave Jones Excel 0 08-30-2012 09:15 AM
Headers - Find text, Select, Pass value to Varable Unable to Select Multiple folders in Outlook 2007 Advance Find gregory Outlook 2 04-28-2012 10:53 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:21 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft