Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBA code to send mail from word not working on every computer

Posted on 2013-02-04
4
Medium Priority
?
948 Views
Last Modified: 2013-02-05
Hi Everybody,

I can't seem to get this code to work properly on 1 computer.
It works on another computer though.
References are on both PCs the same, security levels for both word and oultook are the same on both PCs.


When leaving out the 2 lines of code to add recepient it does work.


Here is the code:

            Dim objOutlook As Outlook.Application
            Dim objOutlookMsg As Outlook.MailItem
            Dim objOutlookRecip As Outlook.Recipient
            Dim objOutlookAttach As Outlook.Attachment
            Dim vrSubj As String, vrRecept As String, vrAttachmentPath As String

            Set objOutlook = CreateObject("Outlook.Application")
            

            Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
            vrAttachmentPath = ActiveDocument.FullName
            vrRecept = "blablabla@bla.bla"
            vrSubj     = "Some subject blabla"
            With objOutlookMsg
                    '############################################
                    ' On the next two lines VBa gives an error 287 user defined error
                    '############################################
                    Set objOutlookRecip = objOutlookMsg.Recipients.Add(vrRecept)
                    objOutlookRecip.Type = olTo
                    .Subject = vrSubj    
                    .Importance = olImportanceHigh  
                    If Not IsMissing(vrAttachmentPath) Then
                        Set objOutlookAttach = .Attachments.Add(vrAttachmentPath)
                    End If
                    .Display
                    
            End With
            Set objOutlook = Nothing

Open in new window

0
Comment
Question by:24Carat
  • 2
  • 2
4 Comments
 

Author Comment

by:24Carat
ID: 38850901
I Forgot to mention.

It does work if outlook is allready running.

But it should work also when Outlook is not running. (like on the other computer)
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 38854492
I would imagine you are falling foul of the outlook security model.  Try avoiding recipients and simply posting the addresses to the to field i.e.

Dim objOutlook As Outlook.Application
            Dim objOutlookMsg As Outlook.MailItem
            Dim objOutlookRecip As Outlook.Recipient
            Dim objOutlookAttach As Outlook.Attachment
            Dim vrSubj As String, vrRecept As String, vrAttachmentPath As String

            Set objOutlook = CreateObject("Outlook.Application")
            

            Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
            vrAttachmentPath = ActiveDocument.FullName
            vrRecept = "blablabla@bla.bla"
            vrSubj = "Some subject blabla"
            With objOutlookMsg
                    '############################################
                    ' On the next two lines VBa gives an error 287 user defined error
                    '############################################
'                    Set objOutlookRecip = objOutlookMsg.Recipients.Add(vrRecept)
'                    objOutlookRecip.Type = olTo
                    .To = vrRecept
                    .Subject = vrSubj
                    .Importance = olImportanceHigh
                    If Not IsMissing(vrAttachmentPath) Then
                        Set objOutlookAttach = .Attachments.Add(vrAttachmentPath)
                    End If
                    .Display
                    
            End With
            Set objOutlook = Nothing

Open in new window

0
 

Author Closing Comment

by:24Carat
ID: 38854559
Fantastic!

sometimes the strangest things have the most simple sollution
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 38854664
:)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

The core idea of this article is to make you acquainted with the best way in which you can export Exchange mailbox to PST format.
I came across an unsolved Outlook issue and here is my solution.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 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