vbaoutlookcreateitem

Creating Outlook Appointment


I have created a basic code wherein it will create an Outlook Appointment via VBA excel UserForm. My Problem, however, is that I am at lost on how to set the 'Start' based on the values of TextBox10 and Textbox11 since it does not accept both values of the textboxes together.

 Dim oAppt As AppointmentItem

    Candidate = TextBox2.Value + " " + TextBox1.Value
    InitialDate = TextBox10.Value
    InitialTime = TextBox11.Value

        Set oAppt = Outlook.Application.CreateItem(olAppointmentItem)

            oAppt.Subject = Candidate
            oAppt.Start = TextBox10.Value + TextBox11.Value
            oAppt.Start = IntialTime
            oAppt.Recipients.Add ("Email@gmail.com")
            oAppt.Save
        Candidate = Sheets("Client Lineup List").Range("F" & lMaxRows + 1).Value

Solution

  • Firstly, use Option Explicit always.

    Convert your TextBox values to a date/time first. Example below

    Dim myDate As Date
    myDate = CDate(TextBox10.Value & " " & TextBox11.Value)
    

    However, in this case further considerations are required - check that a valid date and time is entered otherwise my example code above will error out. One simple part solution is to use a DatePicker instead of TextBox10. There used to be a DateTimePicker in VB, but I am not sure where the current equivalent in VBA is now.

    In addition, check how I have concatenated the strings (with a space in between), use "&", not "+" which can have interesting side effects, especially if you have not set Option Explicit and not declared your variables (making them Variant).