![]() |
|
![]() |
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
![]() 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 ;-) |
#2
|
||||
|
||||
![]()
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] |
#3
|
|||
|
|||
![]()
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. |
#4
|
||||
|
||||
![]()
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] |
#5
|
|||
|
|||
![]()
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. |
#6
|
||||
|
||||
![]()
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] |
#7
|
|||
|
|||
![]()
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. |
#8
|
|||
|
|||
![]()
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 |
#9
|
||||
|
||||
![]()
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] |
#10
|
|||
|
|||
![]()
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. |
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
Jamal NUMAN | Word | 24 | 09-06-2017 12:18 PM |
![]() |
Grahamers | Word | 1 | 05-13-2013 04:42 PM |
![]() |
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 |