运行到错误462:通过Excel VBA处理WORD时,远程服务器机器不存在 [英] Running into Error 462: The remote server machine does not exist when working with WORD via Excel VBA

查看:2202
本文介绍了运行到错误462:通过Excel VBA处理WORD时,远程服务器机器不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在excel vba中以编程方式打开一个word文件,并使用书签添加/编辑内容。但是,我发现在备用运行中,我得到'错误462:远程服务器不存在'错误。我研究了很多,并且理解这有一些要做的'不合格的参考。

I have been trying to open a word file programmatically in excel vba and adding/editing contents in it using bookmarks. However, I find that on alternate runs, I get the 'Error 462: The remote server does not exist" error. I researched a lot and understood this has something to do 'Unqualified references;.

但是我不明白如何将下面的代码片段更正为合格参考?有人可以帮忙吗?

However I don't understand how to correct the below snippet of code to qualified references? Can someone please help?

        Set exR = ActiveSheet.Range(TestIdCol & CStr(DataRowNum) & ":" & TestIdCol & CStr(RowEnd))

           ExistingEvidenceDoc = UseFileDialogOpen("Word Documents", "*.doc;*.docx")

           Set objWord = CreateObject("Word.Application")

           If ExistingEvidenceDoc <> "" Then
                Set objDoc = objWord.Documents.Open(ExistingEvidenceDoc)
           Else
                Exit Sub
           End If

           objWord.Visible = True
           Application.Wait Now() + TimeSerial(0, 0, 5)


           Set objSelection = objWord.Selection

           getExistingEvidences = ExistingTestEvidences(objDoc)
           o = DataRowNum
            For Each cell In exR
                If cell.Value <> "" And Not IsInArray(cell.Value, getExistingEvidences) Then
                    objSelection.Style = ActiveDocument.Styles("Heading 1")
                    objSelection.TypeText text:="Heading " + cell.Value
                    objSelection.TypeParagraph
                    objSelection.MoveLeft
                    objSelection.HomeKey Unit:=wdLine
                    objSelection.EndKey Unit:=wdLine, Extend:=wdExtend
                    objDoc.Bookmarks.Add Name:="BMrk" + CStr(o), Range:=objSelection
                    objSelection.Copy
                    ActiveSheet.Range("Q" + CStr(o)).Select
                           ActiveSheet.PasteSpecial Format:="Hyperlink", Link:=False, DisplayAsIcon _
                                   :=False
                    objSelection.MoveRight

                    'objSelection.Style = ActiveDocument.Styles("Paragraph")
                    objSelection.TypeText text:=Range(DescriptionCol + CStr(cell.Row)).Value
                    objSelection.TypeParagraph


                ElseIf IsInArray(cell.Value, getExistingEvidences) = False Then
                    objSelection.EndKey
                    objSelection.Style = ActiveDocument.Styles("Heading 1")
                    objSelection.TypeText text:="Heading " + cell.Value
                    objSelection.TypeParagraph
                    objSelection.MoveLeft
                    objSelection.HomeKey Unit:=wdLine
                    objSelection.EndKey Unit:=wdLine, Extend:=wdExtend
                    objDoc.Bookmarks.Add Name:="BMrk" + CStr(o), Range:=objSelection
                    objSelection.Copy
                    ActiveSheet.Range("Q" + CStr(o)).Select
                           ActiveSheet.PasteSpecial Format:="Hyperlink", Link:=False, DisplayAsIcon _
                                   :=False
                    objSelection.MoveRight

                    'objSelection.Style = ActiveDocument.Styles("Paragraph")
                    objSelection.TypeText text:=Range(DescriptionCol + CStr(cell.Row)).Value
                    objSelection.TypeParagraph
                End If

                o = o + 1

            Next cell


        MyErrorHandler:
                MsgBox "SeeHeadingPageNumber" & vbCrLf & vbCrLf & "Err = " & Err.Number & vbCrLf & "Description: " & Err.Description

另外,无论我定义的任何exR范围,它完成了整个范围的执行,但在结束MyErrorHandler被调用,我不明白为什么?有理由吗?

Additionaly, whatever exR range I define, it completes execution for the entire range but at the end MyErrorHandler is invoked, I don't understand why? Is there a reason for it?

请各位,帮助我...你的帮助非常感激。
提前感谢

Please guys, help me...your help is very much appreciated. Thanks in advance

推荐答案

您有两个不合格的Word对象引用:

You have two unqualified references to Word objects:

objSelection.Style = ActiveDocument.Styles("Heading 1")

出现两次,需要:

objSelection.Style = objWord.ActiveDocument.Styles("Heading 1")

否则,您将创建一个不能销毁的Word的隐式引用你的代码。

Otherwise you're creating an implicit reference to Word that you can't destroy in your code.

这篇关于运行到错误462:通过Excel VBA处理WORD时,远程服务器机器不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