Macro - select sections, unlink, copy and open new document

A

alisam

I haven't written a macro before and I can see parts of macros that I need
but don't know how to connect all of them together. Can anyone help?

I have a protected form. In separate Sections I use a REF field to link to
the Form Bookmark.

I wish to select protected Sections 10 and 11 and I found the macro below.
The full macro will need to:
1) Select Sections 10 and 11 which contain REF fields linked to bookmarks in
my Form
2) Convert the fields to Text via a ctrl shift F9
3) Copy the Sections 10 and 11
4) Open a new document and paste in sections 10 and 11 (with headers/footers)
5) The icing on the cake would be to generate the file name from 2 REF
fields which are linked to the 2 Bookmarks in the Form. The path will be
fixed and the filename will be in the format T12345_ABC.doc where T12345 is a
task no. and ABC is the client code.
6) Can I then back out any changes the macro may have made so if the
original document is accidently saved, there will be no damage done.

I can then add the macro to the tool bar, because I will then use the above
macro to generate another word document from another section i.e. section 12.

With ActiveDocument
.Range(.Sections(10).Range.Start, .Sections(11).Range.End).Select
Selection.Copy
End With
 
D

Doug Robbins - Word MVP

You do not need to unlink the fields in the source document before you
insert the text into a new document. They can be unlinked in the new
document.

Dim Source As Document, Target As Document
Dim fname As String
Dim arange As Range

Set Source = ActiveDocument
With Source
fname = .FormFields("TaskNumber").Result & "-" &
..FormFields("Client").Result
Set arange = .Sections(10).Range
arange.End = .Sections(11).Range.End
End With
Set Target = Documents.Add
With Target
.Range.FormattedText = arange.FormattedText
.Range.Fields.Unlink
.SaveAs fname
.Close
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
 
A

alisam

The macro was great, thank you very much.
PS I changed this line:
...FormFields("Client").Result
to
..FormFields("Client").Result (i.e a single dot)
 
D

Doug Robbins - Word MVP

Yes, that was a typo (and my eyesight ain't what it used to be).

--
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
 

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