Solved

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

Posted on 2013-02-04
4
937 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Find out what you should include to make the best professional email signature for your organization.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

742 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