Solved

How to email a doc from within word using VBA?

Posted on 2004-04-13
13
445 Views
Last Modified: 2012-03-24
How can I email a doc to a group from within Word using VBA?
0
Comment
Question by:Sparky191
  • 5
  • 5
  • 3
13 Comments
 
LVL 11

Accepted Solution

by:
Steiner earned 125 total points
ID: 10811740
With what program are you going to send the file? You could create VBA-Code to control Outlook and do the sending for you. Unfortunately, newer Outlook-Versions require you to confirm this sending. But as a starting point, you could use this:

First, import the proper Outlook-Object-Library to your project, depending on the version you wish to use.

Sub Test(sRecip As String)
    Dim objOutlook As Outlook.Application, objMail As Object
   
    Set objOutlook = CreateObject("Outlook.Application")
    Set objMail = objOutlook.CreateItem(olMailItem)
    With objMail
        .Subject = "TestMail"
        .To = sRecip
        .Send
    End With
    Set objMail = Nothing
    Set objOutlook = Nothing
End Sub
0
 
LVL 11

Expert Comment

by:Steiner
ID: 10811750
Oh, and to send a Document as attachment, you'll have to save it first and then add it to the mail:

   With objMail
       .Subject = "TestMail"
       .To = sRecip
       .Attachments.Add "e:\test\myfile.doc"
       .Send
   End With
0
 
LVL 1

Author Comment

by:Sparky191
ID: 10811839
OK thanks. I'll try check that out
0
 
LVL 7

Expert Comment

by:rosesolutions1
ID: 10847147
Or just automate word... (which has built in emailing capabilities)

Options.SendMailAttach = True
ActiveDocument.SendMail


0
 
LVL 1

Author Comment

by:Sparky191
ID: 10848924
rosesolutions1 - umm yeah that works aswell but that doesn't give me any options to set the name, subject or any of that stuff. Maybe there is, I might not have figured out how to do it. I'd prefer not to prompt the user for anything either, as I'm trying to make what I'm doing as simple as possible for the user.  Steiner that works fine. Cheers.
0
 
LVL 7

Expert Comment

by:rosesolutions1
ID: 10851856
actually, it gives all the same options - but if you have automating outlook working, then stick with that.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:Sparky191
ID: 10852527
Whats the syntax for getting the other options?
0
 
LVL 7

Assisted Solution

by:rosesolutions1
rosesolutions1 earned 125 total points
ID: 10853143
Good question Sparky191. On review, the mailenvelope object seems to be the key object - with mailenvelope.item you actually get an Outlook mailitem object, which you can attach to (etc) as usual in outlook, without the overhead of loading Outlook (if it is closed).  Below is a snippet I have used (from the help originally I think) - probably only valid in later versiions of Word though (while using Outlook works in all versions, of course).

Sub SendMail(ByVal strRecipient As String, ByVal strSubject as string)
     With Application.ActiveDocument.MailEnvelope
        '.Introduction = "Please read this and send me your comments."
        With .Item         'Return a MailItem object that you can use to send the document.
            'All of the mail item settings are saved with the document.
            'When you add a recipient to the Recipients collection
            'or change other properties these settings will persist.

            .Recipients.Add strRecipient
            .Subject = strSubject
            'The body of this message will be the content of the active document.
            .Send
        End With
    End With
End Sub

0
 
LVL 1

Author Comment

by:Sparky191
ID: 10853979
I'm using Word Xp, I have the Outlook object referenced in my project but I get the following error whe I try to use that code, rosesolutions1.

"Runtime error '430' Class does not support automation or does not support expected interface"

I has a problem with "With Application.ActiveDocument.MailEnvelope"

0
 
LVL 7

Expert Comment

by:rosesolutions1
ID: 10856129
You don't need to reference the Outlook object model (or anything else) for this code to work.

I have just switched to a clean WordXP machine (which doesn't have any dev tools or other rubbish on it) and pasted in the code from above - I do get an error, but it is just that SendMail is a reserved, non-overridable term in that version. I changed the sub name to MySendMail and it ran just fine.  

I then added a ref to the OL 10 object model - just to see if that introduced the problem, and it still runs fine.

I suggest you start a nice clean instance of word, and just test this bit...
0
 
LVL 11

Expert Comment

by:Steiner
ID: 10856759
Hi rosesolutions1,

are you sure that Outlook is not loaded during your process? I would think Word implicitly uses Outlook to create and send the mail item, as Word seems not to have the settings (POP3 etc.) necessary to do this alone.

But since Word 97 does not support this anyway, I don't have a way to be sure about this...

Greets
Steiner
0
 
LVL 7

Expert Comment

by:rosesolutions1
ID: 10857732
I am not sure which version of Word introduced the mailenvelope object - but I wouldn't be at all surprised if it was 2000.

Over the last few version Microsoft have done a lot of work getting different pieces of Office to share components (removing duplication). So you are right, that the key bits of Outlook are in fact being leveraged. However, doing it this way a lot of other Outlook stuff DOESN'T get loaded, so it is 'leaner' on memory use, and more importantly doesn't involve all the disk access that starting the bigger pieces of Outlook consumes.
0
 
LVL 1

Author Comment

by:Sparky191
ID: 11045149
Thanks all
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
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.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

706 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

15 Experts available now in Live!

Get 1:1 Help Now