?
Solved

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

Posted on 2013-02-04
4
Medium Priority
?
943 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
[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
  • 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Many people use more than one email account and so it becomes difficult for them to manage them when they use separate accounts,  so, in this article, I have shared an easy way to add Other Mail Accounts in your Google Inbox. It helps to combine all…
When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
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.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
Suggested Courses

752 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