#1
|
|||
|
|||
Split file using variable delimiter
Hi,
I was wondering if it would be possible, using VBA, to split a large file with multiple pages into smaller individual files using a similar, but not identical, delimiter that begins with a word and several other characters which will vary, for example: - Abcde 1A01BC [XYZ] Abcde 9Z87YX [FE] Abcde 4P44EE [RTEEY] "Abcde" remains constant but the rest, with the exception of the square brackets, is variable, including the number of letters between the square brackets. "Abcde" also appears in the document in the middle of other words, or at the beginning of another statement, but is never followed by a space character and a number apart from at locations at which I want to split the document. The delimiter always appears as the first thing on a page, but not on every page if that makes things easier? I would like to keep the same margins/indents/formats etc. in the new documents. Each new document needs to be saved using the full delimiter as the filename, e.g. "Abcde 4P44EE [RTEEY].doc" Any help you can offer would be appreciated. Last edited by kramer74; 08-27-2014 at 02:01 PM. |
#2
|
||||
|
||||
Does the document already have page breaks and/or section breaks that differentiate each range?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
It does contain page breaks, yes, and they do appear to be in the correct place.
|
#4
|
||||
|
||||
In that case, you could adapt the 'SplitMergedDocument' macro from: https://www.msofficeforums.com/mail-...ps-tricks.html. Simply insert the following code after 'With ActiveDocument':
Code:
.Range(0, 0).InsertBreak wdSectionBreakNextPage .Range.Characters.First.Cut With .Range.Find .ClearFormatting .Replacement.ClearFormatting .Text = "^12" .Replacement.Text = "^c" .Forward = True .Wrap = wdFindContinue .Format = False .MatchWildcards = False .Execute Replace:=wdReplaceAll End With Abcde 1A01BC [XYZ] Abcde 9Z87YX [FE] Abcde 4P44EE [RTEEY] strings constitute the first paragraph on each page, no further code changes should be needed.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
That's working, thank you!
Unfortunately there are still a couple of queries: - How do I know where the new documents are going to be saved, as they've ended up in three different folders as I've been using the macro. Using three test files the last page was missed off in all instances. One last thing, if it's possible, regarding the filenames. I have changed the following code Code:
StrTxt = ActiveDocument.Path & Application.PathSeparator & .Text & ".docx" Thanks. |
#6
|
||||
|
||||
Quote:
Quote:
Code:
StrTxt = ActiveDocument.Path & Application.PathSeparator & Replace(.Text, "Abcde ", "") & ".docx" Quote:
For i = 1 To .Sections.Count - 1 Step j to: For i = 1 To .Sections.Count Step j
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
||||
|
||||
Cross-posted at: http://www.vbaexpress.com/forum/show...on-page-breaks
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184 It is extremely rude to go posting the same questions on other forums after being given help and not even acknowledging that fact.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Quote:
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Split file y Heading1, saves in My documents | didijaba | Word VBA | 3 | 05-06-2014 05:35 AM |
Run-time error 91 object variable or with block variable not set | JUST ME | Word VBA | 4 | 03-25-2014 06:56 AM |
How to split .pst file | annabrown8812 | Outlook | 1 | 10-03-2013 04:27 AM |
Run-time error '91': Object variable or With block variable not set | tinfanide | Excel Programming | 2 | 06-10-2012 10:17 AM |
switching windows for variable file name | jillapass | Word VBA | 1 | 04-21-2012 02:59 AM |