我正在excel中创建一个UserForm,它将在我的Outlook日历中创建一个约会。除了开始和结束时间外,一切都在正常工作。下面是我的代码,其中DTPicker1是约会日期,DTPicker2和DTPicker3分别是开始和结束时间。它们是dtpTime格式的。任命是在正确的日期和主题上创建的,除时间外,一切都正常工作。不知道我该怎么做才能修好它。任何帮助都是非常感谢的。谢谢!
Private Sub CommandButton1_Click()
Dim olApp As Outlook.Application
Dim olAppItem As Outlook.AppointmentItem
Dim r As Long
On Error Resume Next
Worksheets("Sheet1").Activate
Set olApp = GetObject("", "Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
On Error Resume Next
Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
MsgBox "Outlook is not available!"
Exit Sub
End If
End If
Dim mysub, myStart, myEnd
mysub = TextBox1
myStart = DTPicker1 & DatePicker2
myEnd = DTPicker1 & DatePicker3
Set olAppItem = olApp.CreateItem(olAppointmentItem) 'creates a new appointment
With olAppItem
'set default appointment values
.Location = ""
.Body = ""
.ReminderSet = True
.BusyStatus = olFree
.RequiredAttendees = ""
On Error Resume Next
.Start = myStart
.End = myEnd
.Subject = TextBox1
.Attachments.Add ("c:\temp\somefile.msg")
.Location = ""
.Body = ""
.ReminderSet = True
.BusyStatus = olBusy
.Categories = "Orange Category"
On Error GoTo 0
.Save 'saves the new appointment to the default folder
End With
Set olAppItem = Nothing
Set olApp = Nothing
MsgBox "Done !"
End Sub
发布于 2016-12-24 17:10:26
我发现了我的错误。我需要用空格分隔myStart和myEnd中的两个变量,并将它们与"&“连接起来,而不是下面代码中的"+”。我还需要在我的time DTPicker上使用DTPicker函数来正确地格式化它。谢谢大家的意见!
Dim mysub
Dim myStart, myEnd As Date
mysub = TextBox1
myStart = DTPicker1 & " " & TimeValue(DTPicker2)
myEnd = DTPicker1 & " " & TimeValue(DTPicker3)
Set olAppItem = olApp.CreateItem(olAppointmentItem) 'creates a new appointment
With olAppItem
'set default appointment values
.Location = ""
.Body = ""
.ReminderSet = True
.BusyStatus = olFree
.RequiredAttendees = ""
On Error Resume Next
.Start = myStart
.End = myEnd
.Subject = TextBox1
.Attachments.Add ("c:\temp\somefile.msg")
.Location = ""
.Body = .Subject
.ReminderSet = True
.BusyStatus = olBusy
.Categories = "Orange Category" 'add this to be able to delete the test appointments
On Error GoTo 0
.Save 'saves the new appointment to the default folder
发布于 2016-12-24 03:31:59
如果是我的话,我只会让UI成为合适的日期-时间,然后在提交到Outlook时用VBA格式化该单元格。
Dim startDate
Dim endDate
startDate = Format(Range("A1").value, "yyyy-mm-dd")
endDate = Format(Range("A2").value, "yyyy-mm-dd")
' upload to outlook startDate, endDate, appointment details
https://stackoverflow.com/questions/41309744
复制相似问题