2017-04-04 229 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 –