Solved

Code for Macro that will copy and Paste  Contents of the Active Document to a New Document and Make that new Document Active Document

Posted on 2009-05-10
7
323 Views
Last Modified: 2013-11-26
1.      I need help writing a word macro that will copy the contents of the currently active document, then create a new word document, paste the contents of the first  document into this new document and save the new document it with a pre-specified name (that will include time stamp as part of the new filename ).
2.      And make this new document active document.

I already have code (provided very kindly by another expert) that takes does the step one for me. However, for some reason it does not make the new document active document.

Sub Copy_Into_New_Document()
Selection.HomeKey Unit:=wdStory
    Selection.WholeStory
    Selection.Copy
    Documents.Add
    Selection.Paste
 MyDir = "C:\Test Folder\"
  Application.ActiveDocument.BuiltInDocumentProperties(wdPropertySubject) = "Summary " & Format(Now, "dd MMMM yyyy hhnnss")
   Application.ActiveDocument.SaveAs MyDir & " Summary " & Format(Now, "dd MMMM yyyy hhnnss")
 
End Sub

Open in new window

0
Comment
Question by:FaheemAhmadGul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 16

Accepted Solution

by:
Robin earned 500 total points
ID: 24349100
When I test it, it does have the new document as the active one (Office 2002/XP)
But you could try the following changed function:
Sub Copy_Into_New_Document()
    Selection.HomeKey Unit:=wdStory
    Selection.WholeStory
    Selection.Copy
    Documents.Add
    Selection.Paste
    MyDir = "C:\Test Folder\"
    Application.ActiveDocument.BuiltInDocumentProperties(wdPropertySubject) = "Summary " & Format(Now, "dd MMMM yyyy hhnnss")
    Application.ActiveDocument.SaveAs MyDir & " Summary " & Format(Now, "dd MMMM yyyy hhnnss")
    Application.ActiveDocument.Activate
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24352327
Just a thought. Why not just save the active document with a new name? The original will still exist.

In fact it might be appropriate to keep the originate as a template and create new documents from it.
0
 
LVL 1

Author Comment

by:FaheemAhmadGul
ID: 24359242
Thanks for you response to my message. The code I provided myself also works for me when I use it with Office 2003. In fact it was even working for me with Office 2007 but following some changes in other parts of my project, it has stopped working the way I want it to work.
Graham the reason I can not and do not want to save the active document with a new name is that my original active document is a large project file, with lots of VBA forms and code through which I write and formate things in my Active Document. After creating a large body of well formated text I want to email the text part of my ActiveDocument to some predefined email addresses using code in a VBA form in my Active Document.
When this whole thing works properly the first set of code in (Sub Copy_Into_New_Document) creates a new document, and the 2nd part (Sub Send Email) attaches this new document to an email.
This only works properly if the 2nd document after its creation becomes the active document. If it does not, than the parent ActiveDocument (which is some 6 MB in size with all the VBA forms and codes) gets attached to the email which I do not want to happen. I hope this clarifies the situation.
Sub Send_Email()
'
' Send Email Macro
'
'
Dim ol As Object 'outlook application object
Set ol = CreateObject("Outlook.Application") 'create an instance of the outlook application
 
Dim olItem As Object 'outlook mail object
Set olItem = ol.CreateItem(0) 'create an instance of an outlook mail object...the 0 indicates you are creating an emial
 
'using the email object
With olItem
    .To = "gulgee@me.com; " 'email addressee
    .Subject = "Summary"      'email subject
    .Body = "This is a Test Message"         'email message
    .Attachments.Add ActiveDocument.FullName 'full path and name of active document to attach to the email
    .Display 'show the email (user need to click the send button)
    '.Send    'sends the email (Outlook security prompt will appear)
End With
 
'clear the outlook mail object
Set olItem = Nothing
'clear the outlook application object
Set ol = Nothing
 
 
 
 
End Sub

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Robin
ID: 24361350
Why don't you 'save' the newly created document, and add that one as an attachment:

Something like this:

(you could add additional checks to see if oDoc is not nothing, etc.)
Private oNewDoc as Document
 
Sub Copy_Into_New_Document()
    Selection.HomeKey Unit:=wdStory
    Selection.WholeStory
    Selection.Copy
    Set oNewDoc=Documents.Add
    Selection.Paste
    MyDir = "C:\Test Folder\"
    Application.ActiveDocument.BuiltInDocumentProperties(wdPropertySubject) = "Summary " & Format(Now, "dd MMMM yyyy hhnnss")
    Application.ActiveDocument.SaveAs MyDir & " Summary " & Format(Now, "dd MMMM yyyy hhnnss")
    Application.ActiveDocument.Activate
End Sub
 
Sub Send_Email()
'
' Send Email Macro
'
'
Dim ol As Object 'outlook application object
Set ol = CreateObject("Outlook.Application") 'create an instance of the outlook application
 
Dim olItem As Object 'outlook mail object
Set olItem = ol.CreateItem(0) 'create an instance of an outlook mail object...the 0 indicates you are creating an emial
 
'using the email object
With olItem
    .To = "gulgee@me.com; " 'email addressee
    .Subject = "Summary"      'email subject
    .Body = "This is a Test Message"         'email message
    .Attachments.Add oDoc.FullName 'full path and name of active document to attach to the email
    .Display 'show the email (user need to click the send button)
    '.Send    'sends the email (Outlook security prompt will appear)
End With
 
'clear the outlook mail object
Set olItem = Nothing
'clear the outlook application object
Set ol = Nothing
 
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:FaheemAhmadGul
ID: 24370129
Hi Robinu:
I have tried your most recent suggestion that gives an error message at the following line.
.Attachments.Add oDoc.FullName 'full path and name of active document to attach to the email

I have however been able to get around the problem in different way. I create and save the new document and then use a UDF function to assign the name of the most recently saved file in ( C:\Test ) to a string variable and then using that variable to attach the most recently saved file from this folder to the email.
As the 1st solution suggested by you works in Office 2003 I am accepting it as the correct solution. Out of academic interest only I would appreciate if you could suggest why I get the error on line 31 of your most recent solution. Many thanks for your help.
0
 
LVL 1

Author Closing Comment

by:FaheemAhmadGul
ID: 31579928
Hi Robinu:
I have tried your most recent suggestion that gives an error message at the following line.
.Attachments.Add oDoc.FullName 'full path and name of active document to attach to the email

I have however been able to get around the problem in different way. I create and save the new document and then use a UDF function to assign the name of the most recently saved file in ( C:\Test ) to a string variable and then using that variable to attach the most recently saved file from this folder to the email.
As the 1st solution suggested by you works in Office 2003 I am accepting it as the correct solution. Out of academic interest only I would appreciate if you could suggest why I get the error on line 31 of your most recent solution. Many thanks for your help. Regards Faheem
0
 
LVL 16

Expert Comment

by:Robin
ID: 24371931
Thanks Faheem.

I see now that line #31 probably has to be:

   .Attachments.Add oNewDoc.FullName

oDoc does not exist (so is Nothing), so we were trying to attach non existing document
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question