這個'archiveOutlookFolder'代碼工作正常,直到我運行其他代碼,刪除/重新添加對Outlook的引用。卸載/加載Outlook後,我得到objFolder.MoveTo objDestFolder行上的編譯錯誤。爲什麼要編譯錯誤? excel vba引用outlook
我必須卸載/加載Outlook,因爲不同的人在整個辦公室都有不同的outlook版本。因此,爲了防止出現錯誤,如果工作簿加載了版本,則會卸載它,然後加載用戶的版本。
重申:在卸載/加載Outlook後,我開始在'archiveOutlookFolder'子的'objFolder.MoveTo objDestFolder'行上收到編譯錯誤。
任何協助解決這將不勝感激。謝謝!
Private Sub LoadOutlook()
Application.Run "UnloadOutlook"
On Error GoTo unable2Load
ThisWorkbook.VBProject.References.AddFromFile "MSOUTL.OLB"
Exit Sub
unable2Load:
If Err.Number = 32813 Then Exit Sub
If Err.Number = 48 Then'for some reason 16 won't load without specific reference
ThisWorkbook.VBProject.References.AddFromFile "C:\Program Files (x86)\Microsoft Office\Office16\MSOUTL.OLB"
Exit Sub
End If
MsgBox err.number & vblf & vblf & err.description
End Sub
Private Sub UnloadOutlook()
On Error GoTo unable2Unload
Dim References As Object
Set References = ThisWorkbook.VBProject.References
References.Remove References("Outlook")
Exit Sub
unable2Unload:
If Err.Number = 9 Then Exit Sub 'already unloaded
MsgBox err.number & vblf & vblf & err.description
End Sub
Private Sub archiveOutlookFolder()
on error goto errHandler
Dim objOutlook As Outlook.Application
Dim objNamespace As Outlook.Namespace
Dim objSourceFolder As Outlook.MAPIFolder
Dim objDestFolder As Outlook.MAPIFolder
Dim objFolder As Folder
Dim AAfolderToMove As String
Dim PNAToMove As String
Dim eventFolderTomove As String
Dim foundEventFolder As Boolean
Dim olAAfolders As Outlook.Folder
Dim olFolder As Outlook.Folder
PNAToMove = ThisWorkbook.Sheets("data").Range("cleanpna").Value
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0
If objOutlook Is Nothing Then
Set objOutlook = CreateObject("Outlook.Application")
End If
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set olAAfolders = objNamespace.GetDefaultFolder(olFolderInbox).Parent.Folders("Audits-Actuals")
foundEventFolder = False
For Each olFolder In olAAfolders.Folders
If InStr(olFolder.Name, PNAToMove) > 0 Then
eventFolderTomove = olFolder.Name
foundEventFolder = True
Exit For
End If
Next olFolder
If foundEventFolder = False Then
MsgBox "I did not find an Outlook folder for this event to move to Past events. Please move manually.", vbCritical, "Audits\Actuals"
Exit Sub
End If
Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox).Parent.Folders("Audits-Actuals").Folders(eventFolderTomove)
Set objDestFolder = objNamespace.GetDefaultFolder(olFolderInbox).Parent.Folders("PAST Audits-Actuals")
objFolder.MoveTo objDestFolder
Set objDestFolder = Nothing
Set objFolder = Nothing
Set objSourceFolder = Nothing
Set objOutlook = Nothing
Set objDestFolder = Nothing
Exit Sub
errhandler:
subName = "archiveOutlookFolder"
thisErrNum = Err.Number
thisErrDes = Err.Description
Call sendErrorAlert
End Sub
當你的代碼完成後,更改展望特定變量'作爲Object'和你參考免費的,但記得要以'私人Const'定義任何Outlook常量,例如'Private Const olFolderInbox = 6' – PatricK
非常感謝,@PatricK!這太棒了,我不需要參考!我嘗試設置爲Const,但是我必須做錯什麼? –
非常感謝,@PatricK!這太棒了,我不需要參考!我試圖設置爲常量,但我必須做錯誤的cuz'const objOutlook As Object = Outlook.Application'給了我'變量未定義',但我無法弄清楚使用什麼變量。我可以使用'Dim objOutlook As Object'和'Set objOutlook = GetObject(,「Outlook.Application」)',但是我在'olFolderInbox'上得到'未定義的變量',並且再次找不到要使用哪個變量(對象和文件夾丟失不匹配錯誤)。你可能會發布你的解決方案的例子,所以我可以檢查出來嗎? –