首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Excel VBA -如何将UserForm TextBox格式化为时间格式

Excel VBA -如何将UserForm TextBox格式化为时间格式
EN

Stack Overflow用户
提问于 2016-12-24 02:15:57
回答 2查看 1.3K关注 0票数 0

我正在excel中创建一个UserForm,它将在我的Outlook日历中创建一个约会。除了开始和结束时间外,一切都在正常工作。下面是我的代码,其中DTPicker1是约会日期,DTPicker2和DTPicker3分别是开始和结束时间。它们是dtpTime格式的。任命是在正确的日期和主题上创建的,除时间外,一切都正常工作。不知道我该怎么做才能修好它。任何帮助都是非常感谢的。谢谢!

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-24 17:10:26

我发现了我的错误。我需要用空格分隔myStart和myEnd中的两个变量,并将它们与"&“连接起来,而不是下面代码中的"+”。我还需要在我的time DTPicker上使用DTPicker函数来正确地格式化它。谢谢大家的意见!

代码语言:javascript
运行
复制
    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
EN

Stack Overflow用户

发布于 2016-12-24 03:31:59

如果是我的话,我只会让UI成为合适的日期-时间,然后在提交到Outlook时用VBA格式化该单元格。

代码语言:javascript
运行
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41309744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档