2017-04-04 232 views
0

我從Excel自動化Word並使用以下代碼。當我第一次運行代碼時,我總是能夠毫無問題地運行代碼。但是,在第二種情況下,我總是得到這個錯誤。然後我必須手動關閉Word文件並再次運行代碼,它第一次運行平穩,而且第二次再次看到錯誤。Word自動化錯誤 - 服務器機器不存在

enter image description here

'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 
+0

由於你的代碼沒有縮進,你在「失蹤」你的錯誤振振有辭。如果intChoice <> 0 Then'內部的If語句內部「釋放」了Word對象'Set objWord = Nothing'。然而,無論如何,創建'Set objWord = CreateObject(「Word.Application」)',只需將'Set objWord = Nothing'移動到'If'之外,它將關閉您創建的新的** Word **實例。 –

+0

我嘗試了您所建議的更改,但仍然收到相同的錯誤。 –

回答

1

按照上面我的筆記,嘗試下面的代碼:

Option Explicit 

Sub AutoWord() 

Dim objWord As Object 
Dim objdoc As Object 
Dim intChoice As Integer 
Dim strPath As String 
Dim myRange As Range 

'On Error Resume Next 
Application.ScreenUpdating = False 
Application.EnableEvents = False 
Application.Calculation = xlCalculationManual 
Application.DisplayAlerts = False 

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 objdoc 
     ' all your code related to opened Word document goes here 

    End With 
    objdoc.SaveAs ThisWorkbook.Path & "\" & ActiveSheet.Range("E3").Value & "_MVR" 

    objdoc.Close 
End If 

objWord.Quit 
Set objdoc = Nothing 
Set objWord = Nothing 

Application.DisplayAlerts = True 
Application.EnableEvents = True 
Application.Calculation = xlCalculationAutomatic 
Application.ScreenUpdating = True 

End Sub 
+0

與文檔(objdoc) - 我仍然在這裏得到錯誤。 –

+0

@AbhinavBajpai當你在調試模式下運行時,在哪一行? –

+0

在調試模式下,代碼會在此行發生錯誤。 intChoice = Application.FileDialog(msoFileDialogOpen).Show –