Excel automation fails second time code runs

Mr Sin

New Member
Joined
Sep 9, 2014
Messages
4
After running the code, its shows "the remote server machine does not exist or is not available".
Could anyone offer help on reviewing my code. Many thanks.

Sub CopyFilterResult()
' This loop repeats for generate multiple word document by ID
' in the range
Dim objWordApp As Word.Application
Dim objWord As Word.Document
Dim i As Long
Dim lastcell As Long
On Error GoTo errHandle

'--criteria
With Worksheets("id")
lastcell = Sheets("id").Range("A" & Rows.Count).End(xlDown).Row
For i = 2 To lastcell
Worksheets("Sheet4").Cells.Clear
Worksheets("Target").Range("A2").Clear
Sheets("Target").Cells(2, 1).Value = Sheets("id").Cells(i, 1).Value

'--select target content to Sheet 4
With Worksheets("Sheet1")
.Range("A1").CurrentRegion.AdvancedFilter _
Action:=xlFilterCopy, CriteriaRange:=Worksheets("Target").Range("A1:A2").SpecialCells(xlCellTypeVisible), _
CopyToRange:=Worksheets("Sheet4").Range("A1"), Unique:=True
End With

'--copy excel content to word
With Worksheets("Sheet4")
Set rngCopy = Worksheets("Sheet4").Range("A1:D" & .Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeVisible)
End With

Set objWordApp = New Word.Application
Set objWord = objWordApp.Documents.Add
objWord.Application.Visible = True

'--paste content
With objWord.Application
.Selection.Style = .ActiveDocument.Styles("Normal")
.Selection.TypeParagraph
rngCopy.Copy
.Selection.PasteExcelTable False, False, False
End With

objWord.SaveAs
FPath = "XXXXXXXXXX"
FName = Worksheets("Sheet4").Range("A2").Value
ActiveDocument.SaveAs Filename:=FPath & "\" & FName & ".doc", _
FileFormat:=wdFormatDocument
objWord.Close Savechanges:=True
objWordApp.Quit
Next i
End With
errExit:
Set objSel = Nothing
Set objWord = Nothing
Set objWordApp = Nothing
Exit Sub
errHandle:
MsgBox Err.Description
Resume errExit
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top