How to automatically extract selected text to a new document

M

mturner296

I want to create an encyclopedia document full of boilerplate sections. The
user will then go through this document, identifying somehow the sections
he/she is interested in, and then "push a button" and all the sections are
extracted to a new document. Contrary to what this post title implies, I
don't want to have to individually select each piece of text and then
copy-and-paste or drag-and-drop it to the new document. I want all selected
sections to be moved all at once in one step.

A reasonable interface for the user to identify desired sections seems to be
to use a checkbox. However, I don't know how to define the scope of text
associated with that checkbox, nor how to do the actual extraction to the new
document. I'm not aware that this is a standard feature in Word, so I'm
assuming some amount of macro or post-processing will be needed. Any ideas
about how to do this?

Thanks
 
G

Graham Mayor

One possibility would be to create your document as a two column table, with
or without borders. Make the first column wide enough to accommodate a check
box in each cell. Put your texts in the cells of the right column and
protect the document for forms.

Check the required check boxed in the left column and run one of the
following macros to write the associated right column entries to a new
document.

Dim oDoc As Document
Dim oNewDoc As Document
Dim oFld As FormFields
Dim oTable As Table
Dim oRng As Range
Dim i As Integer
Set oDoc = ActiveDocument
Set oNewDoc = Documents.Add
Set oFld = oDoc.FormFields
Set oTable = oDoc.Tables(1)
For i = 1 To oFld.Count
If oFld(i).Type = wdFieldFormCheckBox Then
If oFld(i).CheckBox.Value = True Then
Set oRng = oTable.Cell(i, 2).Range
oNewDoc.Range.InsertAfter oRng.Text
End If
End If
Next i

OR **************

Dim oDoc As Document
Dim oNewDoc As Document
Dim oFld As FormFields
Dim oTable As Table
Dim oRng As Range, oNewRng As Range
Dim i As Integer
Set oDoc = ActiveDocument
Set oNewDoc = Documents.Add
Set oFld = oDoc.FormFields
Set oTable = oDoc.Tables(1)
If oDoc.ProtectionType <> wdNoProtection Then
oDoc.Unprotect Password:=""
End If
For i = 1 To oFld.Count
If oFld(i).Type = wdFieldFormCheckBox Then
If oFld(i).CheckBox.Value = True Then
Set oRng = oTable.Cell(i, 2).Range
oRng.Copy
Set oNewRng = oNewDoc.Range
oNewRng.Start = oNewRng.End
oNewRng.Paste
End If
End If
Next i
oDoc.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=""

to paste the formatted entries to a new document

http://www.gmayor.com/installing_macro.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

mturner296

Thanks, that worked great. (And thanks for the pointer showing how to
install macros, etc.) My only problem now is that I would like it to
preserve the original formatting from the boilerplate document in the new
document. It just seems to paste it into a "Normal" paragraph style. As far
as I can tell, I have the same style set active in both documents. But I am
going to try teaching myself VBA and figure it out on my own. If I fail I
will come back for help.

Thanks again,
 
G

Graham Mayor

The second of the quoted macros simply uses paste to paste from one document
to the other and should preserve the formatting.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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