2016-12-24 113 views
0

我在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 
+0

正如您所認識的,內置文本框控件不會執行此操作。 什麼你可能是響應文本框更改事件 獲取文本和相應的代碼 – dgorti

+0

格式我完全忘了UserForm中的DatePicker框可以更改爲時間格式的格式,所以我打算使用但現在也不行。我想這可能是我編寫的代碼,而不是格式。我將用我的所有代碼編輯我的問題。 – gluc7

+0

@ gluc7我看不到你在哪兒'Dim myStart As Date'?它應該是'myStart = DTPicker1.value'。什麼是'myStart = DTPicker1&DatePicker2'假設是?你想把這些日期加在一起嗎?究竟是什麼? –

回答

0

,當我發現我的錯誤格式化VBA該單元格。我需要在myStart和myEnd中用空格分隔出兩個變量,並將它們與「&」連接,而不是「+」,如下面的代碼所示。我還需要在我的時間DTPicker上使用TimeValue函數來正確格式化它。感謝大家的意見!

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 
+0

不錯的工作,很高興你得到它的工作。 – pizzaslice

0

如果是我的話,我只想讓UI任何像樣樣的日期,時間,然後提交到Outlook

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 
+0

我不會從細胞中獲取信息。發送到Outlook的所有內容都將直接來自UserForm。我也改變了我的問題。 – gluc7