【问题标题】:Excel 2010 VBA runtime Error 462: The remote server machine does not exist or is unavailableExcel 2010 VBA 运行时错误 462:远程服务器计算机不存在或不可用
【发布时间】:2016-04-22 14:14:35
【问题描述】:

我正在从 Excel 写入 Word 文档,如果宏第一次运行完成并且所有 Word 应用程序都已关闭并重新运行宏,我将收到 462 错误。我阅读了大量有关不合格参考的 Microsoft 信息。我无法弄清楚这条线发生了什么导致错误,所以我希望有人能够找到它。

声明

Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim i As Integer
Dim titleCount As Long
Dim Count3, Count4, Count5, endCount3, endCount4, endCount5 As Long
Dim QLRVar As New Scripting.Dictionary
Dim IsOffice2013 As Boolean: IsOffice2013 = False
Dim IsOffice2010 As Boolean: IsOffice2010 = False

如何打开/调用 Word 文档

On Error Resume Next
Set wrdApp = GetObject(Class:="Word.Application")
On Error GoTo 0
If wrdApp Is Nothing Then
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Add
Else:
    wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Add
End If

有错误的部分

With wrdApp.ListGalleries(wdOutlineNumberGallery).ListTemplates(1).ListLevels(1)
    .NumberFormat = "%1. "
    .TrailingCharacter = wdTrailingTab
    .NumberStyle = wdListNumberStyleArabic
    .NumberPosition = InchesToPoints(0)
    .Alignment = wdListLevelAlignLeft
    .TextPosition = InchesToPoints(0)
    .TabPosition = wdUndefined
    .ResetOnHigher = 0
    .StartAt = 1
    .LinkedStyle = "Heading 0"
End With

错误出现在 .NumberPosition 行。

宏非常大,所以我发布了我认为相关的部分,但如果需要我会添加更多。

【问题讨论】:

    标签: vba excel macros


    【解决方案1】:

    Microsoft 表示您必须完全限定每个引用,请参阅: Microsoft error explanation

    我已将这些引用添加为“单词”。在您的代码中添加以下内容:

    With wrdApp.ListGalleries(Word.wdOutlineNumberGallery).ListTemplates(1).ListLevels(1)
        .NumberFormat = "%1. "
        .TrailingCharacter = Word.wdTrailingTab
        .NumberStyle = Word.wdListNumberStyleArabic
        .NumberPosition = Word.InchesToPoints(0)
        .Alignment = Word.wdListLevelAlignLeft
        .TextPosition = Word.InchesToPoints(0)
        .TabPosition = Word.wdUndefined
        .ResetOnHigher = 0
        .StartAt = 1
        .LinkedStyle = "Heading 0"
    End With
    

    我不记得 InchesToPoints() 是否是一个 Word 函数,但我猜它是。我希望你能明白。

    也检查您的其余代码。任何不合格的参考都可能触发此错误。检查每个引用的库(例如 Outlook、Excel、ADODB 等)

    祝大家好运。这个错误是大型代码块中的野兽。我不知道如何找到我总是想念的一个参考。如果有人知道,请分享。

    让我们都知道你是怎么做出来的。我们都需要分享哪些有效,哪些无效。

    【讨论】:

    • 看起来我上面的“Word.InchesToPoints(0)”示例代码应该是“wrdApp.InchesToPoints(0)”来引用您的活动对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    相关资源
    最近更新 更多