I have an excel document containing marks from student papers.
There is a summary tab which collates the marks into a more useable format for the students.
I have cobbled together some VBA code which opens a word doc and then steps through each students record, copying the output page and dropping it across into the word document.
The code runs and does what it is supposed to apart from failing part way through, at a different point each time.
I've tried paste and pastespecial, both fail in same way, this is where debugger indicates issue.
Error codes are usually 4605, though I have had 4198 and runtime error -2147023170
Hopefully someone can help a teacher out!
Code below
Sub Trilogy_output()
Dim x As Integer
Dim wdApp As Word.Application
' openword fdoc
Set wdApp = New Word.Application
With wdApp
.Visible = True
.Activate
.Documents.Add
End With
' Select main data sheet
Sheets("Physics").Select
Range("A12").Select
' Set numrows = number of rows of data.
NumRows = Range("A12", Range("A12").End(xlDown)).Rows.Count
' Select starting cell.
Range("A12").Select
' Establish "For" loop to loop "numrows" number of times.
For x = 1 To NumRows
' paste name to output sheet
Selection.Copy
Sheets("Trilogy Output").Select
Range("B2").Select
ActiveSheet.Paste
' copy sheet to word
Range("A1:G40").Select
Selection.Copy
With wdApp.Selection
' .Paste
.PasteSpecial DataType:=wdPasteEnhancedMetafile, Placement:=wdInLine
.InsertBreak Type:=7
End With
Application.CutCopyMode = False
' Selects cell down 1 row from active cell.
Sheets("Physics").Select
ActiveCell.Offset(1, 0).Select
Next
Application.ScreenUpdating = True
End Sub