With the below code, I open a Word document that grabs different ranges etc to produce what's needed. It works perfectly, but only for the first time. On subsequent runs, I get "Error 462 - The remote server does not exist or is unavailable". My research from forums suggests this is common but I can't seem to identify the cause. Any help would be much appreciated.
VBA Code:
Sub ExcelRangeToWordv21()
Dim tbl As Excel.Range
Dim WordApp As Word.Application
Dim myDoc As Word.Document
Dim WordTable As Word.Table
'Optimize Code
Application.ScreenUpdating = False
Application.EnableEvents = False
'Copy Range from Excel
Set tbl = ThisWorkbook.Worksheets("Marking Guides (2)").Range("b9:i25").SpecialCells(xlCellTypeConstants, 3)
Set Header = ThisWorkbook.Worksheets("Marking Guides (2)").Range("b1:i7")
Set Footer = ThisWorkbook.Worksheets("Marking Guides (2)").Range("b27:i28")
Set Sheet = ThisWorkbook.Worksheets("Marking Guides (2)")
'If MS Word is already open
' Set WordApp = GetObject("Word.Application")
'If MS Word is not already open then open MS Word
If WordApp Is Nothing Then Set WordApp = CreateObject("Word.Application")
'Make MS Word Visible and Active
WordApp.Visible = True
'Create a New Document
Set myDoc = WordApp.Documents.Add
'Copy Header range
Sheet.Select
Header.Select
Selection.Copy
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
'Copy Footer range
Sheet.Select
Footer.Select
'Set Word Margins
With WordApp.ActiveDocument.PageSetup
.Orientation = wdOrientLandscape
.TopMargin = CentimetersToPoints(0.5)
.BottomMargin = CentimetersToPoints(1)
.LeftMargin = CentimetersToPoints(1)
.RightMargin = CentimetersToPoints(1)
End With
'Change the view to header & footer
If WordApp.ActiveWindow.View.SplitSpecial <> wdPaneNone Then
WordApp.ActiveWindow.Panes(2).Close
End If
'Select the Header range and paste as image
ThisWorkbook.Worksheets("Marking Guides (2)").Range("b1:i7").Copy
WordApp.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
WordApp.Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
Placement:=wdInLine, DisplayAsIcon:=False
'Select the Footer range and paste as image
ThisWorkbook.Worksheets("Marking Guides (2)").Range("b27:i28").Copy
WordApp.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
WordApp.Selection.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
WordApp.Selection.Borders(wdBorderTop).LineWidth = wdLineWidth150pt
WordApp.Selection.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
WordApp.Selection.Borders(wdBorderBottom).LineWidth = wdLineWidth150pt
WordApp.Selection.InsertAfter ("Comments:")
WordApp.Selection.InsertAfter Chr(13)
WordApp.ActiveWindow.View.Type = wdNormalView
WordApp.ActiveWindow.View.Type = wdPrintView
'Copy Excel Table range
Sheet.Select
tbl.Copy
'Paste Table into Word
myDoc.Content.Paste
'Autofit Table so it fits inside Word Document
Set WordTable = myDoc.Tables(1)
WordTable.AutoFitBehavior (wdAutoFitWindow)
WordApp.ActiveDocument.Paragraphs.SpaceAfter = 0
WordTable.RightPadding = CentimetersToPoints(0.1)
Application.ScreenUpdating = True
Application.EnableEvents = True
'Clear The Clipboard
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Class Setup").Select
WordApp.Activate
End Sub