title: Why compile error? excel vba reference to outlook
body: <p>This 'archiveOutlookFolder' code works fine until I run other code that removes/re-adds a reference to outlook. After unloading/loading Outlook I get a compile error on the objFolder.MoveTo objDestFolder line.</p>
  
  <p>I have to unload/load Outlook because different people have different versions of outlook throughout the office. So, to prevent errors, if the workbook has a version loaded, it's unloaded, then the user's version is loaded.</p>
  
  <p>To reiterate: after unloading/loading Outlook I start getting a compile error on the 'objFolder.MoveTo objDestFolder' line of the 'archiveOutlookFolder' sub.</p>
  
  <p>Any assistance resolving this would be greatly appreciated. Thanks!</p>
  
  <pre><code>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 &amp; vblf &amp; vblf &amp; 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 &amp; vblf &amp; vblf &amp; 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) &gt; 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
  </code></pre>