split msWord Document

B

Bilal Niaz

Hi,

I want to split my single word document into multiple word documents or
images. The number of peices depend upone a particular keyword.

When ever there is a keyword all the upper portion will be saved to another
word document.

I have been tryin to do it through VB. But, still could not find a solution.

Hope to have cooperation.

Thanks,

Bilal

P.S: The word document contains chines charactors too along with english
charactors, and also the images.
 
D

Doug Robbins

Try the following (Save you document first, it has not been tested!):

Dim Source As Document, target As Document, myrange As Range, i As Long
Set Source = ActiveDocument
i = 1
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="yourkeyword", MatchWildcards:=False,
Wrap:=wdFindContinue, Forward:=True) = True
Set myrange = Selection.Range
myrange.Start = Source.Start
myrange.Cut
Set target = Documents.Add
target.Range.Paste
target.SaveAs "doc" & i
i = i + 1
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

Bilal Niaz

Hi Doug Robbins,

Thankyou very much for the help. It really helped me.

The words comming into my mind regarding your help are nothing in reward of
your effort. But, at least I would like to say "You are Great" :)

Actually, when I tried this snippet. It really worked. But, for only once.
The document which I tried to split contains many repetitions of the key word.

is it possible to keep the repeatitions up untill whole document is searched
and splitted well against the number of repeatitions of the keyword. ?

Here is my code:

Dim Source As Document, target As Document, myrange As Range, i As Long
Dim wd As New Word.Application
Dim wdd As New Word.Document
'Set wdd = New Word.Documents


Set Source = wd.Documents.Open("C:\math.doc")

'Set Source = "C:\math.doc"
i = 1
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="#@#", MatchWildcards:=False,
Wrap:=wdFindContinue, Forward:=True) = True
Set myrange = Selection.Range
myrange.Start = 0
' you suggested this line : myrange.Start = Source.Start
' but it generated error. So, I used 0 instead. Does it make any difference ?


myrange.Cut
Set target = Documents.Add
target.Range.Paste
target.SaveAs "Docs1" & i
' target.Close
i = i + 1
Loop
 
D

Doug Robbins

Source.Start should have been Source.Range.Start

Sorry about that.

Are you saying that it only acts on the first keyword and then stops?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

Bilal Niaz

Hi Doug,

Thanks very much again for the help.
Your previous code helped me and I am happy on that. My problem is to split
a word document into multiple documents depending upone a keyword. And them
those documents should be converted into images.

Here is an example.

suppose I have got a doc file which contains:
---------------------------------------------------------
A quick brown fox jumps over the lazy dog #@#
A quick brown fox jumps over the lazy dog #@#
A quick brown fox jumps over the lazy dog #@#
A quick brown fox jumps over the lazy dog #@#
---------------------------------------------------------

Now, my problem is to split the document into 4 word document and each
document will contain

----------------------------------------------------------------------------
A quick brown fox jumps over the lazy dog
----------------------------------------------------------------------------


But, in current scenerio, it becomes as

----------------------------------------------------------------------------
A quick brown fox jumps over the lazy dog #@#
----------------------------------------------------------------------------

I don't want my keyword to be the part of resulting documents.

Hereis my souce code

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="#@#", MatchWildcards:=False,
Wrap:=wdFindContinue, Forward:=True) = True
Set myrange = Selection.Range
myrange.Start = 0
myrange.Cut
Picture1.Cls
Picture1.Picture = Clipboard.GetData
Clipboard.Clear
SavePicture Picture1, strPath & Left(strPath1, Len(strPath1) - 4) &
"\doc" & i & ".Bmp"
stBar.Panels(1).Text = strPath & Left(strPath1, Len(strPath1) - 4) &
"\doc" & i & ".Bmp"
i = i + 1
Loop
End With
Source.Close

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

The above code is working well. Only I don't need the keyword part to be the
part of my resulting documents.

Hope to again have a great cooperation from you.

Many thanks & Best Regards,

Bilal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top