Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-27-2014, 07:59 AM
kramer74 kramer74 is offline Split file using variable delimiter Windows 7 32bit Split file using variable delimiter Office 2010 32bit
Novice
Split file using variable delimiter
 
Join Date: Aug 2014
Posts: 4
kramer74 is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 08-28-2014, 02:41 AM
macropod's Avatar
macropod macropod is offline Split file using variable delimiter Windows 7 64bit Split file using variable delimiter 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

Does the document already have page breaks and/or section breaks that differentiate each range?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 08-28-2014, 05:07 AM
kramer74 kramer74 is offline Split file using variable delimiter Windows 7 32bit Split file using variable delimiter Office 2010 32bit
Novice
Split file using variable delimiter
 
Join Date: Aug 2014
Posts: 4
kramer74 is on a distinguished road
Default

It does contain page breaks, yes, and they do appear to be in the correct place.
Reply With Quote
  #4  
Old 08-28-2014, 09:28 PM
macropod's Avatar
macropod macropod is offline Split file using variable delimiter Windows 7 64bit Split file using variable delimiter 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

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
Assuming, as you've indicated that the:
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]
Reply With Quote
  #5  
Old 08-29-2014, 02:14 AM
kramer74 kramer74 is offline Split file using variable delimiter Windows 7 32bit Split file using variable delimiter Office 2010 32bit
Novice
Split file using variable delimiter
 
Join Date: Aug 2014
Posts: 4
kramer74 is on a distinguished road
Default

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"
by removing "ActiveDocument.Path & " so that the path isn't included in the filename, so now I get the filename I want. However, is there a way to remove the first 6 characters from the filename, namely "Abcde ", as this would make it perfect.

Thanks.
Reply With Quote
  #6  
Old 08-29-2014, 03:21 PM
macropod's Avatar
macropod macropod is offline Split file using variable delimiter Windows 7 64bit Split file using variable delimiter 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

Quote:
Originally Posted by kramer74 View Post
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.
...
I have changed the following code
Code:
StrTxt = ActiveDocument.Path & Application.PathSeparator & .Text & ".docx"
by removing "ActiveDocument.Path & " so that the path isn't included in the filename, so now I get the filename I want.
What you've done is not to exclude the path from the filename, but the part that tells Word where to save the file! That part of the code tells Word to save the output file back to the same folder as the input file.
Quote:
is there a way to remove the first 6 characters from the filename, namely "Abcde ", as this would make it perfect.
You could use:
Code:
StrTxt = ActiveDocument.Path & Application.PathSeparator & Replace(.Text, "Abcde ", "") & ".docx"
Quote:
Using three test files the last page was missed off in all instances.
Try changing:
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]
Reply With Quote
  #7  
Old 08-29-2014, 03:32 PM
macropod's Avatar
macropod macropod is offline Split file using variable delimiter Windows 7 64bit Split file using variable delimiter 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

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]
Reply With Quote
  #8  
Old 09-01-2014, 12:12 AM
kramer74 kramer74 is offline Split file using variable delimiter Windows 7 32bit Split file using variable delimiter Office 2010 32bit
Novice
Split file using variable delimiter
 
Join Date: Aug 2014
Posts: 4
kramer74 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
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.
If I offended you by doing this, then I apologise. In my defence, I was on a tight deadline. The help is appreciated.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Split file using variable delimiter Split file y Heading1, saves in My documents didijaba Word VBA 3 05-06-2014 05:35 AM
Split file using variable delimiter 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
Split file using variable delimiter 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

Other Forums: Access Forums

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