Consulting

Results 1 to 2 of 2

Thread: Split document with new filename being first line of new document

  1. #1
    VBAX Newbie
    Joined
    Nov 2019
    Posts
    1
    Location

    Split document with new filename being first line of new document

    Hello,

    I currently have a multi page document I am trying to split into individual documents. I want the first line of each page to be the file name. Currently using this code:

    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
    strNewFileName = docMultiple.Path & ""

    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 = strNewFileName & Left(docSingle.Range.Paragraphs(1), Len(docSingle.Range.Paragraphs(1).Range.Text) - 1)
    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

    But I'm getting error 91, Object variable or With block variable not set. Any ideas what's going on?


    Thanks!

  2. #2
    If this is a document created by mail merge see https://www.gmayor.com/MergeAndSplit.htm

    The original error you reported is caused because you set a filename parameter from the document variable - strNewFileName = docMultiple.Path & "" , before you had declared the document variable -Set docMultiple = ActiveDocument - but there are other issues that cannot be determined without access to the document. However having declared a document variable, you refer to the document as activedocument, rather than referring to it by that variable name e.g. rngPage.End = ActiveDocument.Range.End should be rngPage.End = docMultiple.Range.End.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.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
  •