Link to home
Start Free TrialLog in
Avatar of cansevin
cansevin

asked on

Email code only working on one comptuer

I thought this was solved, but turns out it is not. This below code is only working one computer. The debug highlights the         objEmailMessage.To = Forms!MainForm.txtEmail I also tried objEmailMessage.To = Forms!MainForm.[Email]

Both only work on one computer, not the other one. Any ideas?

Set objOutlook = CreateObject("Outlook.Application")
        Set objEmailMessage = objOutlook.CreateItem(olMailItem)
        objEmailMessage.To = Forms!MainForm.txtEmail
        objEmailMessage.Subject = sSubj
        objEmailMessage.htmlBody = sBody & vbCrLf & vbCrLf
        objEmailMessage.Display
        objEmailMessage.Attachments.Add "C:\Invoices\CustomerInvoices.pdf"

Open in new window

Avatar of arnold
arnold
Flag of United States of America image

You are relying on outlook object meaning outlook must be installed and likely configured on the system.

Open outlook on the one where it does not work, are you getting promoted to create a new email account?
what is the error ?
Look at my last suggestion on the previous question!
Throw a MsgBox in there.
What do you get back?

And this is not the whole code.
Post the entire sub.
I suspect you have On Error resume Next on the go because you haven't read
https://www.experts-exchange.com/articles/17466/Properly-open-Outlook-as-an-Application-object-in-VBA.html
and there is no end of grief that On Error Resume Next can cause!
Avatar of cansevin
cansevin

ASKER

The error just says to Debug... then it highlights that line of objEmailMessage.To = Forms!MainForm.txtEmail

Nick I'll look into your other post
in that error message box, there is an error number and description of error, at least try to read the message.
Sorry... the error:

Method 'To' of object '_MailItem' failed
I'll give you a 99/100 guess
Open Outlook on both machines.
Try to run your code.
Does it succeed or bomb on both
Or is one different than the other.
I suspect both bomb.

Now ensure that Outlook is closed on both machines.
Same experiment.
Same result?
I suspect both succeed

When you try this
Set objOutlook = CreateObject("Outlook.Application")
With Outlook open already, a 429 error will occur.
Sometimes, people throw in On Error Resume Next to deal with that.
But it's not dealt with!
It's ignored
So objOutlook is nothing.
so this line is going to fail, too
Set objEmailMessage = objOutlook.CreateItem(olMailItem)
And on error resume next makes the code ignore that too.
But then, finally, it gives up the ghost and stops.
Any ideas on possible solutions?
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Any ideas on possible solutions?
You haven't really given us anything to go on yet
In #40765893 I posted some troubleshooting steps to take to see if the problem is machine-specific or is in fact related to the open-status of Outlook
In #40765776 I asked you to flange in a MsgBox to see if a valid email is being sent to the line of the debug stop.
I have speculated that you have On Error Resume Next in your routine, and asked for you to post the entire subroutine so we can look at all the possibilities.
I've posted a link to an article about how you should open Outlook, and not the manner you are presently doing it

But this was your feedback
Any ideas on possible solutions?
You haven't confirmed or denied anything I've speculated about yet, so the ball is firmly still in your court.
Do you have the olMailItem constant declared somewhere in your code?