【问题标题】:"Run-time error 462 : The remote server machine does not exist or is unavailable" when running VBA code a second time第二次运行 VBA 代码时出现“运行时错误 462:远程服务器机器不存在或不可用”
【发布时间】:2023-03-06 14:13:01
【问题描述】:

下面的代码第一次运行时运行良好,但是当我需要第二次运行时,它给了我这个错误:

运行时错误“462”:远程服务器机器不存在或不可用

它不会一直发生,所以我想这与在后台运行的 Word(不是)有关...?我在这里错过了什么?

Sub Docs()

Sheets("examplesheet").Select

Dim WordApp1 As Object
Dim WordDoc1 As Object

Set WordApp1 = CreateObject("Word.Application")
WordApp1.Visible = True
WordApp1.Activate

Set WordDoc1 = WordApp1.Documents.Add

Range("A1:C33").Copy

WordApp1.Selection.PasteSpecial Link:=False, DataType:=wdPasteRTF, _
Placement:=wdInLine, DisplayAsIcon:=False

Application.Wait (Now + TimeValue("0:00:02"))

WordDoc1.PageSetup.TopMargin = CentimetersToPoints(1.4)
WordDoc1.PageSetup.LeftMargin = CentimetersToPoints(1.5)
WordDoc1.PageSetup.BottomMargin = CentimetersToPoints(1.5)

