Solved

Send Word Doc using VBA

Posted on 2013-06-18
6
574 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
[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
  • 2
6 Comments
 
LVL 51

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

739 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