Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Send Word Doc using VBA

Posted on 2013-06-18
6
Medium Priority
?
594 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 52

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

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 article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

722 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