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
312 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now