![](/img/trans.png)
[英]“Run-time error 462: The remote server machine does not exist or is unavailable” when running VBA code in EXCEL for a second time (get ACCESS table)
[英]Access code using Excel object, error: 462 The remote server machine does not exist or is unavailable
我正在尝试根据我创建的查询在 Excel 中创建列表。 我收到各种错误,与相同的代码不一致。 我的研究指向这篇文章,它很好地描述了这个问题。 Microsoft 支持文章
该代码给出了“With wkb.ActiveSheet.ListObjects.Add”行上的文章中概述的各种错误
wkb.Queries.Add Name:="qry" & strProcedure, Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Odbc.Query(""dsn=" & strDSN & """, ""select * from " & strProcedure & "()"")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
With wkb.ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=qry" & strProcedure & ";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [" & "qry" & strProcedure & "]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "qry" & strProcedure
' .Refresh BackgroundQuery:=False
End With
文章给出了如何重现该问题以及如何修复它的示例。 但是从他们的示例中很难看出我应该如何更改代码以避免错误。
从文章:
注意出现错误消息的原因是代码引用了单元格的方法,而没有在调用之前使用 xlSheet 对象变量。
停止项目并更改以下代码行: xlSheet.Range(Cells(1,1),Cells(10,2)).Value = "Hello"
将代码行更改为类似于以下代码行。 xlSheet.Range(xlSheet.Cells(1,1),xlSheet.Cells(10,2)).Value = "你好"
再次运行程序。 请注意,您可以多次运行代码而不会收到错误消息。
编辑:这是清理的完整代码。 我从 Access 2019 (365) 运行此注意:excel.quit 强制我输入文件名并保存并关闭电子表格。 即使在执行此操作后,并且没有打开其他 Excel 窗口,Excel 仍会保留在任务管理器中,直到我关闭 Access。
编辑 2:如本文所述,此代码第一次运行良好,但在后续运行中失败并显示不一致的错误消息。
Public Sub CreateExcelQueryTest()
Dim AppExcel As Excel.Application
Dim wkb As Excel.Workbook
Dim qry As Excel.WorkbookQuery
Dim strProcedure As String
Dim strDSN As String
Set AppExcel = New Excel.Application
strDSN = "INSPIRE33"
strProcedure = "wwcustomers_addresses"
Set wkb = AppExcel.Workbooks.Add
wkb.Queries.Add Name:="qry" & strProcedure, Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Odbc.Query(""dsn=" & strDSN & """, ""select * from " & strProcedure & "()"")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
wkb.Worksheets.Add.Name = strProcedure
With wkb.ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=qrywwcustomers_addresses;Extended Properties=""""" _
, Destination:=Range("wwcustomers_addresses!$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [" & "qry" & strProcedure & "]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "qry" & strProcedure
.Refresh BackgroundQuery:=False
End With
AppExcel.Visible = True
AppExcel.Quit
End Sub
刚才我在测试这个函数时收到了一个来自Excel的错误报告。 不用说,我向 MS 发送了“皱眉”。
Feedback Type:
皱眉(错误)
错误消息:试图读取或写入受保护的内存。 这通常表明其他内存已损坏。
堆栈跟踪:在 Microsoft.Mashup.Client.Excel.NativeExcelFunctionsC2RBase.Microsoft.Mashup.Client.Excel.INativeExcelFunctions.SaveMashupData(IntPtr workbookPointer, String data) 在 Microsoft.Mashup.Client.Excel.Shim.NativeWorkbookStorageServices.SaveMashupData(IWorkbook) MashupCustomXmlPart 数据)在 Microsoft.Mashup.Client.Excel.Shim.NativeWorkbookStorageServices.NativeWorkbookStorageDeferrer.Release() 在 Microsoft.Mashup.Client.Excel.Shim.NativeWorkbookStorageServices.Microsoft.Mashup.Client.Excel.Shim.IDeferredStorageInvoker.InvokeBookIvoke , Action action) at Microsoft.Mashup.Client.Excel.Shim.NativeCoAuthServices.InvokeCoauthAction(IWorkbook workbook, UndoableActionType actionType, Action action) at Microsoft.Mashup.Client.Excel.Shim.NativeCoAuthServices.InvokeCoauthAction(IWorkbookIdentity workbookIdentity action, UndoableActionType action) action) 在 Microsoft.Mashup.Client.Excel.Fill.FillManager.ForEachFillSessionByWorkbook(Action`1 act ion) 在 Microsoft.Mashup.Client.Excel.Fill.PassiveFillManager.UpdateQueriesProgress() 在 Microsoft.Mashup.Client.Excel.Fill.PassiveFillManager.b__9_0() 在 Microsoft.Mashup.Host.Document.ExceptionHandlerExtensions.HandleExceptions(IExceptionHandler exceptionHandler,动作动作)
堆栈跟踪消息:尝试读取或写入受保护的内存。 这通常表明其他内存已损坏。
调用堆栈跟踪:在 Microsoft.Mashup.Host.Document.ExceptionExtensions.GetCurrentInvocationStackTrace() 在 Microsoft.Mashup.Client.UI.Shared.StackTraceInfo..ctor(String exceptionStackTrace, String invocationStackTrace, String exceptionMessage) 在 Microsoft.Mashup.Client。 UI.Shared.FeedbackErrorInfo..ctor(String message, Exception exception, Nullable`1 stackTraceInfo, String messageDetail) 在 Microsoft.Mashup.Client.Excel.Native.NativeUserFeedbackServices.RaiseErrorDialog(IWindowHandle activeWindow, IUIHost uiHost, FeedbackPackageInfo feedbackPackageInfo, Exception e, LocalizedString dialogTitle、LocalizedString dialogMessage、Boolean useGDICapture) 在 Microsoft.Mashup.Client.Excel.Native.NativeUserFeedbackServices.ReportException(IWindowHandle activeWindow, IUIHost uiHost, FeedbackPackageInfo feedbackPackageInfo, Exception e, Boolean useGDICapture) 在 Microsoft.Mashup.Client.UI.Shared。 UnexpectedExceptionHandler.<>c__DisplayClass14_0.b__0() 在 Microsoft.Mashup.Host.Document.Synchr onizationContextExtensions.SendAndMarshalExceptions(SynchronizationContext context, Action callback) at Microsoft.Mashup.Client.UI.Shared.UnexpectedExceptionHandler.HandleException(Exception e) at Microsoft.Mashup.Host.Document.ExceptionHandlerExtensions.HandleExceptions(IExceptionHandler exceptionHandler, Action action) at Microsoft。 Mashup.Client.Excel.Fill.PassiveFillManager.OnFillUpdateTimerTick(Object sender, EventArgs eventArgs) at System.Windows.Forms.Timer.OnTick(EventArgs e) at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m) at System .Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
支持高级内容:错误
更新 好的,我正在从直接窗口调用这个子程序。 它在第一次尝试时有效,我知道它会结束,因为我现在有一个消息框,上面写着“完成”但 excel 过程并没有结束。 如果我单击停止按钮(蓝色方块),则 Excel 进程结束并且第二次尝试不会失败。 此时代码不应仍在运行。 以前从未见过这个。
考虑几个修复:
正如您引用的文档所示,限定您的单元格目标的Range
,即工作表。
调用 Excel 对象、方法或属性而不使用 Excel 对象变量限定元素的一行代码
避免使用ActiveCell
/ ActiveSheet
/ ActiveWorkbook
。 相反,使用分配的对象,如新添加的工作表strProcedure
;
像在用户界面上一样使用Workbook.Close正确关闭工作簿:Workbook Close > App Exit;
作为 VBA 中的最佳实践,使用Set object = Nothing
释放任何Set
对象,而不是简单地关闭后台进程。
见调整:
With wkb.Worksheets(strProcedure).ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=qry" & strProcedure & ";Extended Properties=""""" _
, Destination:=wkb.Worksheets(strProcedure).Range("$A$1")).QueryTable
...
End With
' NO NEED TO SHOW IF YOU IMMEDIATELY QUIT
AppExcel.Visible = True
' CLOSE WORKBOOK AND EXIT APP
wkb.Close SaveChanges:=True
AppExcel.Quit
' RELEASE RESOURCES
Set wkb = Nothing
Set AppExcel = Nothing
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.