SanFranExcel
New Member
- Joined
- Dec 26, 2008
- Messages
- 1
Hi,
I have an worksheet on which I will paste lists of transactions each month. I have a macro that takes those transactions, brings them over to a new worksheet, arranges, sorts, subtotals and adds headers to the transactions (grouped by name). I now want to create a new Word document for each group of transactions. Some of the entries into the Word document will be from single cells in Excel and will be pasted at certain bookmarked locations in a Word template. That part works fine for me. I also, however, would like to copy and paste all of the transactions for a particular group of transactions, along with the header directly over those transactions and the subtotal row, into the word document as well. I cannot figure out how to name the range that will be pasted into Excel.
For the sake of simplicity, below is sample code that I am working with trying to copy a tiny bit of text from a workbook called "test" into a Word template called "Stub" at the bookmark called "History". I want to paste everything cntrl+shift+up and cntrl+shift+right from the active cell.
The problem is with the red text.
Any suggestions would be much appreciated!
Sub Copy_Range_to_Word()
Dim appwd As Object
On Error GoTo notloaded
Set appwd = GetObject(, "Word.Application")
notloaded:
If Err.Number = 429 Then
Set appwd = CreateObject("Word.Application")
End If
appwd.Visible = True
With appwd
.Activate
.Documents.Add Template:=ThisWorkbook.Path & "\stub.dot"
ActiveDocument.Bookmarks("History").Range = Range("A" & ActiveCell.Row, Selection.End(xlUp)).Select & Range(Selection, Selection.End(x1toRight)).Select
End With
' Save the new document
appwd.ActiveDocument.SaveAs Filename:=ThisWorkbook.Path & "\test.doc"
' Close this new word document
appwd.ActiveDocument.Close
End Sub
I have an worksheet on which I will paste lists of transactions each month. I have a macro that takes those transactions, brings them over to a new worksheet, arranges, sorts, subtotals and adds headers to the transactions (grouped by name). I now want to create a new Word document for each group of transactions. Some of the entries into the Word document will be from single cells in Excel and will be pasted at certain bookmarked locations in a Word template. That part works fine for me. I also, however, would like to copy and paste all of the transactions for a particular group of transactions, along with the header directly over those transactions and the subtotal row, into the word document as well. I cannot figure out how to name the range that will be pasted into Excel.
For the sake of simplicity, below is sample code that I am working with trying to copy a tiny bit of text from a workbook called "test" into a Word template called "Stub" at the bookmark called "History". I want to paste everything cntrl+shift+up and cntrl+shift+right from the active cell.
The problem is with the red text.
Any suggestions would be much appreciated!
Sub Copy_Range_to_Word()
Dim appwd As Object
On Error GoTo notloaded
Set appwd = GetObject(, "Word.Application")
notloaded:
If Err.Number = 429 Then
Set appwd = CreateObject("Word.Application")
End If
appwd.Visible = True
With appwd
.Activate
.Documents.Add Template:=ThisWorkbook.Path & "\stub.dot"
ActiveDocument.Bookmarks("History").Range = Range("A" & ActiveCell.Row, Selection.End(xlUp)).Select & Range(Selection, Selection.End(x1toRight)).Select
End With
' Save the new document
appwd.ActiveDocument.SaveAs Filename:=ThisWorkbook.Path & "\test.doc"
' Close this new word document
appwd.ActiveDocument.Close
End Sub