【问题标题】:Excel VBA - How to format a UserForm TextBox in Time FormatExcel VBA - 如何以时间格式格式化用户窗体文本框
【发布时间】: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.valuemyStart = DTPicker1 & DatePicker2 应该是什么?你想把这些日期加在一起吗?究竟是什么?
  • @ShaiRado Dim myStart As Date 对此没有影响。至于为什么myStart 是这样设置的,据我所知,这就是您通过 VBA 在 Outlook 中创建会议时间的方式。只有一个.Start 行,并且在那里创建了日期和时间。不过,我可能是错的。我确实根据细胞进行了测试。例如myStart = DateValue(Cells(r, 3).Value) + Cells(r, 4).Value,其中第 3 列是日期,第 4 列是开始时间,效果很好。此外,会议仍然是在正确的日期创建的,只是那个时间行不通,我觉得很奇怪。

标签: excel vba userform


【解决方案1】:

如果是我,我只会将 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

【讨论】:

  • 我不会从单元格中提取信息。发送到 Outlook 的所有内容都将直接来自用户窗体。我也稍微改变了我的问题。
【解决方案2】:

我发现了我的错误。我需要用空格分隔 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 2023-01-21
    • 2017-07-11
    相关资源
    最近更新 更多