问题

我在Excel VBA中以编程方式打开Word文件并使用书签添加/编辑内容.

我发现在替代跑步时,我明白了

错误462:不存在远程服务器

我研究并理解这与“不合格的引用”有关.

我不明白如何将代码更正为限定引用.

         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.有没有理由?

  最佳答案

您对Word对象有两个无保留引用:

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

出现两次,需要是:

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

否则,您正在创建一个隐式引用 Word,您不能在代码中销毁它。

  相同标签的其他问题

excelvba