PDA

View Full Version : Splitting Word Files



LordOctane
11-07-2007, 04:24 AM
I am looking to split a large word file into seperate word documents. I have already got code from this great site to split the documents, however, all the new documents have no formatting. All I want is for the the new documents to have the same bold formatting that is in the org. doc.

If any one can help I would be extremely grateful.

Below is the code that I am using.


Put this code In a standard module:

Option Explicit

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 "///", "Notes "
End Sub

Edit by Lucas: VBA Tags added to code

lucas
11-07-2007, 07:49 AM
Charlie has pm'd me about this and I just don't have an extra minute to spare right now. Maybe one of the regulars could take a look with him.

This was a kb entry that is comprised of code that vbaexpress member Norie contributed to a thread and had no desire to post to the kb so I posted it so it would be available to all....

Link to the kb entry: http://vbaexpress.com/kb/getarticle.php?kb_id=922

Charlie has indicated that besides losing the bold formatting it also saves the files as .rtf... I have not had that problem and don't understand why unless he has Word set up to save all files as .rtf as default.

Norie
11-07-2007, 08:26 AM
LordOctane

Can you attach a sample document?

Steve mentions rtf, is the original document rtf?

lucas
11-07-2007, 08:34 AM
LordOctane,
I edited your first post....if you post code...select the code and hit the vba button and it will be formatted as in the vbe...

LordOctane
11-07-2007, 09:20 AM
Lucas - Thanks for updating the post. I use the site but I have never posted on it before.

TonyJollans
11-07-2007, 11:11 AM
Formatting will be lost. Split is a text function and it works on the default property of the Range (Text or Value, I forget which but it makes no difference at the moment).

I don't know any reason for saving as rtf - the code doesn't do it.

Norie
11-07-2007, 01:14 PM
Charlie

Tony is right - the code will not preserve any formatting.

And if what you are dealing with is an rtf file I'm not sure how you can get round that.

It's probably possible to preserve the formatting of a Word document with a little bit extra or different coding.

fumei
11-07-2007, 02:02 PM
If you are using Split, you are stuck, as it will not retain format.

Can you tell us a bit more?

Is ALL the text bold?
What is with the RTF? Is the original RFT?

Are you using Styles? I you are dumping your chunks into a template with known styles, then you could simply use them.

LordOctane
11-12-2007, 03:10 AM
Sorry for the delay in getting back to you all but I have been away from the real world and technology for a few days. I have attached a sample of the document I am trying to split. The document comes to me as an rtf file. I tried saving that file as word document but it still does not work. I just need to retain the bold as you see it in the document.

I have added "End" to each doc to help indicate where I want to spilt them.

Thanks again for all the help.

Charlie

TonyJollans
11-12-2007, 04:19 AM
Try this ...

Sub LordOctane()

Dim R As Range
Set R = ActiveDocument.Range.Duplicate

Application.ScreenUpdating = False

With R.Find
.Text = "^13UCD*^12"
.MatchWildcards = True
While .Execute
CopyAndSave R
Wend
End With
R.Collapse wdCollapseEnd
R.End = R.Parent.Range.End
CopyAndSave R

End Sub
Static Sub CopyAndSave(R As Range)
Dim D As Document
Dim Count As Long
Count = Count + 1

R.Copy
Set D = Documents.Add
D.Range.PasteAndFormat wdFormatOriginalFormatting
D.SaveAs R.Parent.Path & Application.PathSeparator & _
"Part" & Count, wdFormatDocument
D.Close

End Sub

It should just create (normal Word) documents called Part1, Part2, etc in the folder where you have the original.

LordOctane
11-12-2007, 04:29 AM
Tony,

You are a genius. That will save me so much work and make me look like I actually know things about computers - which could be a dangerous thing.

Seeing, as I am not a genius can I just check what part of the code I would need to change if the documents finished with something else other than "End"

Thanks again,

Charlie

TonyJollans
11-12-2007, 04:37 AM
Hi Charlie - the critical part is .Text = "^13UCD*^12"

This is (not foolproof but) looking for ^13 (paragraph mark) UCD (literal text as in your heading) * (followed by anything else until) ^12 (page break).

So your End doesn't come into it, so to speak :)

If you didn't have page breaks you would have to explicitly specify something unique that was there instead.

LordOctane
11-12-2007, 04:50 AM
Hi Tony,

What would you suggest would be the easiest thing to add to the end of the document that I want to split and what would the code be?


Best,

Charlie

TonyJollans
11-12-2007, 05:10 AM
Does the code I posted work without you doing anything?

If not, can you post a sample document unedited by you?

LordOctane
11-12-2007, 05:15 AM
Hi Tony,

The code works perfectly. I just wanted to be sure that if the document was any longer that the one I posted or slightly different would it affect the splitting process. Don't want to be back annoying you later on.

Best,

Charlie

TonyJollans
11-12-2007, 06:06 AM
A longer document shouldn't make any difference - clearly changed content could. I don't know the process that creates the file but my guess is that if it works nw it will carry on working - the page break at the end will probably always be there and the danger, if any, is that extra ones may be added. Another possibility is that the heading may change or may be one of several possibles of which only one exists in the test data - you tell me :)

If anything does change, do feel free to come back.

LordOctane
11-12-2007, 07:07 AM
Hi Tony,

Thanks for all your help and to everybody else that chipped in.

Keep up the great work.

Best,


Charlie