Solved

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

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Check out this infographic on what you need to make a good email signature that will work perfectly for your organization.
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.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

914 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

19 Experts available now in Live!

Get 1:1 Help Now