【发布时间】:2016-12-24 02:15:57
【问题描述】:
我正在用 Excel 制作一个用户窗体,它将在我的 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
【问题讨论】:
-
如您所见,内置的文本框控件不这样做。您可能会响应文本框更改事件以在代码中相应地获取文本和格式
-
我完全忘记了用户窗体中的 DatePicker 框可以更改为时间类型的格式,所以我打算使用它,但现在它也不起作用。我想这可能是我编写的代码而不是格式。我将使用我的所有代码编辑我的问题。
-
@gluc7 我没看到你在哪里
Dim myStart As Date?应该是myStart = DTPicker1.value。myStart = DTPicker1 & DatePicker2应该是什么?你想把这些日期加在一起吗?究竟是什么? -
@ShaiRado
Dim myStart As Date对此没有影响。至于为什么myStart是这样设置的,据我所知,这就是您通过 VBA 在 Outlook 中创建会议时间的方式。只有一个.Start行,并且在那里创建了日期和时间。不过,我可能是错的。我确实根据细胞进行了测试。例如myStart = DateValue(Cells(r, 3).Value) + Cells(r, 4).Value,其中第 3 列是日期,第 4 列是开始时间,效果很好。此外,会议仍然是在正确的日期创建的,只是那个时间行不通,我觉得很奇怪。