I have searched around but I am finding it hard to actually get a solution. I have a word doc (currently leaving as a docx rather than dotx) with a logo and some text at the beginning.
I have set a bookmark "startingPoint" after these and was hoping to then paste in the data copied from excel into the word document at / or after the bookmark. I found some code for bookmarks but cant seem to get it to work for me. It just moves everything down and pastes above it.
Any clues as to why? please
Bookmark code:
Sub UpdateBookmark(BookmarkToUpdate As String, PasteRange As Variant)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange = PasteRange
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
Code that I'm currently using to copy and paste.
Sub CopyExcelRngToWord() ' copies a range of data from Excel to a Word template
Dim xltblRange As Excel.Range
Dim ObjW As Word.Application
Dim ObjDoc As Word.Document
Dim ObjWTble As Word.Table
Dim templatename As String
templatename = ActiveWorkbook.Path & Application.PathSeparator & "Lab Template.docx"
Set xltblRange = ThisWorkbook.Worksheets("UIC").Range("$A$2:$J$24") ' fixed worksheet name and range for testing
On Error Resume Next
Set ObjW = GetObject(class:="Word.Application")
If ObjW Is Nothing Then Set ObjW = CreateObject(class:="Word.Application")
ObjW.Visible = True
ObjW.Activate
Set ObjD = ObjW.Documents.Open(templatename)
ObjD.PageSetup.Orientation = 1 'wdOrientLandscape
xltblRange .Copy
UpdateBookmark "StartingPoint", xltblRange ' not using as doesn't work
ObjD..Paragraphs(1).Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False
Set ObjWTble = WordDoc.Tables(1)
ObjWTble.AutoFitBehavior (wdAutoFitWindow)
ObjD.SaveAs ActiveWorkbook.Path & Application.PathSeparator & "Final_Report11.docx" 'fixed doc name for testing
ObjD.Close
End Sub
I have set a bookmark "startingPoint" after these and was hoping to then paste in the data copied from excel into the word document at / or after the bookmark. I found some code for bookmarks but cant seem to get it to work for me. It just moves everything down and pastes above it.
Any clues as to why? please
Bookmark code:
Sub UpdateBookmark(BookmarkToUpdate As String, PasteRange As Variant)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange = PasteRange
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
Code that I'm currently using to copy and paste.
Sub CopyExcelRngToWord() ' copies a range of data from Excel to a Word template
Dim xltblRange As Excel.Range
Dim ObjW As Word.Application
Dim ObjDoc As Word.Document
Dim ObjWTble As Word.Table
Dim templatename As String
templatename = ActiveWorkbook.Path & Application.PathSeparator & "Lab Template.docx"
Set xltblRange = ThisWorkbook.Worksheets("UIC").Range("$A$2:$J$24") ' fixed worksheet name and range for testing
On Error Resume Next
Set ObjW = GetObject(class:="Word.Application")
If ObjW Is Nothing Then Set ObjW = CreateObject(class:="Word.Application")
ObjW.Visible = True
ObjW.Activate
Set ObjD = ObjW.Documents.Open(templatename)
ObjD.PageSetup.Orientation = 1 'wdOrientLandscape
xltblRange .Copy
UpdateBookmark "StartingPoint", xltblRange ' not using as doesn't work
ObjD..Paragraphs(1).Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False
Set ObjWTble = WordDoc.Tables(1)
ObjWTble.AutoFitBehavior (wdAutoFitWindow)
ObjD.SaveAs ActiveWorkbook.Path & Application.PathSeparator & "Final_Report11.docx" 'fixed doc name for testing
ObjD.Close
End Sub