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

x
?
Solved

How to email a doc from within word using VBA?

Posted on 2004-04-13
13
Medium Priority
?
454 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
[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
  • 5
  • 5
  • 3
13 Comments
 
LVL 11

Accepted Solution

by:
Steiner earned 500 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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 Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
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.
Suggested Courses

715 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