Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-29-2013, 12:50 AM
megatronixs megatronixs is offline split word document based on bookmarks with each new document title of the bookmark Windows XP split word document based on bookmarks with each new document title of the bookmark Office 2003
Advanced Beginner
split word document based on bookmarks with each new document title of the bookmark
 
Join Date: Aug 2012
Posts: 42
megatronixs is on a distinguished road
Default split word document based on bookmarks with each new document title of the bookmark

Hi all,



I would like to create a macro that would split a word document based on the bookmarks. So if I have a word document with 3 bookmarks, I would like it to split into 3 documents with the name for the document comes from the bookmark name.
I just have no clue how to pull this one of, and if some one can help in finding a solution for this, that would really be very great ;-)

Thanks in advance and greetings ;-)
Reply With Quote
  #2  
Old 05-29-2013, 04:46 AM
macropod's Avatar
macropod macropod is offline split word document based on bookmarks with each new document title of the bookmark Windows 7 32bit split word document based on bookmarks with each new document title of the bookmark Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

What do you want to save - the bookmarked content, or content between the bookmarks?

PS: Moving this thread to the Word VBA forum, as that's what macros are.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 06-03-2013, 03:23 AM
megatronixs megatronixs is offline split word document based on bookmarks with each new document title of the bookmark Windows XP split word document based on bookmarks with each new document title of the bookmark Office 2003
Advanced Beginner
split word document based on bookmarks with each new document title of the bookmark
 
Join Date: Aug 2012
Posts: 42
megatronixs is on a distinguished road
Default

Hi, I would like to save the content between the bookmarks. What we do is, scanning a huge pile of documents and then I need to split them each part depending how many pages are in a document. The first page we give an ID number (writen with a balpoint on the paper it self). Ideal would be that we give the bookmark the ID number and then when we finish, the macro will split them so we don't have to do this manually. The best would be that they will end up in a folder that has the same name as the bookmark. So if a group of pages that forms the document would have ID 2333, then it would look for a folder named 2333 and put there that part only.

I guess that is to much to ask, but any pointing in the right direction would be a time saver for me.

Greetings.
Reply With Quote
  #4  
Old 06-03-2013, 04:32 AM
macropod's Avatar
macropod macropod is offline split word document based on bookmarks with each new document title of the bookmark Windows 7 32bit split word document based on bookmarks with each new document title of the bookmark Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Try the following macro. It should process all documents in the source folder, outputting the pages to child folders under it named according to the bookmark names.
Code:
Option Explicit
Sub SplitDocsToFolders()
Application.ScreenUpdating = False
Dim DocSrc As Document, DocTgt As Document
Dim Rng As Range, BkMk As Bookmark
Dim StrPth As String, StrNm As String, StrFld As String
'browse to a folder
StrPth = GetFolder & "\"
If StrPth = "\" Then Exit Sub
StrNm = Dir(StrPth & "\*.doc", vbNormal)
'process all files in the source folder
While StrNm <> ""
  'open the source document
  Set DocSrc = Documents.Open(FileName:=StrPth & StrNm, _
      AddToRecentFiles:=False, Visible:=False)
  With DocSrc
    If .Bookmarks.Count > 0 Then
      'find the ranges from one bookmark to the next
      For Each BkMk In .Bookmarks
        If BkMk.StoryType = wdMainTextStory Then
          'get the bookmark's name
          StrFld = BkMk.Name & "\"
          'find the range spanned till the next bookmark
          Set Rng = BkMk.Range
          With Rng
            Do While .Bookmarks.Count = 1
              .MoveEnd wdParagraph, 1
              If .End = DocSrc.Range.End Then Exit Do
            Loop
            Do While .Bookmarks.Count > 1
              .MoveEnd wdSentence, -1
            Loop
            Do While .Bookmarks.Count < 2
              .MoveEnd wdWord, 1
              If .End = DocSrc.Range.End Then Exit Do
            Loop
            Do While .Bookmarks.Count > 1
              .MoveEnd wdCharacter, -1
            Loop
            .Start = BkMk.Range.Start
            If .End > .Start Then
              'copy the spanned range
              .Copy
              'create the output folder, if necessary, using the bookmark's name
              If Dir(StrPth & StrFld, vbDirectory) = "" Then MkDir (StrPth & StrFld)
              'create the output document
              Set DocTgt = Documents.Add(Visible:=False)
              With DocTgt
                'add the content to the new document
                .Range.Paste
                'save the document to its folder, then close it
                .SaveAs2 FileName:=StrPth & StrFld & StrNm, _
                    Fileformat:=DocSrc.Type, AddToRecentFiles:=False
                .Close False
              End With
            End If
          End With
        End If
      Next
    End If
    .Close False
  End With
  'process the next document
  StrNm = Dir()
