Laurence Martin
asked on
Outlook and VBA closes message incorrectly
Hi,
My code is creating a mail item from a template, rewriting the body and then saving it as a draft.
The problem is that when the vba object for the mail item is set to Nothing the message in the drafts folder is lost and replaced with some other text.
Dim objMail As Outlook.MailItem
Set objMail = Outlook.Application.Create ItemFromTe mplate(stT emplate)
stMsgText = objMail.Body
Debug.Print stMsgText
Call FitConfirmMailText ' this populates stMsgText with text
objMail.Body = stMsgText
objMail.To = strCustEmail
objMail.Subject = strSubject
Debug.Print stMsgText
Debug.Print objMail.Body
If strApptType = "frm25leads" Then
objMail.Attachments.Add ("H:\John Lewis\Masters\quality assured (v2).pdf")
End If
objMail.Close olSave
Debug.Print stMsgText
Debug.Print objMail.Body
Set objMail = Nothing
The debug.prints all show the right text.
When the message appears in the drafts folder after objmail.close olsave it is the correct message, but as soon as set objMail=Nothing it changes to a different and incorrect body (the subject is correct). The same thing happens when the procedure finishes if I remark out the Set objMail=Nothing
How can setting the object to nothing replace the body of the item?
NB: There is other code in the procedure, but it doesn't work with objMail.
I am not using a namespace object. Is that the problem?
Cheers
My code is creating a mail item from a template, rewriting the body and then saving it as a draft.
The problem is that when the vba object for the mail item is set to Nothing the message in the drafts folder is lost and replaced with some other text.
Dim objMail As Outlook.MailItem
Set objMail = Outlook.Application.Create
stMsgText = objMail.Body
Debug.Print stMsgText
Call FitConfirmMailText ' this populates stMsgText with text
objMail.Body = stMsgText
objMail.To = strCustEmail
objMail.Subject = strSubject
Debug.Print stMsgText
Debug.Print objMail.Body
If strApptType = "frm25leads" Then
objMail.Attachments.Add ("H:\John Lewis\Masters\quality assured (v2).pdf")
End If
objMail.Close olSave
Debug.Print stMsgText
Debug.Print objMail.Body
Set objMail = Nothing
The debug.prints all show the right text.
When the message appears in the drafts folder after objmail.close olsave it is the correct message, but as soon as set objMail=Nothing it changes to a different and incorrect body (the subject is correct). The same thing happens when the procedure finishes if I remark out the Set objMail=Nothing
How can setting the object to nothing replace the body of the item?
NB: There is other code in the procedure, but it doesn't work with objMail.
I am not using a namespace object. Is that the problem?
Cheers
I can only imagine there is something unexpected in FitConfirmMailText ...can you supply a copy of that?
ASKER
Hi,
Here is the code, but I'll be amazed if that's the problem. I also attach a text file of the full code in the module. The procedure with the problem is ConfirmAppt which is called from the event procedure in the form.
Thanks for looking at this.
Sub FitConfirmMailText()
Dim stAMPM As String
Dim stDuration As String
stAMPM = AMorPM(dteStarttime)
stDuration = InputBox("How long will the fitting take?", "Fitting confirmation", "Hours")
stMsgText = "The installation will take approximately " & stDuration & " hours." & vbCr & vbCr & stMsgText
stMsgText = "Your surveyor is " & stFitter & " and he is contactable on " & stFitterMobile & ". " & stMsgText
stMsgText = "Your installation appointment has been booked and agreed for " & FindDay(dteStart) & " " & dteStart & " with an " & stAMPM & " arrival time. " & stMsgText
stMsgText = "Thank-you for your interest in S:CRAFT's plantation shutters. " & stMsgText
stMsgText = "Dear " & strCustSal & "," & vbCr & vbCr & stMsgText
End Sub modOutlook.txt
Here is the code, but I'll be amazed if that's the problem. I also attach a text file of the full code in the module. The procedure with the problem is ConfirmAppt which is called from the event procedure in the form.
Thanks for looking at this.
Sub FitConfirmMailText()
Dim stAMPM As String
Dim stDuration As String
stAMPM = AMorPM(dteStarttime)
stDuration = InputBox("How long will the fitting take?", "Fitting confirmation", "Hours")
stMsgText = "The installation will take approximately " & stDuration & " hours." & vbCr & vbCr & stMsgText
stMsgText = "Your surveyor is " & stFitter & " and he is contactable on " & stFitterMobile & ". " & stMsgText
stMsgText = "Your installation appointment has been booked and agreed for " & FindDay(dteStart) & " " & dteStart & " with an " & stAMPM & " arrival time. " & stMsgText
stMsgText = "Thank-you for your interest in S:CRAFT's plantation shutters. " & stMsgText
stMsgText = "Dear " & strCustSal & "," & vbCr & vbCr & stMsgText
End Sub modOutlook.txt
OK, it looks as though stMsgText is a global ... how/where is it defined? I am particularly interested in anywhere it is used seperately.
Would also be useful to know in what way the body is wrong ... is it different to the template for example?
Chris
Would also be useful to know in what way the body is wrong ... is it different to the template for example?
Chris
replace:
objMail.Close olSave
with
objMail.save
objmail.close 0
did you set olSave = 0 anywhere?
objMail.Close olSave
with
objMail.save
objmail.close 0
did you set olSave = 0 anywhere?
ASKER
Hi DavidT - No that makes no difference. The body changes when objMail.Close 0. I didn't set olSave = 0 but I guess that's academic now.
The following is a copy of the immediate window of the last two sets of debug.print. The underlined are my comments:
stMsgText before save and close
Dear Mrs Froehlich ,
Thank-you for your interest in S:CRAFT's plantation shutters. Your installation appointment has been booked and agreed for Wednesday 05/08/2009 with an AM arrival time. Your surveyor is Mark Duwell and he is contactable on 07884 432139. The installation will take approximately 2 hours.
Please note that we are unable to install the window if any work is being carried out on the window, window furnishings or surround. Please make sure that the floor area around the windows is cleared as much as possible for working space, and that the furnishings currently covering the windows are removed so that the shutters can be installed. It is not S:CRAFT's responsibility to remove/rehang the existing curtains/blinds and the installers have no liability cover for any non S:CRAFT products. If the installer feels that the shutters can not be installed due to this reason, the installation will have to be cancelled and re-arranged.
S:CRAFT require the person that placed the order to be available for signing off the shutter satisfaction form. This also brings the 3 year warranty into effect. If the customer is not available our installers have been instructed not to install the shutters. Should you wish the installer to install the shutters without you being present we require written confirmation prior to installation that you forfeit the right to signing off the customer satisfaction note. Your warranty will come into effect automatically.
If you feel that you can not make the shutter installation, or you need to change the date, please contact us giving 48 hours notice.
Please be aware John Lewis require the remaining balance a minimum of three working days prior to the installation, if the final payment has not been made within this time period, the installation will be cancelled and rebooked, once John Lewis can confirm that the full payment has been received S-CRAFT will book a new installation appointment.
Kind regards
Customer Services
t. +44 (0)844 2412270
objMail.body before save and close
Dear Mrs Froehlich ,
Thank-you for your interest in S:CRAFT's plantation shutters. Your installation appointment has been booked and agreed for Wednesday 05/08/2009 with an AM arrival time. Your surveyor is Mark Duwell and he is contactable on 07884 432139. The installation will take approximately 2 hours.
Please note that we are unable to install the window if any work is being carried out on the window, window furnishings or surround. Please make sure that the floor area around the windows is cleared as much as possible for working space, and that the furnishings currently covering the windows are removed so that the shutters can be installed. It is not S:CRAFT's responsibility to remove/rehang the existing curtains/blinds and the installers have no liability cover for any non S:CRAFT products. If the installer feels that the shutters can not be installed due to this reason, the installation will have to be cancelled and re-arranged.
S:CRAFT require the person that placed the order to be available for signing off the shutter satisfaction form. This also brings the 3 year warranty into effect. If the customer is not available our installers have been instructed not to install the shutters. Should you wish the installer to install the shutters without you being present we require written confirmation prior to installation that you forfeit the right to signing off the customer satisfaction note. Your warranty will come into effect automatically.
If you feel that you can not make the shutter installation, or you need to change the date, please contact us giving 48 hours notice.
Please be aware John Lewis require the remaining balance a minimum of three working days prior to the installation, if the final payment has not been made within this time period, the installation will be cancelled and rebooked, once John Lewis can confirm that the full payment has been received S-CRAFT will book a new installation appointment.
Kind regards
Customer Services
t. +44 (0)844 2412270
stMsgText after save and close
Dear Mrs Froehlich ,
Thank-you for your interest in S:CRAFT's plantation shutters. Your installation appointment has been booked and agreed for Wednesday 05/08/2009 with an AM arrival time. Your surveyor is Mark Duwell and he is contactable on 07884 432139. The installation will take approximately 2 hours.
Please note that we are unable to install the window if any work is being carried out on the window, window furnishings or surround. Please make sure that the floor area around the windows is cleared as much as possible for working space, and that the furnishings currently covering the windows are removed so that the shutters can be installed. It is not S:CRAFT's responsibility to remove/rehang the existing curtains/blinds and the installers have no liability cover for any non S:CRAFT products. If the installer feels that the shutters can not be installed due to this reason, the installation will have to be cancelled and re-arranged.
S:CRAFT require the person that placed the order to be available for signing off the shutter satisfaction form. This also brings the 3 year warranty into effect. If the customer is not available our installers have been instructed not to install the shutters. Should you wish the installer to install the shutters without you being present we require written confirmation prior to installation that you forfeit the right to signing off the customer satisfaction note. Your warranty will come into effect automatically.
If you feel that you can not make the shutter installation, or you need to change the date, please contact us giving 48 hours notice.
Please be aware John Lewis require the remaining balance a minimum of three working days prior to the installation, if the final payment has not been made within this time period, the installation will be cancelled and rebooked, once John Lewis can confirm that the full payment has been received S-CRAFT will book a new installation appointment.
Kind regards
Customer Services
t. +44 (0)844 2412270
objMail.Body after save and close
Please find attached a copy of your John Lewis Shutter quotation. We apologise for the delay in generating the quote for you.
If you wish to progress with this order please sign the copy of the quotation and place your order with your John Lewis Store. A 20% deposit of the product cost will be taken at the time of order.
Expected lead time is 8-9 weeks from the placement of your order if ordered by sea and 3-4 weeks if ordered by air.
t. +44 (0)844 2412270
The following is a copy of the immediate window of the last two sets of debug.print. The underlined are my comments:
stMsgText before save and close
Dear Mrs Froehlich ,
Thank-you for your interest in S:CRAFT's plantation shutters. Your installation appointment has been booked and agreed for Wednesday 05/08/2009 with an AM arrival time. Your surveyor is Mark Duwell and he is contactable on 07884 432139. The installation will take approximately 2 hours.
Please note that we are unable to install the window if any work is being carried out on the window, window furnishings or surround. Please make sure that the floor area around the windows is cleared as much as possible for working space, and that the furnishings currently covering the windows are removed so that the shutters can be installed. It is not S:CRAFT's responsibility to remove/rehang the existing curtains/blinds and the installers have no liability cover for any non S:CRAFT products. If the installer feels that the shutters can not be installed due to this reason, the installation will have to be cancelled and re-arranged.
S:CRAFT require the person that placed the order to be available for signing off the shutter satisfaction form. This also brings the 3 year warranty into effect. If the customer is not available our installers have been instructed not to install the shutters. Should you wish the installer to install the shutters without you being present we require written confirmation prior to installation that you forfeit the right to signing off the customer satisfaction note. Your warranty will come into effect automatically.
If you feel that you can not make the shutter installation, or you need to change the date, please contact us giving 48 hours notice.
Please be aware John Lewis require the remaining balance a minimum of three working days prior to the installation, if the final payment has not been made within this time period, the installation will be cancelled and rebooked, once John Lewis can confirm that the full payment has been received S-CRAFT will book a new installation appointment.
Kind regards
Customer Services
t. +44 (0)844 2412270
objMail.body before save and close
Dear Mrs Froehlich ,
Thank-you for your interest in S:CRAFT's plantation shutters. Your installation appointment has been booked and agreed for Wednesday 05/08/2009 with an AM arrival time. Your surveyor is Mark Duwell and he is contactable on 07884 432139. The installation will take approximately 2 hours.
Please note that we are unable to install the window if any work is being carried out on the window, window furnishings or surround. Please make sure that the floor area around the windows is cleared as much as possible for working space, and that the furnishings currently covering the windows are removed so that the shutters can be installed. It is not S:CRAFT's responsibility to remove/rehang the existing curtains/blinds and the installers have no liability cover for any non S:CRAFT products. If the installer feels that the shutters can not be installed due to this reason, the installation will have to be cancelled and re-arranged.
S:CRAFT require the person that placed the order to be available for signing off the shutter satisfaction form. This also brings the 3 year warranty into effect. If the customer is not available our installers have been instructed not to install the shutters. Should you wish the installer to install the shutters without you being present we require written confirmation prior to installation that you forfeit the right to signing off the customer satisfaction note. Your warranty will come into effect automatically.
If you feel that you can not make the shutter installation, or you need to change the date, please contact us giving 48 hours notice.
Please be aware John Lewis require the remaining balance a minimum of three working days prior to the installation, if the final payment has not been made within this time period, the installation will be cancelled and rebooked, once John Lewis can confirm that the full payment has been received S-CRAFT will book a new installation appointment.
Kind regards
Customer Services
t. +44 (0)844 2412270
stMsgText after save and close
Dear Mrs Froehlich ,
Thank-you for your interest in S:CRAFT's plantation shutters. Your installation appointment has been booked and agreed for Wednesday 05/08/2009 with an AM arrival time. Your surveyor is Mark Duwell and he is contactable on 07884 432139. The installation will take approximately 2 hours.
Please note that we are unable to install the window if any work is being carried out on the window, window furnishings or surround. Please make sure that the floor area around the windows is cleared as much as possible for working space, and that the furnishings currently covering the windows are removed so that the shutters can be installed. It is not S:CRAFT's responsibility to remove/rehang the existing curtains/blinds and the installers have no liability cover for any non S:CRAFT products. If the installer feels that the shutters can not be installed due to this reason, the installation will have to be cancelled and re-arranged.
S:CRAFT require the person that placed the order to be available for signing off the shutter satisfaction form. This also brings the 3 year warranty into effect. If the customer is not available our installers have been instructed not to install the shutters. Should you wish the installer to install the shutters without you being present we require written confirmation prior to installation that you forfeit the right to signing off the customer satisfaction note. Your warranty will come into effect automatically.
If you feel that you can not make the shutter installation, or you need to change the date, please contact us giving 48 hours notice.
Please be aware John Lewis require the remaining balance a minimum of three working days prior to the installation, if the final payment has not been made within this time period, the installation will be cancelled and rebooked, once John Lewis can confirm that the full payment has been received S-CRAFT will book a new installation appointment.
Kind regards
Customer Services
t. +44 (0)844 2412270
objMail.Body after save and close
Please find attached a copy of your John Lewis Shutter quotation. We apologise for the delay in generating the quote for you.
If you wish to progress with this order please sign the copy of the quotation and place your order with your John Lewis Store. A 20% deposit of the product cost will be taken at the time of order.
Expected lead time is 8-9 weeks from the placement of your order if ordered by sea and 3-4 weeks if ordered by air.
t. +44 (0)844 2412270
ASKER
Chris,
Please see my previous submission for the text.
stMsgText is declared in the Declarations section of this module. There are other stMsgText variables in the project and so I have renamed it to stMailText so that it's unique to this module.
Unfortunately, that's made no difference.
Please see my previous submission for the text.
stMsgText is declared in the Declarations section of this module. There are other stMsgText variables in the project and so I have renamed it to stMailText so that it's unique to this module.
Unfortunately, that's made no difference.
ASKER
The incorrect text that appears is contained in another template in the same folder as the original that objMail is created from.
That other template is not referred to by any code in the module and even if I change the template name the problem still occurs!
Aggghhhh!
That other template is not referred to by any code in the module and even if I change the template name the problem still occurs!
Aggghhhh!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.