Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
alisam - 26 Oct 2007 04:16 GMT
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
Doug Robbins - Word MVP - 26 Oct 2007 11:09 GMT
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

Signature

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

>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?
[quoted text clipped - 28 lines]
>    Selection.Copy
> End With
alisam - 27 Oct 2007 23:35 GMT
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)

> 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
[quoted text clipped - 51 lines]
> >    Selection.Copy
> > End With
Doug Robbins - Word MVP - 28 Oct 2007 00:10 GMT
Yes, that was a typo (and my eyesight ain't what it used to be).

Signature

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

> The macro was great, thank you very much.
> PS I changed this line:
[quoted text clipped - 64 lines]
>> >    Selection.Copy
>> > End With

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.