Results 1 to 14 of 14
  1. #1
    Join Date
    May 2009
    Posts
    10

    Question Unanswered: VB Script for Word Doc creation

    Hi All

    Am in urgent need for a VB script that can create word files from an existing document.

    Ex: i have about 100 pages in a document, which i need to create individual Word files for each page, with a name on each file.

    Can any one please help me out here.....


    Thanks in Advance.
    Srini

  2. #2
    Join Date
    May 2009
    Posts
    258
    The following might get you going in the right direction:
    VBA Express : Word - Split file into multiple documents using chosen delimiter and filename

    Also Individual merge letters, in the Split the single merged document into separate letters section.

    Regards,

    Ax

  3. #3
    Join Date
    May 2009
    Posts
    10
    Hi Ax, thanks for sending this link. Am not quite sure, how to do the delimiter in the word file. Can you help me out with that.

    ** i added the code in a new module, post that am stuck in proceeding further

  4. #4
    Join Date
    May 2009
    Posts
    258
    The delimiter would be whatever text you wanted to use as a splitting point for the files. But do note that this delimiter should be unique to the document, in that it would not be present anywhere else in the document. If this is a document created from a [mail] merge, see the second link.

    It's hard for me to assist you without knowing what's in the document. How was this document created? Does it have section breaks or bookmarks?

  5. #5
    Join Date
    May 2009
    Posts
    258
    I coded the following method, it should work for you:
    Code:
    Sub SplitDocument()
        Dim iCount As Integer, i As Integer, DocName As String, DocPath As String
        iCount = BuiltInDocumentProperties(wdPropertyPages)
        DocName = Left(ActiveDocument.Name, InStrRev(ActiveDocument.Name, ".") - 1)
        DocPath = ActiveDocument.Path
        
        For i = 1 To iCount
            Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=i
            ActiveDocument.Bookmarks("\Page").Range.Copy
            Documents.Add.Content.Paste
            ActiveDocument.Bookmarks("\EndOfDoc").Select
            If i <> iCount Then Selection.TypeBackspace ' Remove page break on all but last page
            ActiveDocument.SaveAs Path & "\" & DocName & " Page " & i
            ActiveDocument.Close
        Next
    End Sub
    You can see how it works by stepping through it and viewing the document. If you need to name them differently, I'm sure that won't be too difficult. It saves the files in the same directory as the document you are splitting, but you can change that easily enough as well.

    Ax

  6. #6
    Join Date
    May 2009
    Posts
    10

    Hi Ax

    Hi

    thanks for your responses. I have been traveling for the last couple of weeks so i couldnt revert to you. thanks a ton for your help.

    Am still encountered with a problem, when i put ur code in a module, i get a complier Error in line = incount.

    Also can you help me by explaning, how the process of this works. i;e, from mail merge to splitting them in individual docs.

    Thanks again Ax, appreciate your help to me.

    Regards
    Srini

  7. #7
    Join Date
    May 2009
    Posts
    10
    the complie error that i get from your code is in line 3 which starts at iCount = BuiltInDocumentProperties.

  8. #8
    Join Date
    May 2009
    Posts
    10
    Ax, now i could manage myself to split the document, but the document after split lies in one document with mupltiple pages. Can you guide me as to how can i save those pages into individual files?

  9. #9
    Join Date
    May 2009
    Posts
    258
    The code I posted will save them into individual files. What is the error that you are receiving? Try changing the line to the following:
    Code:
        iCount = ActiveDocument.BuiltInDocumentProperties("Number of Pages")
    Ax

  10. #10
    Join Date
    May 2009
    Posts
    10
    Hi Ax,
    Thanks for the code, now able to save the document in individual filese
    but there is a complie error that i get in the below line

    ActiveDocument.SaveAs Path & "\" & DocName & " Page " & i

    as with reference to PATH. Not sure what to do now?
    Also when i run the code to save the files in individual files, i get all 100 pages in each individual file, can you help me solve this?

  11. #11
    Join Date
    May 2009
    Posts
    258
    My bad, that line should be:
    Code:
            ActiveDocument.SaveAs DocPath & "\" & DocName & " Page " & i
    So the code should be:
    Code:
    Sub SplitDocument()
        Dim iCount As Integer, i As Integer, DocName As String, DocPath As String
        iCount = ActiveDocument.BuiltInDocumentProperties("Number of Pages")
        DocName = Left(ActiveDocument.Name, InStrRev(ActiveDocument.Name, ".") - 1)
        DocPath = ActiveDocument.Path
        
        For i = 1 To iCount
            Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=i
            ActiveDocument.Bookmarks("\Page").Range.Copy
            Documents.Add.Content.Paste
            ActiveDocument.Bookmarks("\EndOfDoc").Select
            If i <> iCount Then Selection.TypeBackspace ' Remove page break on all but last page
            ActiveDocument.SaveAs DocPath & "\" & DocName & " Page " & i
            ActiveDocument.Close
        Next
    End Sub
    Ax

  12. #12
    Join Date
    May 2009
    Posts
    10
    this is awesome just how i wanted it to be. Thank you so much Ax..
    am delighted with what i could accomplish with your code.

    Srini

  13. #13
    Join Date
    May 2009
    Posts
    10
    Hi Ax, could you also help me in telling me which code would help me save the files in the name i want?

  14. #14
    Join Date
    May 2009
    Posts
    258
    The following line saves the files, just change it to what you want the name to be:
    Code:
      ActiveDocument.SaveAs DocPath & "\" & DocName & " Page " & i
    Ax

Posting Permissions

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