我有一個運行的Excel宏,它從電子表格中獲取活動名稱,日期和時間並將它們放入Outlook日曆中。這在Outlook正在運行時工作正常,但當它不是時,宏不會進行約會。宏未能根據工作表數據創建約會
我做了一個錯誤檢查,檢查是否正在運行Outlook的運行實例,如果沒有創建一個,但它仍然只在Outlook運行時才起作用。
任何想法爲什麼?
Sub SetAppt()
' Dim olApp As Outlook.Application
Dim olApt As AppointmentItem
Dim olApp As Object
'if an instance of outlook is not open then create an instance of the application
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If er.Number = 429 Then
Set olApp = CreateObject("Outlook.Application.14")
End If
On Error GoTo 0
Set olApp = CreateObject("Outlook.Application")
' Set olApp = New Outlook.Application
'declare an index for all the variables
Dim i As Integer
i = 2
'declare the variables that will hold the data and set their initial value
Dim occ, actName, srtTime, duration As String
occ = "A" & i
actName = "B" & i
srtTime = "F" & i
duration = "G" & i
'for holding different parts of the dates/times that will be split
Dim splitStr() As String
Dim splitDrtion() As String
'loop until there is no more items
While Range(occ).Value <> ""
'create a new appointment
Set olApt = olApp.CreateItem(olAppointmentItem)
'we must split the start time and date
splitStr = Split(Range(srtTime).Value, " ")
Dim oDate As Date
oDate = splitStr(0)
'we must also spilt the duration (number/hour)
splitDrtion = Split(Range(duration).Value, " ")
'with is used to acces the appointment items properties
With olApt
.Start = oDate + TimeValue(splitStr(1))
'if the duration is in hours then multiply number else leave it
If splitDrtion(1) = "Hour" Then
.duration = 60 * splitDrtion(0)
Else
.duration = splitDrtion(0)
End If
.Subject = Range(occ).Value
.Body = Range(actName).Value
.Save
End With
'increment i and reset all the variables with the new number
i = i + 1
occ = "A" & i
actName = "B" & i
srtTime = "F" & i
duration = "G" & i
Set olApt = Nothing
Wend
Set olApp = Nothing
End Sub
我試過你的一段代碼Siddharth,但它仍然沒有預約。 – codingNightmares
你有一些我可以測試的樣本數據嗎? –
另外還有其他幾個變化。例如,您必須將'Dim olApt As AppointmentItem'修改爲'Dim olApt As Object',並且在您使用後期綁定後將'olApp.CreateItem(olAppointmentItem)'更改爲'olApp.CreateItem(1)'。 –