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
317 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
  • 3
  • 3
7 Comments
 
LVL 16

Accepted Solution

by:
robinu 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
 

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 16

Expert Comment

by:robinu
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
 

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
 

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Winforms Signing 13 32
Word 2010 - Review Pane setting is not persisting 4 24
Name Space error VS2015 1 25
abstract class C# 1 31
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

829 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