我一直試圖在Excel VBA中以編程方式打開一個Word文件,並使用書籤在其中添加/編輯內容。然而,我發現在交替運行時,我得到'錯誤462:遠程服務器不存在'的錯誤,我研究了很多,並且明白這有些事情要做'不合格的參考文獻;遇到錯誤462:通過Excel使用WORD工作時,遠程服務器機器不存在VBA
但是我沒有了解如何正確的代碼,以合格的引用下面的代碼片段?是否有人可以幫助?
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
Additionaly,無論EXR範圍我定義,它完成整個範圍,但在最後MyErrorHandler執行被調用時,我不不明白爲什麼嗎?有沒有理由呢?
請大家,幫幫我...你的幫助非常讚賞特德。 在此先感謝
您的錯誤處理程序會被調用,因爲您在到達之前沒有退出該子程序。在MyErrorHandler之前添加'exit sub'。此外,我沒有看到任何地方的任何錯誤轉到 – 2014-09-25 04:37:20
@ Nick.McDermaid謝謝,這解決了一個問題......但主要問題仍然是關於遇到錯誤462。你能幫我解決嗎? :( – 2014-09-25 07:06:55