If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Visual Basic > VB Script for Word Doc creation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-16-09, 07:30
sri.1220 sri.1220 is offline
Registered User
 
Join Date: May 2009
Posts: 10
Question 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
Reply With Quote
  #2 (permalink)  
Old 09-16-09, 08:50
Ax238 Ax238 is offline
Registered User
 
Join Date: May 2009
Posts: 257
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
Reply With Quote
  #3 (permalink)  
Old 09-17-09, 00:40
sri.1220 sri.1220 is offline
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old 09-17-09, 10:30
Ax238 Ax238 is offline
Registered User
 
Join Date: May 2009
Posts: 257
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?
Reply With Quote
  #5 (permalink)  
Old 09-17-09, 12:30
Ax238 Ax238 is offline
Registered User
 
Join Date: May 2009
Posts: 257
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
Reply With Quote
  #6 (permalink)  
Old 10-08-09, 04:50
sri.1220 sri.1220 is offline
Registered User
 
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
Reply With Quote
  #7 (permalink)  
Old 10-08-09, 04:53
sri.1220 sri.1220 is offline
Registered User
 
Join Date: May 2009
Posts: 10
the complie error that i get from your code is in line 3 which starts at iCount = BuiltInDocumentProperties.
Reply With Quote
  #8 (permalink)  
Old 10-08-09, 05:16
sri.1220 sri.1220 is offline
Registered User
 
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?
Reply With Quote
  #9 (permalink)  
Old 10-08-09, 11:55
Ax238 Ax238 is offline
Registered User
 
Join Date: May 2009
Posts: 257
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
Reply With Quote
  #10 (permalink)  
Old 10-09-09, 00:24
sri.1220 sri.1220 is offline
Registered User
 
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?
Reply With Quote
  #11 (permalink)  
Old 10-09-09, 09:17
Ax238 Ax238 is offline
Registered User
 
Join Date: May 2009
Posts: 257
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
Reply With Quote
  #12 (permalink)  
Old 10-12-09, 01:21
sri.1220 sri.1220 is offline
Registered User
 
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
Reply With Quote
  #13 (permalink)  
Old 10-13-09, 05:02
sri.1220 sri.1220 is offline
Registered User
 
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?
Reply With Quote
  #14 (permalink)  
Old 10-15-09, 13:59
Ax238 Ax238 is offline
Registered User
 
Join Date: May 2009
Posts: 257
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On