Solved

Send Word Doc using VBA

Posted on 2013-06-18
6
552 Views
Last Modified: 2013-06-19
Hi,
I can send the active document simple enough, but I am looking for the active document to be sent to certain recipients without the Outlook program asking me.
I also don't want to save the document after emailing the edited version when closing.

I hope this can be done with VBA and I would be grateful if anyone could point me in the right direction. I have looked through this site but they all seem to want me to save the document before sending.

Thanks
0
Comment
Question by:Stephen Byrom
  • 3
  • 2
6 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 39256403
Hi,

As far as I know, it is not possible to send a Wod doc as Attachment without saving it

The code could save it, send it and then delete the document

Regards
0
 
LVL 76

Expert Comment

by:David Lee
ID: 39257282
Hi, olorin57.

I can help, but to do so I first need to make sure I understand what you want.  You want to send the contents of the active document to a set group of recipients with the click of a button.  Once the message is sent you want to close the active document without saving it.  Do I have it right?
0
 
LVL 1

Author Comment

by:Stephen Byrom
ID: 39257340
Yes BDF that's correct. I have some code behind a button already that clears the relevant cells in a table but I cannot attach it as it is a banned format for attachments.
I am sending the document without the coding behind the button.

Regards
Plant2Downtime.docx
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 39257610
This should get the job done.  Add this subroutine to the code you already have.  Edit the code per the comments I included in it.  Add a command to call it from the subroutine you run when you click the button.

The code copies the current document to the clipboard, creates a pre-addressed email and pastes the contents of the clipboard into it.  Finally, it sends the email then closes the active document without saving it.

Sub SendCurrentDocument()
    '--> Create some constants
    'On the next line edit the list of recipients the message goes to
    Const MSG_RECP = "john@company.com; sally@company.com"
    'On the next line edit the message subject
    Const MSG_SUBJ = "Some Subject"
    Const olMailItem = 0
    
    '--> Create some variables
    Dim olkApp As Object, olkSes As Object, olkMsg As Object, olkDoc As Word.Document, wrdRng As Word.Range
    
    '--> Copy the active doccument
    Set wrdRng = Application.ActiveDocument.Range
    wrdRng.Select
    wrdRng.Copy
    
    '--> Connect to Outlook
    Set olkApp = CreateObject("Outlook.application")
    Set olkSes = olkApp.GetNamespace("MAPI")
    olkSes.Logon olkApp.DefaultProfileName
    Set olkMsg = olkApp.CreateItem(olMailItem)
    Set olkDoc = olkMsg.GetInspector.WordEditor
    
    '--> Create a message, paste the contents of the active document into it, then send it
    With olkMsg
        .To = MSG_RECP
        .Subject = MSG_SUBJ
        olkDoc.Windows(1).Selection.HomeKey wdStory, wdMove
        olkDoc.Windows(1).Selection.Paste
        olkMsg.Send
    End With
    
    '--> Disconnect from Outlook and dispose of all objects
    olkSes.Logoff
    Set olkDoc = Nothing
    Set olkMsg = Nothing
    Set olkSes = Nothing
    Set olkApp = Nothing
    Set wrdRng = Nothing
    
    '--> Close the active document without saving it
    Application.ActiveDocument.Close SaveChanges:=False
End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:Stephen Byrom
ID: 39258366
Fandabbydozy!!
Perfect!
Thanks for your help BDF.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 39258845
You're welcome!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
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 …

821 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