Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook refuses "mailto:" over 255 characters. 1

Status
Not open for further replies.

Petemush

Technical User
Jun 21, 2002
255
GB
Hi,

I've posted a thread concerning this before but I didn't really get an answer and now I've narrowed down the problem.

When I use ShellExecute to create a new message in Outlook, I pass it a string variable that contains "mailto:" and whatever email addresses have been added to that variable.

Sometimes the variable will have enough addresses in it to be over 255 charcters long. When you check the debug window, you can't see past 255 characters in the value part of the variable but a debug.print statement in the immediate window confirms that the data is all present and correct.

However, when I pass this string to Outlook, it only accepts 255 characters of the string, so not all of the addresses appear in the To: part of the new message window and the last address that appears is usually cut off half way through.

I thought that perhaps the To: textbox has a limit of 255 characters but it's possible to manually carry on typing into it and finish off the addresses.

This, as you can imagine, is my problem. Does anyone know why this is happening? Has anyone had a similar problem? I'm pretty sure that this shouldn't be happening and my code doesn't seem complex enough to cause an unexpected error, just incase it is causing it though, I present it below:

Private Sub frmMassEmailButton_Click()

'qdf - Used to represent the query qryMassEmail.
'rs - Used to represnt the recordset generated by qryMassEmail.

Dim db As DAO.Database, rs As DAO.Recordset, qdf As DAO.QueryDef

'sText - Used to hold the "mailto:" text string.
Dim sText As String

Set
db = CurrentDb
Set qdf = db.QueryDefs("qryMassEmail")

'Set the three parameters in the query.
qdf![Forms!frmMassEmail!CCode] = Forms![frmMassEmail]![CCode]
qdf![Forms!frmMassEmail!CoDate] = Nz(Forms![frmMassEmail]![CoDate], "*")
qdf![Forms!frmMassEmail!Status] = Nz(Forms![frmMassEmail]![Status], "*")

Set rs = qdf.OpenRecordset()

With rs
Do While Not rs.EOF

'If this is the first email address...
If Len(sText) = 0 Then

'...If an Email Address does exist and it's not equal to "Not Set"...
If Len(![EMail Address]) And Not (![EMail Address] = "Not Set") Then

'...add the first address to the string.
sText = "mailto:" & ![EMail Address]
End If

'Otherwise this is not the first email address to be added.
Else

If Len(![EMail Address]) And Not (![EMail Address] = "Not Set") Then

'Add the new address on to the end of the sText.
sText = sText & "; " & ![EMail Address]
End If
End If


'Move to the next record in the recordset.
.MoveNext
Loop
End With


'If sText has email addresses...
If Len(sText) Then

'...launch default e-mail program
Call ShellExecute(Me.hwnd, "open", sText, vbNullString, vbNullString, 0)

'If the user didn't enter a status of an employee to search for...
ElseIf IsNull(Me.Status) Then
MsgBox ("There are no employees with any status") & _
" for this course on " & Nz(Me.CoDate, "any date")
Else
MsgBox ("There are no employees with status '" & Me.Status) & _
"' for this course on " & Nz(Me.CoDate, "any date") & "."
End If
End Sub




Here's ShellExecute, I believe I got i off this site somewhere, possibly in a FAQ.


Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long


Hope somebody can help, thanks a lot.

Cheers,

Pete
 
Pete, I am the MAN!! I knew the problem was the lpFile variable was limited to 255 char. but I was curious as to whether I could get around that. Well, I couldn't but luckily there are lot's of smarter people than me out there in the world and I found the exact answer you need. Vist and you will achieve nirvana. As a side note, you could use the Outlook object model and create the message, add the addresses and send it all in the background. If you are interested set a reference to Outlook and browse around the object model, it's pretty self-explanatory.
 
You are indeed the man! Helped me out with plenty this week! Cheers for finding this, only thing is that instead of a new message being displayed ready for sending, it's treated like a saved received email and gives you the options to reply or forward which isn't what I need because you can't reuse the addresses either way.

I don't think this should happen since the screen shots on the page show a new message with all the addresses inserted in the To: part of the message. Any ideas?

Failing that, I think I'm going to have a go with the Oulook object model, but I don't really want to send the email in the background because the message has to be typed so I still want outlook to open with a new message but not sure how to do this with the model, do you know how?

Thanks a lot, as I've previously mentioned, you are indeed the man!

Cheers,

Pete
 
Pete,
I was a little full of myself last Fri. I am but a simple serf in the legions of mediocrity.
I didn't even go back to look at the code on the page I pointed you to because I already had this snippet of VBA to open Outlook with a message displayed. It's real simple but should get you started. If you have further questions let me know.

Public Sub OpenOutlookWithMessage()

Dim oOutlook As Outlook.Application
Dim oMsg As Outlook.MailItem

Set oOutlook = New Outlook.Application
Set oMsg = oOutlook.CreateItem(olMailItem)
With oMsg
.Body = "Test"
.Subject = "What's Up"
.To = "peter.lake@verizon.net"
.Display
End With


End Sub
 
Thanks, that is perfect.

Do I have to check whether Outlook is already open though? And then assign the open instance of Outlook to the variable oOutlook?

Cheers Peter, you've managed to solve the two major bugs I had with my code (thread705-326776)! I would say you can promote yourself out of the legions mediocrity now!

Cheers,

Pete
 
Pete,
I believe if Outlook is already running the code above will simply use the open instance, but I am not sure. I know only one instance of Outlook can be running on a machine (unlike Word, Excel, etc.) so this may generate an error instead. Easiest way to find out is to open Outlook and try it. Let me know which way it went, will you? Thanks.
 
Seems to be fine if Outlook is already running, doesn't try to open another instance and no error appears, everything works great!

Cheers for all your help,

Pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top