title: “Run-time error 462 : The remote server machine does not exist or is unavailable” when running VBA code a second time
body: <p>The code below is working fine the first time I run it, but when I need to run it a second time, it gives me this error: </p>
  
  <blockquote>
    <p>Run Time error '462': the remote server machine does not exist or is unavailable</p>
  </blockquote>
  
  <p>It does happen all the time and i've fight against background excel instance so maybe it's something like that...? What am I missing here?</p>
  
  <pre><code>Option Compare Database
  Option Explicit
  
  Private Sub Commande2_Click()
  On Error GoTo err_Handler
  
     MsgBox ExportRequest, vbInformation, "Terminé"
     Application.FollowHyperlink CurrentProject.Path &amp; "\Stage1.xlsm"
  
  exit_Here:
     Exit Sub
  err_Handler:
     MsgBox Err.Description, vbCritical, "Erreur"
     Resume exit_Here
  End Sub
  
  
  Public Function ExportRequest() As String
     On Error GoTo err_Handler
  
     ' Excel object variables
     Dim appExcel As Excel.Application
     Dim wbk As Excel.Workbook
     Dim wks As Excel.Worksheet
  
     Dim sTemplate As String
     Dim sTempFile As String
     Dim sOutput As String
  
     Dim dbs As DAO.Database
     Dim rst As DAO.Recordset
     Dim sSQL As String
     Dim Periode_var As String
     Dim lRecords As Long
     Dim iRow As Integer
     Dim iCol As Integer
     Dim derl As Integer
     Dim iFld As Integer
     Dim R As Long
  
     Const cTabTwo As Byte = 2
     Const cStartRow As Byte = 6
     Const cStartColumn As Byte = 2
  
     DoCmd.Hourglass True 'icone tablier a true
  
     ' set to break on all errors
     Application.SetOption "Error Trapping", 0
  
     ' start with a clean file built from the template file
     sTemplate = CurrentProject.Path &amp; "\Output_Template.xlsm"
     sOutput = CurrentProject.Path &amp; "\Stage1.xlsm"
     If Dir(sOutput) &lt;&gt; "" Then Kill sOutput
     FileCopy sTemplate, sOutput
  
     ' Create the Excel Applicaiton, Workbook and Worksheet and Database object
     Set appExcel = New Excel.Application
      'appExcel.Visible = True
      'appExcel.DisplayAlerts = False
     Set wbk = appExcel.Workbooks.Open(sOutput)
     Set wks = appExcel.Worksheets(cTabTwo)
  
      Periode_var = Modifiable5.Value
  
     sSQL = "SELECT " &amp; Periode_var &amp; "A, Nom, Cat" &amp; Periode_var &amp; "A FROM Planif WHERE Cat" &amp; Periode_var &amp; "A &gt; 0 ORDER BY Cat" &amp; Periode_var &amp; "A ASC "
     Set dbs = CurrentDb
     Set rst = dbs.OpenRecordset(sSQL, dbOpenSnapshot)
     If Not rst.BOF Then rst.MoveFirst
  
     ' For this template, the data must be placed on the 4th row, third column.
     ' (these values are set to constants for easy future modifications)
     iCol = cStartColumn
     iRow = cStartRow
    '''''''''''''''''''''''''''''''''''''
  
    wks.Names.Add Name:="Tablo", RefersTo:="=DECALER(Feuil2!$B$6;;;NBVAL(Feuil2!$B$6:$B$5000);5)"
      'ActiveWorkbook.Worksheets("Feuil1").Names("tablo111").Comment = ""
  
     '''''''''''''''''''''''''''''''''''''
  'Stop
     Do Until rst.EOF
        'iFld = 0
        lRecords = lRecords + 1
        'Me.lblMsg.Caption = "Exporting record #" &amp; lRecords &amp; " to Stage1.xls"
        Me.Repaint
  
        For iCol = cStartColumn To cStartColumn + (rst.Fields.Count - 1)
           wks.Cells(iRow, "B") = rst.Fields(0)
           wks.Cells(iRow, "B").WrapText = False
           wks.Cells(iRow, "C") = rst.Fields(1)
           wks.Cells(iRow, "C").WrapText = False
           wks.Cells(iRow, "F") = rst.Fields(2)
           wks.Cells(iRow, "F").WrapText = False
        Next
  
        wks.Rows(iRow).EntireRow.AutoFit
       ' wks.Range("B" &amp; iRow &amp; ":E" &amp; iRow).Borders.LineStyle = xlContinuous
        iRow = iRow + 1
        rst.MoveNext
     Loop
  
      sSQL = "SELECT " &amp; Periode_var &amp; "B, Nom, Cat" &amp; Periode_var &amp; "B FROM Planif WHERE Cat" &amp; Periode_var &amp; "B &gt; 0 ORDER BY Cat" &amp; Periode_var &amp; "B ASC "
     Set dbs = CurrentDb
     Set rst = dbs.OpenRecordset(sSQL, dbOpenSnapshot)
  
     'Stop
     Do Until rst.EOF
        'iFld = 0
        lRecords = lRecords + 1
        'Me.lblMsg.Caption = "Exporting record #" &amp; lRecords &amp; " to Stage1.xls"
        Me.Repaint
  
        For iCol = cStartColumn To cStartColumn + (rst.Fields.Count - 1)
           wks.Cells(iRow, "B") = rst.Fields(0)
           wks.Cells(iRow, "B").WrapText = False
           wks.Cells(iRow, "D") = rst.Fields(1)
           wks.Cells(iRow, "D").WrapText = False
           wks.Cells(iRow, "F") = rst.Fields(2)
           wks.Cells(iRow, "F").WrapText = False
        Next
  
        wks.Rows(iRow).EntireRow.AutoFit
     '   wks.Range("B" &amp; iRow &amp; ":E" &amp; iRow).Borders.LineStyle = xlContinuous
        iRow = iRow + 1
        rst.MoveNext
     Loop
  
     appExcel.Run "Fusionner"
  
  ''''''''''''''''''''''''''''''''''''''''''''
      sSQL = "SELECT Categorie, Catindex FROM Catvaleur"
     Set dbs = CurrentDb
     Set rst = dbs.OpenRecordset(sSQL, dbOpenSnapshot)
  
    'Stop
     Do Until rst.EOF
  
        lRecords = lRecords + 1
  
        Me.Repaint
  
        For iCol = cStartColumn To cStartColumn + (rst.Fields.Count - 1)
           wks.Cells(iRow, "B") = rst.Fields(0)
           wks.Cells(iRow, "B").WrapText = False
           wks.Cells(iRow, "F") = rst.Fields(1)
           wks.Cells(iRow, "F").WrapText = False
  
           If rst.Fields(1) = "0,1" Then
                  wks.Range("B" &amp; iRow).Interior.Color = RGB(244, 176, 132)
              ElseIf rst.Fields(1) = "1,2" Then
                  wks.Range("B" &amp; iRow).Interior.Color = RGB(155, 194, 230)
              ElseIf rst.Fields(1) = "2,3" Then
                  wks.Range("B" &amp; iRow).Interior.Color = RGB(255, 192, 0)
              ElseIf rst.Fields(1) = "3,4" Then
                  wks.Range("B" &amp; iRow).Interior.Color = RGB(169, 208, 142)
              End If
  
        Next
  
        wks.Rows(iRow).EntireRow.AutoFit
     '   wks.Range("B" &amp; iRow &amp; ":E" &amp; iRow).Borders.LineStyle = xlContinuous
  
        iRow = iRow + 1
        rst.MoveNext
     Loop
  
     'wks.Range("F6").End(xlDown).Select
     wks.Sort.SortFields.Clear
     wks.Sort.SortFields.Add Key:=Range("F6"), _
          SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
      With wks.Sort
          .SetRange Range("B6:F300")
          .Header = xlNo
          .MatchCase = False
          .Orientation = xlTopToBottom
          .SortMethod = xlPinYin
          .Apply
      End With
  
       derl = Range("F6").End(xlDown).Row
       wks.Range("B6:E" &amp; derl).Borders.LineStyle = xlContinuous
       appExcel.DisplayAlerts = False
      wbk.SaveAs CurrentProject.Path &amp; "\Stage1.xlsm"
      ExportRequest = "Total de " &amp; lRecords &amp; " lignes traitées."
  
      'Quitte Excel
      'wbk.Close (True)
      'Libère la mémoire
  '    Set wks = Nothing
  '    wbk.Close savechanges:=False
  '    appExcel.Quit
  '    Set wbk = Nothing
  '    Set appExcel = Nothing
      Dim sKill As String
  
  sKill = "TASKKILL /F /IM excel.exe"
  Shell sKill, vbHide
  
  exit_Here:
     ' Cleanup all objects  (resume next on errors)
     On Error Resume Next
     Set wks = Nothing
     'wbk.Close savechanges:=True
     Set wbk = Nothing
     Set appExcel = Nothing
  '        sKill = "TASKKILL /F /IM excel.exe"
  '        Shell sKill, vbHide
  
     Set rst = Nothing
     Set dbs = Nothing
     DoCmd.Hourglass False 'icone tablier a false
     Exit Function
  
  err_Handler:
     ExportRequest = Err.Description
     Resume exit_Here
  
  End Function
  </code></pre>