Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-04-2015, 05:00 AM
Julian Julian is offline Filename after splitting document Windows 7 64bit Filename after splitting document Office 2013
Novice
Filename after splitting document
 
Join Date: Dec 2015
Posts: 3
Julian is on a distinguished road
Default Filename after splitting document

hey everybody, I have a big word document that I want to split automaticly into a large number of smaller documents. I found this code below, that works perfectly. I just have one question. The code names the new documents Notes1, Notes2, Notes3 ect. but I would like to give them the respective first text line in the document (always 6 words) as document titles.
Is that possible and if yes, how do I have to change the code?
Thank you very much for your help!!!

Quote:
Sub SplitNotes(delim As String, strFilename As String)
Dim doc As Document
Dim arrNotes
Dim I As Long
Dim X As Long
Dim Response As Integer
arrNotes = Split(ActiveDocument.Range, delim)
Response = MsgBox("This will split the document into " & UBound(arrNotes) + 1 & " sections.Do you wish to proceed?", 4)
If Response = 7 Then Exit Sub
For I = LBound(arrNotes) To UBound(arrNotes)
If Trim(arrNotes(I)) <> "" Then
X = X + 1
Set doc = Documents.Add
doc.Range = arrNotes(I)
doc.SaveAs ThisDocument.Path & "\" & strFilename & Format(X, "000")
doc.Close True
End If


Next I
End Sub
Sub test()
'delimiter & filename
SplitNotes "XXX", "Notes "
End Sub
Reply With Quote
  #2  
Old 12-04-2015, 05:41 AM
gmayor's Avatar
gmayor gmayor is offline Filename after splitting document Windows 7 64bit Filename after splitting document Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Maybe something like. I left in the numbering.
Code:
Option Explicit

Sub SplitNotes(delim As String)
Dim doc As Document
Dim arrNotes As Variant
Dim strPath As String
Dim strFilename As String
Dim I As Long
Dim X As Long
Dim oPara As Range
Dim Response As Integer
    ActiveDocument.Save
    strPath = ActiveDocument.Path
    arrNotes = Split(ActiveDocument.Range, delim)
    Response = MsgBox("This will split the document into " & UBound(arrNotes) + 1 & _
                      " sections.Do you wish to proceed?", 4)
    If Response = 7 Then Exit Sub
    For I = LBound(arrNotes) To UBound(arrNotes)
        If Trim(arrNotes(I)) <> "" Then
            X = X + 1
            Set doc = Documents.Add(Template:=ActiveDocument.FullName)
            doc.Range = arrNotes(I)
            Set oPara = doc.Paragraphs(1).Range
            oPara.End = oPara.End - 1
            strFilename = oPara.Text & Chr(32)
            doc.SaveAs strPath & "\" & strFilename & Format(X, "000")
            doc.Close True
        End If
    Next I
lbl_Exit:
    Set doc = Nothing
    Exit Sub
End Sub
Sub test()
'delimiter
    SplitNotes "XXX"
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 12-07-2015, 06:16 AM
Julian Julian is offline Filename after splitting document Windows 7 64bit Filename after splitting document Office 2013
Novice
Filename after splitting document
 
Join Date: Dec 2015
Posts: 3
Julian is on a distinguished road
Default

Thank you very much for the quick reply!!!

I tried the code and it is on a good way. For the first document that it creates it works, but unfortunately only for the first dokument. All other documents were just numbered but didn't had the first line as document title.

Any ideas?

thank you very much for your support!
Reply With Quote
  #4  
Old 12-07-2015, 04:45 PM
macropod's Avatar
macropod macropod is offline Filename after splitting document Windows 7 64bit Filename after splitting document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

See the Split Merged Output to Separate Documents topic in the Mailmerge Tips and Tricks 'Sticky' thread, at:
https://www.msofficeforums.com/mail-...ps-tricks.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 12-08-2015, 01:56 AM
Julian Julian is offline Filename after splitting document Windows 7 64bit Filename after splitting document Office 2013
Novice
Filename after splitting document
 
Join Date: Dec 2015
Posts: 3
Julian is on a distinguished road
Default

Thank you for your help. The code that you provided splits the document by sections, but I need to split it by a delimiter. I tried to combine the two codes but I just don't get it work

Do you know how I would have to edit my original code (see above) to save the documents with the first textline as FileNames, OR how I would have to edit your code the split the documents by a delimiter?
Reply With Quote
  #6  
Old 12-08-2015, 04:51 AM
macropod's Avatar
macropod macropod is offline Filename after splitting document Windows 7 64bit Filename after splitting document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Graham's macro works correctly for the scenario you described. Evidently, though, your notes do not always have meaningful content as the first paragraph. Indeed, your description in post #3 suggests only the first note is formatted as you described.

The macro in the link I gave you also shows how to get the first paragraph's text, but adds processing to remove any illegal filename characters before trying to save. You could incorporate however much of that process you want into your original code. The other difference between my filename code and Graham's is that his also adds the Note # to the filename. Of course, all of this is moot if your notes don't conform to your specifications.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Splitting Word Document based on line content TwiceOver Word VBA 23 10-12-2015 02:01 PM
Document Filename Generator for Referencing john.adams Word 10 03-26-2013 12:14 AM
Document splitting MsLavigne Word 2 05-09-2012 05:52 AM
Filename after splitting document Save Filename using Document Text Knawl Word 11 10-10-2011 03:00 AM
Filename after splitting document WORD 2003 Need help splitting a HUGE Document dlawson Word 4 04-14-2009 12:22 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:11 PM.


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