[英]“Run-time error 462 : The remote server machine does not exist or is unavailable” when running VBA code a second time
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: 下面的代码在我第一次运行时运行良好,但是当我需要第二次运行时,它给了我这个错误:
Run Time error '462': the remote server machine does not exist or is unavailable
运行时错误“ 462”:远程服务器计算机不存在或不可用
It does happen all the time and i've fight against background excel instance so maybe it's something like that...? 它的确无时无刻不在发生,而且我一直在与后台excel实例作斗争,所以也许是那样的……? What am I missing here?
我在这里想念什么?
Option Compare Database
Option Explicit
Private Sub Commande2_Click()
On Error GoTo err_Handler
MsgBox ExportRequest, vbInformation, "Terminé"
Application.FollowHyperlink CurrentProject.Path & "\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 & "\Output_Template.xlsm"
sOutput = CurrentProject.Path & "\Stage1.xlsm"
If Dir(sOutput) <> "" 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 " & Periode_var & "A, Nom, Cat" & Periode_var & "A FROM Planif WHERE Cat" & Periode_var & "A > 0 ORDER BY Cat" & Periode_var & "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 #" & lRecords & " 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" & iRow & ":E" & iRow).Borders.LineStyle = xlContinuous
iRow = iRow + 1
rst.MoveNext
Loop
sSQL = "SELECT " & Periode_var & "B, Nom, Cat" & Periode_var & "B FROM Planif WHERE Cat" & Periode_var & "B > 0 ORDER BY Cat" & Periode_var & "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 #" & lRecords & " 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" & iRow & ":E" & 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" & iRow).Interior.Color = RGB(244, 176, 132)
ElseIf rst.Fields(1) = "1,2" Then
wks.Range("B" & iRow).Interior.Color = RGB(155, 194, 230)
ElseIf rst.Fields(1) = "2,3" Then
wks.Range("B" & iRow).Interior.Color = RGB(255, 192, 0)
ElseIf rst.Fields(1) = "3,4" Then
wks.Range("B" & iRow).Interior.Color = RGB(169, 208, 142)
End If
Next
wks.Rows(iRow).EntireRow.AutoFit
' wks.Range("B" & iRow & ":E" & 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" & derl).Borders.LineStyle = xlContinuous
appExcel.DisplayAlerts = False
wbk.SaveAs CurrentProject.Path & "\Stage1.xlsm"
ExportRequest = "Total de " & lRecords & " 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
This is usually caused by unqualified objects: 这通常是由不合格的对象引起的:
"Visual Basic has established a reference to Excel because of a line of code that calls an Excel object, method, or property without qualifying the element with an Excel object variable. Visual Basic does not release this reference until you end the program. This errant reference interferes with automation code when the code is run more than one time." “ Visual Basic之所以建立了对Excel的引用,是因为有一行代码调用了Excel对象,方法或属性,而没有用Excel对象变量来限定元素。VisualBasic在您结束程序之前不会释放该引用。当代码多次运行时,引用会干扰自动化代码。” [ https://support.microsoft.com/en-us/help/178510/excel-automation-fails-second-time-code-runs]
[ https://support.microsoft.com/en-us/help/178510/excel-automation-fails-second-time-code-runs]
On a quick look through your code I see unqualified Ranges in this section of code, so try qualifying the ranges eg wks.Range("F6") etc. 快速浏览您的代码,我在此部分代码中看到不合格的范围,因此请尝试限定范围,例如wks.Range(“ F6”)等。
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
First, I believe you're running into the situation described here, even though you're certainly not using one of the Excel versions the article mentions: Excel automation fails second time code runs . 首先,我相信您会遇到这里描述的情况,即使您确实没有使用本文提到的Excel版本之一,也是如此: Excel自动化第二次代码运行失败 。 The key sentence in the article is
文章中的关键句子是
Visual Basic has established a reference to Excel because of a line of code that calls an Excel object, method, or property without qualifying the element with an Excel object variable.
Visual Basic已建立对Excel的引用,因为有一行代码调用了Excel对象,方法或属性,而没有用Excel对象变量限定元素。
A quick inspection of your code reveals this line: 快速检查您的代码会发现以下行:
derl = Range("F6").End(xlDown).Row
Notice that you haven't qualified Range
with wks.
请注意,您尚未使用
wks.
限定Range
wks.
. 。 There might be other occurrences of unqualified references in your code;
您的代码中可能还会出现其他不合格的引用; I'll let you double-check.
我让你仔细检查。
Then, here's how you should be closing the Excel session: 然后,这是关闭Excel会话的方法:
'Release child objects, then their parents, etc.
Set wks = Nothing
wbk.Close SaveChanges:=False
Set wbk = Nothing
appExcel.Quit
Set appExcel = Nothing
The principle is to release the "deepest" objects first and walk your way up the hierarchy to the application itself, quit it and finally, release it. 原则是先释放“最深”的对象,然后逐步到达应用程序本身的层次结构,然后退出并最终释放它。
Don't forget to remove the task killing shell call. 不要忘记删除杀死Shell的任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.