S
sirbanks
Guest
HELP! I'm having a problem with creating an e-mail through code from Access, then attaching the file with a name. The problem is that on SOME computers, when the e-mail opens, the attachment is there, with all the data, but has no name. It shows in the e-mail window as "untitled", but there is actually no name at all. I've tried everything but cannot find why this happens on certain computers only. My experience has been that it works virtually all the time, but when it fails on a machine, it will never work. It doesn't matter what you uninstall or reinstall or what.
The code is below. If anyone can help as to why this may be occurring, I will be forever indebted!
Thanks in advance,
Mike Freeman
Private Sub Command265_Click()
On Error GoTo Err_Command265_Click
Dim SFileData As String
Dim Track As String
If Field = 0 Then
' Sets the date of dispatch
DatePromised = Date
' Sets the field dispatch button to value -1.
Field = -1
DoCmd.DoMenuItem acFormBar, acRecords, acSaveRecord
End If
If IsNull(DatePromised) Then
' Sets the date of dispatch
DatePromised = Date
' Sets the field dispatch button to value -1.
Field = -1
DoCmd.DoMenuItem acFormBar, acRecords, acSaveRecord
End If
SFileData = "c:\windows\temp\" & Me.ServiceRecordID & ".txt"
Track = Me.ServiceRecordID & ".txt"
DoCmd.TransferText acExportDelim, , "Field Service Export Query", SFileData, True
SendMAPIMessage SFileData, Track
DoCmd.SetWarnings False
'Kill SFileData
DoCmd.SetWarnings True
DoCmd.RunCommand acCmdSaveRecord
' Sets the email confirmation.
ETrac = "This call has been e-mailed."
Exit_Command265_Click:
Exit Sub
Err_Command265_Click:
MsgBox Err.Description
Resume Exit_Command265_Click
End Sub
__________________________
After I've created the file and name of the file, I then create the message as follows:
Sub SendMAPIMessage(StrSFileData As String, Tracks As String)
Dim MapiSession As Object
Dim MapiMessage As Object
Dim MapiRecipient As Object
Dim MapiAttachment As Object
Dim MapiRecipColl As Object
Dim ObjOLApp As Object
Dim MyOl As Object
Dim errObj As Long
Dim errMsg
Dim cindx As Integer
Dim i As Integer
Dim NameData
Dim ContData
Dim PhonData
Dim ModData
Dim SerData
On Error GoTo MAPITrap
' Create an OutLook Session.
Set ObjOLApp = CreateObject("Outlook.Application")
Set MyOl = ObjOLApp.GetNameSpace("Mapi")
MyOl.Logon "Field Export", , True, False
NameData = Forms![Technical Support Test Form]!ServiceRecordID
ContData = Forms![Technical Support Test Form]!TSUserF & " " & Forms![Technical Support Test Form]!TSUserL
PhonData = Forms![Technical Support Test Form]!TSPhone1
ModData = Forms![Technical Support Test Form]!TSModel
SerData = Forms![Technical Support Test Form]!FixedAssetID
' Create the MAPI Session.
Set MapiSession = CreateObject("Mapi.Session")
' Log on to the session. If the ProfileName argument is omitted,
' Microsoft Exchange prompts you for the profile to use. If the
' profile name is incorrect, you will receive a runtime error.
MapiSession.Logon profilename:="Field Export", showdialog:=True
If MapiSession Is Nothing Then
MsgBox "Must first create MAPI session and log on"
Exit Sub
End If
Set MapiRecipColl = MapiSession.AddressBook(Title:="Select Recipients", forceResolution:=True, recipLists:=3)
' "recipients:=" parameter not used in preceding call
cindx = MapiRecipColl.Count
' Add a message to the Outbox.
Set MapiMessage = MapiSession.Outbox.Messages.Add
' Add the recipients of the message. Note, each recipient must be
' added separately to the Recipients collection of the Message
' object.
With MapiMessage
'Count through each recipients and adds them to message
For i = 1 To cindx
Set MapiRecipient = MapiMessage.Recipients.Add
MapiRecipient.Name = MapiRecipColl.Item(i).Name
MapiRecipient.Type = MapiRecipColl.Item(i).Type
'Resolve each recipient's e-mail name.
MapiRecipient.Resolve
Next
' Attach a file to the message.
Set MapiAttachment = MapiMessage.Attachments.Add
With MapiAttachment
.Name = Tracks 'Name of file
.Type = mapiFileData
.Source = StrSFileData
.ReadFromFile filename:=StrSFileData
.position = 2880
End With
' Assign the text, subject, and importance of the message.
.Subject = "Tracking Number " & NameData & " Opened"
.Text = "Contact " & ContData & " at " & PhonData & " regarding " & ModData & " serial number " & SerData & "." & vbCrLf & vbCrLf
.importance = mapiNormal
.DeliveryReceipt = True
' View the message in Microsoft Exchange before sending. Set
' the ShowDialog argument to False if you want to send the
' message without viewing it in Microsoft Exchange.
.Send showdialog:=True
End With
Set MapiSession = Nothing ' Clear the object variable.
Set MyOl = Nothing ' This is new for Outlook.
Set ObjOLApp = Nothing ' This is new for Outlook.
MAPIExit:
Exit Sub
MAPITrap:
errObj = Err - vbObjectError ' Strip out the OLE automation error.
Select Case errObj
Case 275 ' User cancelled sending of message.
Resume MAPIExit
Case Else
errMsg = MsgBox("Error " & errObj & " was returned.")
Resume MAPIExit
End Select
End Sub
The code is below. If anyone can help as to why this may be occurring, I will be forever indebted!
Thanks in advance,
Mike Freeman
Private Sub Command265_Click()
On Error GoTo Err_Command265_Click
Dim SFileData As String
Dim Track As String
If Field = 0 Then
' Sets the date of dispatch
DatePromised = Date
' Sets the field dispatch button to value -1.
Field = -1
DoCmd.DoMenuItem acFormBar, acRecords, acSaveRecord
End If
If IsNull(DatePromised) Then
' Sets the date of dispatch
DatePromised = Date
' Sets the field dispatch button to value -1.
Field = -1
DoCmd.DoMenuItem acFormBar, acRecords, acSaveRecord
End If
SFileData = "c:\windows\temp\" & Me.ServiceRecordID & ".txt"
Track = Me.ServiceRecordID & ".txt"
DoCmd.TransferText acExportDelim, , "Field Service Export Query", SFileData, True
SendMAPIMessage SFileData, Track
DoCmd.SetWarnings False
'Kill SFileData
DoCmd.SetWarnings True
DoCmd.RunCommand acCmdSaveRecord
' Sets the email confirmation.
ETrac = "This call has been e-mailed."
Exit_Command265_Click:
Exit Sub
Err_Command265_Click:
MsgBox Err.Description
Resume Exit_Command265_Click
End Sub
__________________________
After I've created the file and name of the file, I then create the message as follows:
Sub SendMAPIMessage(StrSFileData As String, Tracks As String)
Dim MapiSession As Object
Dim MapiMessage As Object
Dim MapiRecipient As Object
Dim MapiAttachment As Object
Dim MapiRecipColl As Object
Dim ObjOLApp As Object
Dim MyOl As Object
Dim errObj As Long
Dim errMsg
Dim cindx As Integer
Dim i As Integer
Dim NameData
Dim ContData
Dim PhonData
Dim ModData
Dim SerData
On Error GoTo MAPITrap
' Create an OutLook Session.
Set ObjOLApp = CreateObject("Outlook.Application")
Set MyOl = ObjOLApp.GetNameSpace("Mapi")
MyOl.Logon "Field Export", , True, False
NameData = Forms![Technical Support Test Form]!ServiceRecordID
ContData = Forms![Technical Support Test Form]!TSUserF & " " & Forms![Technical Support Test Form]!TSUserL
PhonData = Forms![Technical Support Test Form]!TSPhone1
ModData = Forms![Technical Support Test Form]!TSModel
SerData = Forms![Technical Support Test Form]!FixedAssetID
' Create the MAPI Session.
Set MapiSession = CreateObject("Mapi.Session")
' Log on to the session. If the ProfileName argument is omitted,
' Microsoft Exchange prompts you for the profile to use. If the
' profile name is incorrect, you will receive a runtime error.
MapiSession.Logon profilename:="Field Export", showdialog:=True
If MapiSession Is Nothing Then
MsgBox "Must first create MAPI session and log on"
Exit Sub
End If
Set MapiRecipColl = MapiSession.AddressBook(Title:="Select Recipients", forceResolution:=True, recipLists:=3)
' "recipients:=" parameter not used in preceding call
cindx = MapiRecipColl.Count
' Add a message to the Outbox.
Set MapiMessage = MapiSession.Outbox.Messages.Add
' Add the recipients of the message. Note, each recipient must be
' added separately to the Recipients collection of the Message
' object.
With MapiMessage
'Count through each recipients and adds them to message
For i = 1 To cindx
Set MapiRecipient = MapiMessage.Recipients.Add
MapiRecipient.Name = MapiRecipColl.Item(i).Name
MapiRecipient.Type = MapiRecipColl.Item(i).Type
'Resolve each recipient's e-mail name.
MapiRecipient.Resolve
Next
' Attach a file to the message.
Set MapiAttachment = MapiMessage.Attachments.Add
With MapiAttachment
.Name = Tracks 'Name of file
.Type = mapiFileData
.Source = StrSFileData
.ReadFromFile filename:=StrSFileData
.position = 2880
End With
' Assign the text, subject, and importance of the message.
.Subject = "Tracking Number " & NameData & " Opened"
.Text = "Contact " & ContData & " at " & PhonData & " regarding " & ModData & " serial number " & SerData & "." & vbCrLf & vbCrLf
.importance = mapiNormal
.DeliveryReceipt = True
' View the message in Microsoft Exchange before sending. Set
' the ShowDialog argument to False if you want to send the
' message without viewing it in Microsoft Exchange.
.Send showdialog:=True
End With
Set MapiSession = Nothing ' Clear the object variable.
Set MyOl = Nothing ' This is new for Outlook.
Set ObjOLApp = Nothing ' This is new for Outlook.
MAPIExit:
Exit Sub
MAPITrap:
errObj = Err - vbObjectError ' Strip out the OLE automation error.
Select Case errObj
Case 275 ' User cancelled sending of message.
Resume MAPIExit
Case Else
errMsg = MsgBox("Error " & errObj & " was returned.")
Resume MAPIExit
End Select
End Sub