Full Version: Code won't run from subform
UtterAccess Forums > Microsoft® Access > Access Forms
I have a form "frm Recs Tracked Jobs" in which there is a subform "tbl Additional Files subform" in datasheet format. I wish to run a routine (see below) to create an e-mail using info in the chosen record in the subform by clicking on a field in the subform.
Private Sub Miscemail()
On Error GoTo nofiles
Dim appOutlook As Outlook.Application
Dim ItmNewEmail As Outlook.MailItem
Dim TemplateName As String
Dim strBody As String
Dim attname As String
Dim attnameAP As String
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70 'save record before proceeding
TemplateName = "i:\ia manual\e-mail templates\drtemp1.oft"
attname = Forms![frm recs tracked jobs]![tbl additional files subform]![FullName]
Set appOutlook = New Outlook.Application
Rem Set ItmNewEmail = appOutlook.CreateItem(olMailItem)
Set ItmNewEmail = appOutlook.CreateItemFromTemplate(TemplateName)
strBody = strBody & Chr(13) & Chr(10)
strBody = strBody & Forms![frm recs tracked jobs]![HOSFirstName] & Chr(13) & Chr(13)
strBody = strBody & "Regards"
With ItmNewEmail
     .To = Forms("frm recs tracked jobs").Controls("empname").Value
     .CC = Forms("frm recs tracked jobs").Controls("Contact").Value & ";Brona Slevin"
     .Subject = Forms("frm recs tracked jobs").Controls("job").Value & " IA Inspection - Final Report"
     .Body = strBody
    .BodyFormat = olFormatHTML
    .Attachments.Add attname
    Rem .Attachments.Add attnameAP
    .ReadReceiptRequested = True
End With
Exit Sub
MsgBox "Final Report and/or Signed Action Plan is not present", vbInformation, "Cannot create e-mail"
GoTo exit_01
End Sub

When I assign this code to the "Click" event on the field in the subform, I get the following error message when I try to run it:
"A problem occured while Microsoft Access was communicating with the OLE server or ActiveX Control. Close the OLE server and restart it outside of Microsoft Access. Then try the original operation again in Microsoft Access."
The code does work if I run it manually from the VBA Module window after choosing the relevant record in the subform.
Any advice would be greatly appreciated.
You can change all the OLE code to the one line:
DoCmd.SendObject(ObjectType, ObjectName, OutputFormat, To, Cc, Bcc, Subject, MessageText, EditMessage, TemplateFile)
lso change
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.Save acForm, "YourFormName"
RunCommand acCmdSaveRecord
All of the acMenuVer70 commands have newer counterparts.
Oassume you are using automation for a reason e.g. (wanting to send properly formatted HTML emails).
I've run a small test with my own procedure and it worked as expected. Can attach a small sample app (zip it and keep it under 500kb). Have you tried to run Compact and Repair? Have you tried to import all db objects into a blank new application? BTW...you might want to set the email format first and then assign the body appropriately e.g.:

.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><BODY>" & YourContent & "</BODY></HTML>"

Good luck
Thanks for your advice.
tried the code in another sub form and it worked OK.
Othen recreated the original subform and everything now works fine.
Not sure what the problem was.
You're welcome.
Glad you got it sorted out.
Good luck on future projects!
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.