Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MACRO/VBA IN WORD 1

Status
Not open for further replies.

fusion786

Technical User
Nov 11, 2001
15
GB
Hi.

I have one big word file that contains many mini reports. What I need to be able to do is split that file into the various reports and files.

I can not do this manually as there are hundreds of reports on this file.

There is a common field that identifies a change in report.
There is a header for each report that is the same throughout.

I would like to know if there is a macro that can be used to split the reports into different workbooks and save the reports automatically in a specified folder using the common field to identify the report as the filename.

If anyone can help with this it would really be appreciated.

Thanks.
 
One thing I forgot to mention was that I would like if possible to have the output files saved in pdf format but is not essential if it is difficult to do.

 
Hi fusion786,

I am not sure if this will help with a solution for your specific needs, but it may help you towards achieving the result you want.

1. Here is some code I have copied because I thought it was good, which saves every page of a document as a separate document:

=============
Sub splitter()
'
' splitter Macro
' Macro created 16-08-98 by Doug Robbins to save each page of a document
' as a separate file with the name Page#.DOC
'
Selection.HomeKey Unit:=wdStory
Pages = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
Counter = 0
While Counter < Pages
Counter = Counter + 1
DocName = &quot;Page&quot; & Format(Counter)
ActiveDocument.Bookmarks(&quot;\Page&quot;).Range.Cut
Documents.Add
Selection.Paste
ActiveDocument.SaveAs FileName:=DocName, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:=&quot;&quot;, _
AddToRecentFiles:= True, _
WritePassword:=&quot;&quot;, ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ActiveWindow.Close
Wend

End Sub
================

2. Here is a URL for some code which saves each section of a mailmerge document as a separate file:


3. Here is another approach from a newsgroup to doing a similar thing:


At least these may give you some ideas. From what you have said I would be inclined to try and make your current document have each report separated by a section break. If you are able to do this then you would probably be able to implement suggestion 2 without too much trouble as each page in a mailmerge is a section break.

Good Luck!

Peter Moran
Two heads are always better than one!
 
Hi Peter

Thanks for that!

Suggestion 1 works better for me as it saves the files automatically though I need to work out how to get the files to save by report name rather than page10, page11, etc.

The problem with this method is that it does not keep the page layout format exactly as the master word file and I have some reports that go over one page so these reports get split over various files.

I need to be able to work out on how to split the reports by a key &quot;field&quot; at the start of each report.

But you have started me off which is great!

Any further tips would be very much appreciated.

Thanks once again.
 
Hi fusion786,

As I suggested previously, is it possible to modify your basic document so that you insert a section break at the end of each report. This would mean you could virtually use the second suggestion as is. This may well be possible with a Find and Replace.

Good Luck!

Peter Moran
 
Hi Peter

I tried inserting section breaks at the end of each report but this is very time consuming as there are abut 200 reports. Then each report would need to be saved manually.

The first suggestion works fine except that the report splits at each page and the filename it uses to save is like page10, page11. I need to be able to split the report at each change in name and use the report name as the filename and save it to a specific directory.

If anyone can help that would be great.

Here is the code as it stands:


Sub splitter()
'
' splitter Macro
' Macro created 16-08-98 by Doug Robbins to save each page of a document
' as a separate file with the name Page#.DOC
'
Selection.HomeKey Unit:=wdStory
Pages = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
Counter = 0
While Counter < Pages
Counter = Counter + 1
DocName = &quot;Page&quot; & Format(Counter)
ActiveDocument.Bookmarks(&quot;\Page&quot;).Range.Cut
Documents.Add
Selection.Paste
ActiveDocument.SaveAs FileName:=DocName, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:=&quot;&quot;, _
AddToRecentFiles:= True, _
WritePassword:=&quot;&quot;, ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
ActiveWindow.Close
Wend

End Sub

Peter, thanks for your suggestions so far.

Regards
 
Hi fusion786,

It seems to me that you need to have some unique way in your Word document to be able to identify the end of each report. Pages obviously won't do as some reports have more than one page. The logical way to break any document up is as occurs in the Mailmerge, where each page is made a separate section. (I stand to be corrected but I suspect trying to identify report name Fields may be a more difficult approach.)

Once you do that, and it may take some initial effort then you are in a position to proceed where you need to break the document up at each section break.

To insert section breaks go to the end of the first report, and then select Insert, Break, Section Break - Next Page. Then scroll to the end of the next report and just press F4 (to repeat the last command). Scroll and press F4 to insert the break after each report.

With a little help from some of the VBA helpers who usually assist on the forum it would not seem to be too difficult to change your current macro to operate on sections.

Good Luck!
Peter Moran
 
Hi Peter

Thanks for that. I may have to go down the route you suggest to insert section breaks as I am struggling to do it they way I intended to.

I have some coding that will search for each change in the file but it looks for a change in heading but all the headings in my reports are the same. I need to be able to amend the coding so that it searches for a change in a particular &quot;field&quot;.

this is the coding:

Sub EditFindLoopExample()
'This example inserts &quot;Tip: &quot; at the beginning of
' every paragraph formatted with the Heading 3 style.
With ActiveDocument.Content.Find
.ClearFormatting
> .Style = wdStyleHeading3
> 'The Do...Loop statement repeats a series of
> ' actions each time this style is found.
> Do While .Execute(Forward:=True, Format:=True) = True
> With .Parent
> 'If the found text is the last
> ' paragraph in the document...
> If .End = ActiveDocument.Content.End Then
> .StartOf Unit:=wdParagraph, Extend:=wdMove
> .InsertAfter &quot;Tip: &quot;
> Exit Do
> 'If the found text is *not* the last
> ' paragraph in the document...>
> Else
> .StartOf Unit:=wdParagraph, Extend:=wdMove
> .InsertAfter &quot;Tip: &quot;
> .Move Unit:=wdParagraph, Count:=1
> End If
> End With
> 'Goes back to the beginning of the Do...Loop statement.
> Loop
> End With
> End Sub


Any suggestions on this welcome please.

Thanks again.
 
Hi fusion786,

You say you have a field which identifies each report. Does the field only appear once for each report? Where is the field in relation to the start of the report?

If it is at the start of each report then you can use Find to locate each occurrence of the field, and when you find it you could insert your section break. To use Edit Find on fields you must first display the field codes using Select All (Ctrl+A) and then Shift+F9, and then you can search on them like any other text in the document. When finished use Shift+F9 the return to hiding the field codes.

To search and find text/fields in a document use Ctrl+F the first time and subsequently use Shift+F4 to repeat the last Find.

Good Luck!
Peter Moran
 
Hi Peter

The heading and the centre appear on each page rather than only at the beginning of the report.

But I will try and insert section breaks at the end of each report.

Thanks for your on-going suggestions.

If you think of anything else please let me know.

Regards
 
Hi PeterMoran,

I like the script that you posted as it does exactly what I need. I have a 25 page document that I need to split into 25 1-page documents.

However, while your function does what I need, I am unsure of how to run it. I have written scripts before to manipulate Word and Excel files but always using vbscript and I run them using windows script host.

How do I run the script that you posted above??

Mighty
 
Forget that question that I posted - a simple read of the Word Help and everything is simple.

Thanks.

Mighty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top