我在Excel中創建一個用戶窗體,它將在我的Outlook日曆中創建一個約會。除了開始和結束時間以外,一切都在工作。下面是我的代碼,其中DTPicker1是約會的日期,DTPicker2和DTPicker3分別是開始和結束時間。它們採用dtpTime格式。約會是在正確的日期和主題上創建的,除了時間之外,一切都正常工作。不知道我應該如何解決它。任何幫助表示讚賞。謝謝!Excel VBA - 如何在時間格式中格式化用戶窗體文本框
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
正如您所認識的,內置文本框控件不會執行此操作。 什麼你可能是響應文本框更改事件 獲取文本和相應的代碼 – dgorti
格式我完全忘了UserForm中的DatePicker框可以更改爲時間格式的格式,所以我打算使用但現在也不行。我想這可能是我編寫的代碼,而不是格式。我將用我的所有代碼編輯我的問題。 – gluc7
@ gluc7我看不到你在哪兒'Dim myStart As Date'?它應該是'myStart = DTPicker1.value'。什麼是'myStart = DTPicker1&DatePicker2'假設是?你想把這些日期加在一起嗎?究竟是什麼? –