Solved

Send Word Doc using VBA

Posted on 2013-06-18
6
497 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 48

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
Highfive Gives IT Their Time Back

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
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.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

707 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