I have a document template which needs to be filled in with data from my workbook - I've managed to get it to put the correct data at the correct part of the Word doc with bookmarks, but would like it to do a new doc for each line.
The code below will put the data in, and in column Y will put a yes when it has copied the data, however it currently tries doing every row in the same document rather than the new document with the pasted table in.
Public Sub openExistingWordFile()
Dim objWord
Dim objDoc
Dim objRange
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(".... Draft Invoice Template.doc")
objWord.Visible = True
objWord.Selection.WholeStory
objWord.Selection.Copy
R = Cells(Rows.Count, 1).End(xlUp).Row
For i = 6 To R
With Cells(i, 2)
If .Value <> "" And Cells(i, 25) = "" Then
Cells(i, 25) = "Yes"
Set objRange = objDoc.Bookmarks("OurRef").Range
objRange.InsertAfter Cells(i, 4)
Set objRange = objDoc.Bookmarks("WorkRef").Range
objRange.InsertAfter Cells(i, 5)
Set objRange = objDoc.Bookmarks("Location").Range
objRange.InsertAfter Cells(i, 7)
Set objRange = objDoc.Bookmarks("WorksType").Range
objRange.InsertAfter Cells(i, 11)
Set objRange = objDoc.Bookmarks("ReinCat").Range
objRange.InsertAfter Cells(i, 12)
Set objRange = objDoc.Bookmarks("TS").Range
objRange.InsertAfter Cells(i, 13)
Set objRange = objDoc.Bookmarks("Charge").Range
objRange.InsertAfter Cells(i, 18)
Set objRange = objDoc.Bookmarks("From").Range
objRange.InsertAfter Cells(i, 15)
Set objRange = objDoc.Bookmarks("To").Range
objRange.InsertAfter Cells(i, 16)
Set objRange = objDoc.Bookmarks("Days").Range
objRange.InsertAfter Cells(i, 17)
Set objRange = objDoc.Bookmarks("Total").Range
objRange.InsertAfter Cells(i, 24)
Set objRange = objDoc.Bookmarks("Date").Range
objRange.InsertDateTime DateTimeFormat:="d/M/yyyy"
objWord.Documents.Add DocumentType:=wdNewBlankDocument
objWord.Activate
objWord.Selection.PasteAndFormat (wdUseDestinationStylesRecovery)
End If
End With
Next i
End Sub
Next i
and overwrite values after each bookmark. – JvdVobjWord.Documents.Open(".....Draft Invoice Template.doc")
starting each iteration? – JvdV