excel - 在 VBA : Run-time error ‘462’ : The remote server machine does not exist or is unavialiable 中打开 Excel 文件

标签 excel vba

我需要创建一个宏来打开一个 Excel 文件,并将一些文件保存在工作簿中。问题是,当我想在短时间内多次运行宏时(不幸的是我需要这样做),我收到错误“462”:远程服务器机器不存在或不可用。

我已经阅读并尝试修复它:我在开始时创建了一个特殊模块来终止 Excel 进程:

Call KillExcel

Function KillExcel()

Dim oServ As Object
Dim cProc As Variant
Dim oProc As Object

Set oServ = GetObject("winmgmts:")
Set cProc = oServ.ExecQuery("Select * from Win32_Process")

For Each oProc In cProc

    'Rename EXCEL.EXE in the line below with the process that you need to Terminate.
    'NOTE: It is 'case sensitive

    If oProc.Name = "EXCEL.EXE" Or oProc.Name = "EXCEL.EXE *32" Then
     ' MsgBox "KILL"   ' used to display a message for testing pur
      errReturnCode = oProc.Terminate()
    End If
Next

End Function

但不幸的是,即使我关闭了这个进程,我仍然会收到这个错误。我使用 Excel 的部分代码如下所示:
Dim ark As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Set ark = Excel.Workbooks.Open(FileName:=scexcel)
Set xlSheet = ark.Worksheets("Sheet1")
a = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row + 1


Cells(a, 2).Value = "ABC"
Cells(a, 3).Value = "DEF"
Cells(a, 4).Value = "GHI"
Cells(a, 5).Value = "JKL"

a = a + 1

Set xlSheet = Nothing
ark.Close SaveChanges:=True

Set ark = Nothing

如果它有帮助,每次我在短时间内多次运行宏时都会失败:
Set ark = Excel.Workbooks.Open(FileName:=scexcel)

请注意 scexcel是 Excel 文件的路径。

你能帮我解决这个问题吗?

最佳答案

这应该对您有用(确保首先杀死以前代码中遗留的任何隐藏的 Excel.exe 实例):

Dim ark                   As Excel.Workbook
Dim xlSheet               As Excel.Worksheet
Dim a                     As Long

Set ark = GetObject(scexcel)
ark.Application.Visible = True
Set xlSheet = ark.Worksheets("Sheet1")
With xlSheet
    a = .Range("B" & .Rows.Count).End(xlUp).Row + 1
    .Cells(a, 2).Value = "ABC"
    .Cells(a, 3).Value = "DEF"
    .Cells(a, 4).Value = "GHI"
    .Cells(a, 5).Value = "JKL"
End With
Set xlSheet = Nothing
ark.Close SaveChanges:=True

Set ark = Nothing

关于excel - 在 VBA : Run-time error ‘462’ : The remote server machine does not exist or is unavialiable 中打开 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33253570/

相关文章:

mysql - MariaDB 2.0.13 与 MySQL 5.3 ODBC 驱动程序(VBA 连接)

excel - WScript.Shell 使 Excel 崩溃

vba - 大数组的excel vba错误400(大数据输入/输出)

sql - VBA for Excel 2016 中的 ADODB 连接超时 - 如何检查连接是否仍处于事件状态?

c# - Excel 2016 在保存错误时触发撤消?

excel - 有没有办法通过仅粘贴值但保留表格来将完整的工作表复制到另一个工作簿?

excel - 从 Excel vba 将“适合”设置为“在 Pdf 中可见”

Excel (VBA) : Disabling cell formatting except bold font

删除千位分隔符

c# - 如何将 ClosedXML 中的合并单元格范围内的图像居中