2016-12-24 53 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