Solved

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

Posted on 2013-02-04
4
931 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
Comment Utility
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
Comment Utility
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
Comment Utility
Fantastic!

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

Expert Comment

by:Chris Bottomley
Comment Utility
:)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Check out this infographic on what you need to make a good email signature that will work perfectly for your organization.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

763 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

13 Experts available now in Live!

Get 1:1 Help Now