我從Excel自動化Word並使用以下代碼。當我第一次運行代碼時,我總是能夠毫無問題地運行代碼。但是,在第二種情況下,我總是得到這個錯誤。然後我必須手動關閉Word文件並再次運行代碼,它第一次運行平穩,而且第二次再次看到錯誤。Word自動化錯誤 - 服務器機器不存在
'On Error Resume Next
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Dim intChoice As Integer
Dim strPath As String
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
intChoice = Application.FileDialog(msoFileDialogOpen).Show
If intChoice <> 0 Then
'get the path selected
strPath = Application.FileDialog(_
msoFileDialogOpen).SelectedItems(1)
'opens the document
Set objdoc = objWord.Documents.Open(strPath)
With Documents(objdoc) '''This is where error points to in yellow '''
Set myrange = ActiveDocument.Content
''' My execution code here
end with
objWord.ActiveDocument.SaveAs ThisWorkbook.Path & "\" &
ActiveSheet.Range("E3").Value & "_MVR"
'objWord.ActiveDocument.Close
objdoc.Close
objWord.Quit
Set objdoc = Nothing
Set objWord = Nothing
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
由於你的代碼沒有縮進,你在「失蹤」你的錯誤振振有辭。如果intChoice <> 0 Then'內部的If語句內部「釋放」了Word對象'Set objWord = Nothing'。然而,無論如何,創建'Set objWord = CreateObject(「Word.Application」)',只需將'Set objWord = Nothing'移動到'If'之外,它將關閉您創建的新的** Word **實例。 –
我嘗試了您所建議的更改,但仍然收到相同的錯誤。 –