将VBA格式$转换为VBS

普拉莫·卡兰迪卡(Pramod Karandikar)

我正在使用一个简单的VBA来显示Outlook日历中明天的会议。想法是安排这个VBA每天晚上安排,以显示第二天的约会,以防万一我错过了在离开办公室之前检查约会的机会。

为了测试代码,我将其编写为MS Excel宏。这是完整的代码:

Sub getMyAppointmentsForTomorrow()
    Dim objOutlook
    Dim objNameSpace
    Dim objFolder
    Dim MyItems
    Dim CurrentAppointment
    Dim meetingDetails
    Dim dateTomorrowWihoutTime

    Dim restrictionString

    Const olFolderCalender = 9

    Set objOutlook = CreateObject("Outlook.application")
    Set objNameSpace = objOutlook.GetNameSpace("MAPI")
    Set objFolder = objNameSpace.GetDefaultFolder(olFolderCalender)
    Set olItems = objFolder.items
    olItems.Sort "[Start]"
    olItems.IncludeRecurrences = True

    myStart = Date
    myEnd = DateAdd("d", 3, myStart)

    StartDate = Format$(Date, "mmm-dd-yyyy")
    EndDate = Format$(DateAdd("d", 1, StartDate), "mmm-dd-yyyy")

    strRestriction = "[Start] >= '" & _
    Format$(myStart, "mmm-dd-yyyy") _
    & "' AND [End] <= '" & _
    Format$(myEnd, "mmm-dd-yyyy") & "'"
    Set oItemsInDateRange = olItems.Restrict(strRestriction)

    dateTomorrow = Format$(DateAdd("d", 1, StartDate), "mmm-dd-yyyy")
    dayAfterTomorrow = Format$(DateAdd("d", 2, StartDate), "mmm-dd-yyyy")

    For Each CurrentAppointment In oItemsInDateRange
        If CurrentAppointment.Start >= DateValue(dateTomorrow) And CurrentAppointment.Start <= DateValue(dayAfterTomorrow) Then
            meetingDetails = meetingDetails & CurrentAppointment.Subject & " " & CurrentAppointment.Start & vbNewLine
        End If
    Next
    MsgBox ("*** Tomorrow's Meetings ***" & vbNewLine & vbNewLine & meetingDetails)

End Sub

我需要将其作为独立文件运行。我试图将此文件保存为名为.vbs并运行它,但它显示错误Invalid CharacterFormat$我也尝试将脚本嵌入到HTA文件中,但是遇到相同的错误。

有没有办法在独立文件中运行此代码?

麦克
' StartDate, EndDate, myStart, myEnd variables not needed; so, I removed them
' dateTomorrowWihoutTime variable is not being used; removed it
' dateTomorrow, dayAfterTomorrow are being used as Date Values; so, removed conversion to (and back from) string
' restrictionString is not being used; renamed to strRestriction which is being used.
' "Option Explicit" is needed at the top of the module (both VBA and VBScript) to enforce variable declaration; added it.
' Added some declarations needed
' Initiallized meetingDetails variable outside the For loop; just a matter of practice, not really needed in this case
' Added strMeetings to show additional meeting information in a slightly different format
' Works in Excel VBA and as VBScript without any changes
' Scope for improvement: Optionally send the message text to the default printer

Option Explicit

Sub getMyAppointmentsForTomorrow()
    Dim olItems
    Dim CurrentAppointment

    Dim dateTomorrow
    Dim dayAfterTomorrow

    Dim meetingDetails
    Dim strRestriction
    Dim strMeetings

    Const olFolderCalender = 9




    dateTomorrow = DateAdd("d", 1, Date)
    dayAfterTomorrow = DateAdd("d", 2, Date)

    Set olItems = CreateObject("Outlook.application").GetNameSpace("MAPI").GetDefaultFolder(olFolderCalender).items
    olItems.Sort "[Start]"
    olItems.IncludeRecurrences = True

    strRestriction = "([Start] >= '" & dateTomorrow & "') AND ([End] < '" & dayAfterTomorrow & "')"

    meetingDetails = ""
    strMeetings = ""
    For Each CurrentAppointment In olItems.Restrict(strRestriction)
        If (CurrentAppointment.Start >= dateTomorrow) And (CurrentAppointment.Start <= dayAfterTomorrow) Then
            meetingDetails = meetingDetails & CurrentAppointment.Subject & " " & CurrentAppointment.Start & vbNewLine
            strMeetings = strMeetings & CurrentAppointment.Start & " - " & CurrentAppointment.Location & vbNewLine & CurrentAppointment.Subject & vbNewLine & vbNewLine
        End If
    Next
    If (Len(strMeetings) > 4) Then ' Eliminate trailing newlines
        strMeetings = Left(strMeetings, Len(strMeetings) - 4)
    End If

    MsgBox ("*** Tomorrow's Meetings ***" & vbNewLine & vbNewLine & meetingDetails)
    MsgBox strMeetings, vbInformation, "Tomorrow's Meetings"
End Sub

getMyAppointmentsForTomorrow

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将VBA转换为VBS的问题

来自分类Dev

将VBS脚本转换为VBA并通过Excel执行

来自分类Dev

将日期格式转换为

来自分类Dev

将日期格式转换为适当的格式?

来自分类Dev

将.dll格式转换为.exe格式?

来自分类Dev

将长格式转换为宽格式

来自分类Dev

将长格式转换为宽格式

来自分类Dev

将格式转换为新格式javascript

来自分类Dev

将VBA转换为VBScript

来自分类Dev

将公式转换为 VBA

来自分类Dev

VBS将字符串转换为浮点

来自分类Dev

Excel VBA如何将单元格格式文本转换为数字

来自分类Dev

使用forloops VBA-Excel将数据转换为面板格式

来自分类Dev

如何将“ 12/08/16”转换为日期格式并在VBA中进行比较?

来自分类Dev

如何将填充整数的列转换为文本格式 - VBA - Excel

来自分类Dev

在 Excel VBA 中使用数值将美国数字转换为欧洲格式

来自分类Dev

当操作系统文化设置为英国时,Excel vba 将日期转换为不同的格式

来自分类Dev

使用 VBA 将 dd.mm.yyyy 转换为英国日期格式的问题

来自分类Dev

解析/将cookie转换为JSON格式

来自分类Dev

将日期格式代码转换为日期

来自分类Dev

将String转换为hh:mm格式

来自分类Dev

lang格式将模板转换为位移

来自分类Dev

将datetime转换为特定格式?

来自分类Dev

Javascript将时间转换为纪元格式

来自分类Dev

将图像转换为GRF格式

来自分类Dev

将PDF转换为图像(格式正确)

来自分类Dev

WebAPI将OData $格式转换为xml

来自分类Dev

Python将时间转换为UTC格式

来自分类Dev

将代码从Java转换为PHP格式。