Wend
'cleanup
Set Rng = Nothing: Set DocTgt = Nothing: Set DocSrc = Nothing
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 06-03-2013, 05:20 AM
megatronixs megatronixs is offline split word document based on bookmarks with each new document title of the bookmark Windows XP split word document based on bookmarks with each new document title of the bookmark Office 2003
Advanced Beginner
split word document based on bookmarks with each new document title of the bookmark
 
Join Date: Aug 2012
Posts: 42
megatronixs is on a distinguished road
Default

Hi Paul,

Thanks a lot for above macro. do I need to give it the path to the a drive? or will it look for it in all drives? If I would have the folders in drive G:\Documents\Documents 2013. How can I let the macro know where the drive is and which folders it need to look for.

Thanks a lot ;-)

Greetings.
Reply With Quote
  #6  
Old 06-03-2013, 05:25 AM
macropod's Avatar
macropod macropod is offline split word document based on bookmarks with each new document title of the bookmark Windows 7 32bit split word document based on bookmarks with each new document title of the bookmark Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

The macro has its own browser. Simply point it to the folder containing the documents you want to process. It only processes a single folder at a time. The macro doesn't go looking for your other folders outside of that one, either. So, if you have 10 documents in G:\Documents\Documents 2013, and they all have a bookmark named 2333, you'll end up with a folder named G:\Documents\Documents 2013\2333 containing 10 new documents with just the relevant portions from the source documents.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 06-03-2013, 05:29 AM
megatronixs megatronixs is offline split word document based on bookmarks with each new document title of the bookmark Windows XP split word document based on bookmarks with each new document title of the bookmark Office 2003
Advanced Beginner
split word document based on bookmarks with each new document title of the bookmark
 
Join Date: Aug 2012
Posts: 42
megatronixs is on a distinguished road
Default

Hi Paul, there is a little thing that I have problems with, Word does not let me put comments without letters in the name. If I want to create a bookmark with only a number in it without letters, it does not let me. Maybe the macro can see that if the bookmark starts with ID, it will just skip that part when looking for a folder?

greetings.
Reply With Quote
  #8  
Old 06-03-2013, 05:57 AM
megatronixs megatronixs is offline split word document based on bookmarks with each new document title of the bookmark Windows XP split word document based on bookmarks with each new document title of the bookmark Office 2003
Advanced Beginner
split word document based on bookmarks with each new document title of the bookmark
 
Join Date: Aug 2012
Posts: 42
megatronixs is on a distinguished road
Default

Hi Paul,

I did check the macro, I let's me select the folder, but then I get an error that the object does not support this property or method. No clue what goes wrong. The folder is created but empty. Maybe I do need to add some references?

greetings
Reply With Quote
  #9  
Old 06-03-2013, 08:16 PM
macropod's Avatar
macropod macropod is offline split word document based on bookmarks with each new document title of the bookmark Windows 7 32bit split word document based on bookmarks with each new document title of the bookmark Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Since the bookmark names need to begin with a letter, you could change:
StrFld = BkMk.Name & "\"
to:
StrFld = Right(BkMk.Name, Len(BkMk.Name) - 1) & "\"

You might also need to change:
.SaveAs2
to:
.SaveAs
and change:
If Dir(StrPth & StrFld, vbDirectory) = "" Then MkDir (StrPth & StrFld)
to:
On Error Resume Next
MkDir (StrPth & StrFld)
On Error GoTo 0
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 09-05-2020, 02:29 PM
mi42 mi42 is offline split word document based on bookmarks with each new document title of the bookmark Windows 10 split word document based on bookmarks with each new document title of the bookmark Office 2016
Novice
 
Join Date: Sep 2020
Posts: 1
mi42 is on a distinguished road
Default

Hi Paul,

First of all, I would like to thank you very much for the macro.
Would it be possible to modify the macro in such a way that it also copies the content of the header and footer and removes the page breaks in the created files?


I managed to remove page breaks puting
.Range.Find.Execute Findtext:="^b", ReplaceWith:=""


between

.Range.PasteAndFormat wdFormatOriginalFormatting and .SaveAs FileName:=StrPth & StrFld & StrNm, _

Last edited by mi42; 09-06-2020 at 02:42 AM.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
split word document based on bookmarks with each new document title of the bookmark How to have the location (path) of the Word document on the title bar? Jamal NUMAN Word 24 09-06-2017 12:18 PM
split word document based on bookmarks with each new document title of the bookmark Good Word-based Document Assembly Software? Grahamers Word 1 05-13-2013 04:42 PM
split word document based on bookmarks with each new document title of the bookmark Split MailMerge document agujoa Mail Merge 3 04-08-2012 11:26 PM
Display Full File Path Name of Document in Title bar MS-Word 2010 Carlos06x Word 1 10-12-2011 10:39 AM
Bookmark to another document spqr Word 3 06-26-2009 04:51 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:29 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