' Control if folder exists, if not create folder
If Len(Dir("F:\documents\" & Year(Date), vbDirectory)) = 0 Then
MkDir "F:\documents\" & Year(Date)
End If

WordDoc1.SaveAs "F:\documents\" & Year(Date) & "\examplename " & Format(Now, "YYYYMMDD") & ".docx"

WordDoc1.Close
'WordApp1.Quit

Set WordDoc1 = Nothing
Set WordApp1 = Nothing

Windows("exampleworkbook.xlsm").Activate
Sheets("examplesheet").Select
Application.CutCopyMode = False
Range("A1").Select


' export sheet 2 to Word
Sheets("examplesheet2").Select

Set WordApp2 = CreateObject("Word.Application")
WordApp2.Visible = True
WordApp2.Activate

Set WordDoc2 = WordApp2.Documents.Add

Range("A1:C33").Copy

WordApp2.Selection.PasteSpecial Link:=False, DataType:=wdPasteRTF, _
Placement:=wdInLine, DisplayAsIcon:=False

Application.Wait (Now + TimeValue("0:00:02"))

WordDoc2.PageSetup.LeftMargin = CentimetersToPoints(1.5)
WordDoc2.PageSetup.TopMargin = CentimetersToPoints(1.4)
WordDoc2.PageSetup.BottomMargin = CentimetersToPoints(1.5)

WordDoc2.SaveAs "F:\files\" & Year(Date) & "\name" & Format(Now, "YYYYMMDD") & ".docx"

WordDoc2.Close
'WordApp2.Quit

Set WordDoc2 = Nothing
Set WordApp2 = Nothing

Windows("exampleworkbook.xlsm").Activate
Sheets("examplesheet2").Select
Application.CutCopyMode = False
Range("A1").Select

' Variables Outlook
Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngCc As Range
Dim rngSubject As Range
Dim rngBody As Range
Dim rngAttach1 As Range
Dim rngAttach2 As Range
Dim numSend As Integer

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)

' Outlook
On Error GoTo handleError

With Sheets("Mail")
    Set rngTo = .Range("B11")
    Set rngCc = .Range("B12")
    Set rngSubject = .Range("B13")
    Set rngBody = .Range("B14")
    Set rngAttach1 = .Range("B15")
    Set rngAttach2 = .Range("B16")
End With

With objMail
    .To = rngTo.Value
    .Subject = rngSubject.Value
    .Cc = rngCc.Value
    '.Body = rngBody.Value
    .Body = "Hi," & _
            vbNewLine & vbNewLine & _
            rngBody.Value & _
            vbNewLine & vbNewLine & _
            "Kind regards,"
    .Attachments.Add rngAttach1.Value
    .Attachments.Add rngAttach2.Value
    .Display
     Application.Wait (Now + TimeValue("0:00:01"))
     Application.SendKeys "%s"
  ' .Send       ' Instead of .Display, you can use .Send to send the email _
                or .Save to save a copy in the drafts folder
End With

numSend = numSend + 1

GoTo skipError

handleError:
numErr = numErr + 1
oFile.WriteLine "*** ERROR *** Email for account" & broker & " not sent. Error: " & Err.Number & " " & Err.Description
skipError:

On Error GoTo 0

MsgBox "Sent emails: " & numSend & vbNewLine & "Number of errors: " & numErr, vbOKOnly + vbInformation, "Operation finished"

GoTo endProgram

cancelProgram:
MsgBox "No mails were sent.", vbOKOnly + vbExclamation, "Operation cancelled"

endProgram:
Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
Set rngAttach1 = Nothing
Set rngAttach2 = Nothing

End Sub

【问题讨论】:

  • 哪行代码产生错误?
  • 如果你运行这个然后打开任务管理器,你的进程列表中有没有winword.exe?
  • @MatthewD WordDoc1.PageSetup.TopMargin = CentimetersToPoints(1.4)
  • 您是否设置了对 Word 的引用?如果不是,像 wdPasteRTFwdInLine 这样的常量是空变量。另外,你为什么不退出你打开的 Word 实例?
  • 除了不关闭你打开的Word实例。您不需要创建 2 个单独的单词实例。您可以拥有一个实例和任意数量的文档。然后关闭该实例。您可能还希望使用早期绑定(如果您知道使用该代码的每个人都会引用 Word 集(或者将从同一个 Excel 文件中使用它)。

标签: excel vba outlook ms-word


【解决方案1】:

第一个问题:Run-time error '462':远程服务器机器不存在或不可用。

这里的问题是使用:

  1. 迟投标:Dim Smthg As Object
  2. 隐式引用:Dim Smthg As Range 而不是
    Dim Smthg As Excel.RangeDim Smthg As Word.Range

所以你需要完全限定你设置的所有变量(我已经在你的代码中做到了)



第二个问题

您使用多个 Word 实例,而您只需要一个来处理多个文档

所以不要每次都创建一个新的:

Set WordApp = CreateObject("Word.Application")

您可以获得一个打开的实例(如果有的话)或使用该代码创建一个:

On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number > 0 Then Set WordApp = CreateObject("Word.Application")
On Error GoTo 0

并且一旦你把它放在你的触发的开始,你可以使用这个实例直到结束结束之前,退出它以避免运行多个实例。


这是您的代码经过审查和清理,请看:

Sub Docs()

Dim WordApp As Word.Application
Dim WordDoc As Word.Document

' Control if folder exists, if not create folder
If Len(Dir("F:\documents\" & Year(Date), vbDirectory)) = 0 Then MkDir "F:\documents\" & Year(Date)

' Get or Create a Word Instance
On Error Resume Next
Set WordApp = GetObject(, "Word.Application")
If Err.Number > 0 Then Set WordApp = CreateObject("Word.Application")
On Error GoTo 0

Workbooks("exampleworkbook.xlsm").Sheets("examplesheet").Range("A1:C33").Copy

With WordApp
    .Visible = True
    .Activate
    Set WordDoc = .Documents.Add
    .Selection.PasteSpecial Link:=False, DataType:=wdPasteRTF, _
                Placement:=wdInLine, DisplayAsIcon:=False
End With

With Application
    .Wait (Now + TimeValue("0:00:02"))
    .CutCopyMode = False
End With

With WordDoc
    .PageSetup.TopMargin = WordApp.CentimetersToPoints(1.4)
    .PageSetup.LeftMargin = WordApp.CentimetersToPoints(1.5)
    .PageSetup.BottomMargin = WordApp.CentimetersToPoints(1.5)
    .SaveAs "F:\documents\" & Year(Date) & "\examplename " & Format(Now, "YYYYMMDD") & ".docx"
    .Close
End With

' export sheet 2 to Word
Workbooks("exampleworkbook.xlsm").Sheets("examplesheet2").Range("A1:C33").Copy

Set WordDoc = WordApp.Documents.Add
WordApp.Selection.PasteSpecial Link:=False, DataType:=wdPasteRTF, _
                        Placement:=wdInLine, DisplayAsIcon:=False
Application.Wait (Now + TimeValue("0:00:02"))

With WordDoc
    .PageSetup.LeftMargin = WordApp.CentimetersToPoints(1.5)
    .PageSetup.TopMargin = WordApp.CentimetersToPoints(1.4)
    .PageSetup.BottomMargin = WordApp.CentimetersToPoints(1.5)
    .SaveAs "F:\files\" & Year(Date) & "\name" & Format(Now, "YYYYMMDD") & ".docx"
    .Close
End With

Application.CutCopyMode = False
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing

' Variables Outlook
Dim objOutlook As Outlook.Application
Dim objMail As Outlook.MailItem
Dim rngTo As Excel.Range
Dim rngCc As Excel.Range
Dim rngSubject As Excel.Range
Dim rngBody As Excel.Range
Dim rngAttach1 As Excel.Range
Dim rngAttach2 As Excel.Range
Dim numSend As Integer


On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If Err.Number > 0 Then Set objOutlook = CreateObject("Outlook.Application")
On Error GoTo 0


Set objMail = objOutlook.CreateItem(0)

' Outlook
On Error GoTo handleError

With Sheets("Mail")
    Set rngTo = .Range("B11")
    Set rngCc = .Range("B12")
    Set rngSubject = .Range("B13")
    Set rngBody = .Range("B14")
    Set rngAttach1 = .Range("B15")
    Set rngAttach2 = .Range("B16")
End With

With objMail
    .To = rngTo.Value
    .Subject = rngSubject.Value
    .CC = rngCc.Value
    '.Body = rngBody.Value
    .Body = "Hi," & _
            vbNewLine & vbNewLine & _
            rngBody.Value & _
            vbNewLine & vbNewLine & _
            "Kind regards,"
    .Attachments.Add rngAttach1.Value
    .Attachments.Add rngAttach2.Value
    .Display
     Application.Wait (Now + TimeValue("0:00:01"))
     Application.SendKeys "%s"
  ' .Send       ' Instead of .Display, you can use .Send to send the email _
                or .Save to save a copy in the drafts folder
End With

numSend = numSend + 1

GoTo skipError

handleError:
numErr = numErr + 1
oFile.WriteLine "*** ERROR *** Email for account" & broker & " not sent. Error: " & Err.Number & " " & Err.Description
skipError:

On Error GoTo 0

MsgBox "Sent emails: " & numSend & vbNewLine & "Number of errors: " & numErr, vbOKOnly + vbInformation, "Operation finished"

GoTo endProgram

cancelProgram:
MsgBox "No mails were sent.", vbOKOnly + vbExclamation, "Operation cancelled"

endProgram:
Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
Set rngAttach1 = Nothing
Set rngAttach2 = Nothing

End Sub

【讨论】:

  • 感谢您清理代码,但运行两次仍会在此行出现相同的 462 错误:.PageSetup.TopMargin = CentimetersToPoints(1.4)
  • 事实上,它似乎来自于你的变量的输入,让我完成审查并编辑我的帖子! ;)
  • 这正是 Cindy Meister 所建议的,并且有效。因为您还查看了我的代码,所以我将此标记为正确答案。谢谢!
  • 谢谢,看看帖子编辑,我包含了一些关于已修复错误的解释! ;)
【解决方案2】:

如果这是在 Excel 中运行,那么您可能需要指定 CentimetersToPoints 来自 Word 库。就目前而言,VBA 必须猜测,有时它可能无法找到它。所以试试:

wdApp.CentimetersToPoints

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 2015-12-08
    相关资源
    最近更新 更多