我正在制作一个简单的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 Character
处显示错误Format$
。我还尝试将脚本嵌入到HTA文件中,但遇到了同样的错误。
有没有办法在独立文件中运行此代码?
答案 0 :(得分:1)
' 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
答案 1 :(得分:0)
我将Format$
更改为FormatDateTime
后解决了问题(正如MikeC的评论中指出的那样)。不需要进行其他更改。