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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pulling sections of word documents with vb 1

Status
Not open for further replies.

dmodele

Technical User
Jul 30, 2002
19
US
Would anyone know how to slice up a word document into labeled major sections and then use vb to pull one of the sections into copy memory for use somewhere else?
 
More specs are need. What do you mean by "major sections"? Are you talking about "major section" as defined by a content experty, or section as defined by format?

In any case, this is fairly easy to do. Post a bit more details.

1. The document already exists?
2. Are there any bookmarks to identify the "major sections?
3. Are the section breaks already in the document?
4. Are the breakpoints (i.e the start of the "major sections") consistent? Do they start with a repeatative piece of text? Do they start with a particular heading style...or any Style at all. Are they high enough Heading Styles to be able grab them in Outline mode.

But certainly the answer is Yes. Word can do what you want. However, specifics are needed to suggested real code to do it.

Better user specs, better software.


Gerry
 
I didnt want to post a longer post that would have been passed over so I tried to simplify.. maybe too much.. ok, here is a better description:

Lets say I have an existing Word document that was typed in manually. Lets say its a resume with 3 sections labeled

Education
Experience
Summery

Each section contains text organized in bullets, some bolded text, italics, font changes, maybe other formatted stuff.

From a VB module, I want to tell word to open the document and pull the entire section titled "Education" with all its formatting and content into copy memory so that I could paste it into another document.

Someone told me that I could do this with XML but it doesnt seem like its right. Does this make it easier to understand?
 
I just reread your post, fumei...

To answer you questions, there are no section breaks or bookmarks. I guess what I am asking is how to add breaks like that. What should I use? What if I wanted to use repetative text or a tag like

<Education>

blah blahb stuff formword

</Education>

How could I select everything between those tags?

 
Do not think tags. That is HTML.

There are two methods to doing this.

1 Use Bookmarks. Go through the document. Select everything from the start of Education, to the JUST BEFORE the next Education. Go Insert > Bookmarks and then give that bookmark a name - say Ed1. Repeat for the next "section" - from the start of Education to the character JUST BEFORE the next one. Set as a bookmark (Ed2).

Once you have all the chunks of text set as bookmarks you can grab them. The important concept here is that bookmarks not only mark a location, they mark a RANGE. That range can be a huge chunk of text.

Next, run code like this:

You did not state if there are more than one Education chunk per document. You did not mention that, if there is one, you are trying to extract one chunk per file, from a bunch of files. That is why we need details to really try and help. If it is ONE chunk per file, through a bunch of files, you will need some Dir logic to loop through a bunch of files in a folder. See my FAQ on FormFields for some file looping ideas.

This extracts mulitple Education chunks from the SAME file. If it is one per multiple file, you would have to adjust accordingly. This extracts multiple chunks into a new document.

Essentially, if it is one chunk in multiple files, you would use Dir to check a specified folder for .doc file. Open each each file and check for EDx bookmarks. if they exist, extract it, and copy to a nwe doc.


[code}Sub ExtractBookmarks()
Dim aDoc As Document
Dim NewDoc As Document
Dim mBookmark As Bookmark
Dim i As Integer
Dim var
i = 1
Set aDoc = ActiveDocument
Application.Documents.Add
Set NewDoc = ActiveDocument

aDoc.Activate

For Each mBookmark In aDoc.Bookmarks()
If mBookmark.Name = "Ed" & i Then
mBookmark.Select
Selection.Copy
NewDoc.Activate
Selection.Paste
' the next could be a section break rather than
' a page break if that would work better
Selection.InsertBreak Type:=wdPageBreak
aDoc.Activate
i = i + 1
End If
Next
Set NewDoc = Nothing
Set aDoc = Nothing
End Sub[/code]

This easily be adjusted to save the new doc file, or whatever.

2. If the word Education is by itself, and it is using a proper unique style, then loop throug the paragraph collection, and make a selection based on the analysis of each paragraph style. If the style is the one you want, move the selection to the start of the paragraph, and set a Range object with that start. Continue on until you find the next instance of the style. Make the End of the Range object that Style Start - 1 (i.e. just to the left of the Style Start. Select that range. Copy and paste to new document.

Anyway, here is a start. With more speciifcs this could, I'm sure be tuned to cover your need.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top