Solved

Send Word Doc using VBA

Posted on 2013-06-18
6
538 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 49

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

809 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