PDA

View Full Version : Type Mismatch error



Michal
05-18-2015, 10:07 AM
Hello Guys
I'm learning VBA to automate couple of things with my Outlook.
Now I'm trying to create a script that will generate email for me.
Unfortunately I'm stuck. 2nd code works perfect but 1st one returns type mismatch error. I tried to convert variables types but I failed,
Please advise
Thank you



Sub HelloWorldMessage()
Dim Msg As Outlook.MailItem
Set Msg = Application.CreateItem(olMailItem)
Msg.To = "[email address]"
Msg.Subject = "Todays All Green Today email" + DateAdd("m", 1, "31-Jan-95")
IntervalType = "d" ' "m" specifies months as interval.
FirstDate = Now()
Number = 1
SecondDate = DateAdd(IntervalType, Number, FirstDate)
Msg.Body = "New date: " & _
"Hello Team, [link]" + CStr(FirstDate) + "%20AND%20created%20%3C%3D%20" + CStr(SecondDate)
Msg.Display
Msg.Send
Set Msg = Nothing
End Sub
------------------------------------------------------------------------------------------



Sub Msg()
Dim FirstDate As Date ' Declare variables.
Dim IntervalType As String
Dim Number As Integer
Dim Msg
IntervalType = "d" ' "m" specifies months as interval.
FirstDate = Format(Now(), "yyyy-mm-dd")
Number = 1
SecondDate = DateAdd(IntervalType, Number, FirstDate)
Msg = "New date: " & _
"Hello Team, [link]" + CStr(FirstDate) + "%20AND%20created%20%3C%3D%20" + CStr(SecondDate)
MsgBox Msg
End Sub

gmayor
05-18-2015, 10:04 PM
The error message was caused by the use of + rather than & to concantenate the text strings.
Get into the habit of declaring your variables. Option Explicit at the start of the module will enforce that. You can set the VBA editor opttions to insert that automatically in new modules.
For editing Outlook message bodies, use the WordEditor inspector. You can set a range to the start of the body and thus retain the automatically inserted default signature (see below)
I have no idea what you are trying to achieve with the message body, but you can treat it as a Word range.



Option Explicit

Sub HelloWorldMessage()
Dim Msg As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim IntervalType As String
Dim FirstDate As Date
Dim SecondDate As Date
Dim Number As Integer

IntervalType = "d"
Number = 1
FirstDate = Now()
SecondDate = DateAdd(IntervalType, Number, FirstDate)

Set Msg = Application.CreateItem(olMailItem)
With Msg
.To = "[email address]"
.Subject = "Todays All Green Today email " & DateAdd("m", 1, "31-Jan-95")
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
Set oRng = wdDoc.Range(0, 0)
oRng.Text = "New date: " & _
"Hello Team, [link]" & CStr(FirstDate) & " %20AND%20created%20%3C%3D%20 " & CStr(SecondDate)
.Display
.sEnd
End With

lbl_Exit:
Set Msg = Nothing
Exit Sub
End Sub

Michal
05-19-2015, 03:21 AM
HI,

Thank you for your help.

Actually what fixed it for me was converting var type (Msg.Subject = "Todays All Green Today email" + DateAdd("m", 1, "31-Jan-95"))

Thank for all advise. I really like it and I'm going to use it.

Thanks again