我从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 **实例。 –
我尝试了您所建议的更改,但仍然收到相同的错误。 –