【问题标题】:Opening Excel file in VBA: Run-time error ‘462’: The remote server machine does not exist or is unavialiable在 VBA 中打开 Excel 文件:运行时错误“462”:远程服务器机器不存在或不可用
【发布时间】:2015-10-21 07:39:57
【问题描述】:

我需要创建一个宏来打开一个 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文件的路径。

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

【问题讨论】:

  • 您是否在 in Excel 中运行此代码?
  • 否,在 Outlook 中。对不起,我忘了提。
  • 那就是问题所在。您没有创建 Excel.Application 对象或正确限定您的任何 excel 对象。

标签: excel vba


【解决方案1】:

这应该对你有用(确保你首先杀死之前代码留下的任何隐藏的 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 文件 - 在那里打开 Excel 后没有工作簿-只有灰色背景。你知道我该如何解决吗?
  • 我对应该修复该问题的代码进行了小幅更改,但首先您需要使用查看 - 取消隐藏菜单使工作簿可见,然后保存。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多