DropBear
05-26-2014, 09:29 PM
Hi All,
I'm hoping you can help, as I have tried a number of different things and cant get it working.
What I am trying to do:
I am looping through rows in an Excel spreadsheet.
If certain conditions are met, I am opening 2 Word documents, copying and pasting a table from one document to the other.
Then using other data in excel, I am populating data into the table at various bookmarks located in the table, then removing the bookmarks (thus the reason for copying and pasting the table instead of just creating it on the fly).
This is repeated so that I have a table in Word which expands and is populated depending on the number of rows in Excel which match the criteria.
This is all working with no issues apart from going to the line under the last table in order to add another row to the table. The first cell in the bottom row of the table is selected, so all I should have to do is:
docTemplate.Selection.MoveDown Unit:=wdLine, Count:=1
However, this is not working for me. I have unsuccessfully tried a number of different things:
.Selection.MoveDown Unit:=wdLine, Count:=1
.Selection.MoveDown Unit:=wdParagraph, Count:=1
.Selection.EndKey Unit:=wdStory isnt working either!
Can someone else please offer a suggestion?
Thanks!
Relevant code is below:
Dim appWord As Word.Application
Dim docTemplate As Word.Document
Dim docText As Word.Document
'Open Excel Data file
Set wbData = Workbooks.Open(sData)
'Open Word Application
Set appWord = CreateObject("word.Application")
'Loop through all the data
wbData.Activate
For Each rcell In Range("A2", Range("A36645").End(xlUp))
'Open new word template and word document containing text
Set docText = appWord.Documents.Open(sDocText, ReadOnly:=True)
Set docTemplate = appWord.Documents.Add(sTemplate)
appWord.Visible = True
'Insert the data
docTemplate.Activate
IF .....
ElseIf Left(rcell.Offset(0, 8), 5) = "RATE=" Then
' Insert bookmark from other document with the table
docText.Activate
Set objRangeText = docText.Bookmarks("RATES_TABLE").Range
objRangeText.Copy
docTemplate.Activate
Set objRange = docTemplate.Bookmarks("INSERT_RATES_TABLE").Range
objRange.Paste
Set objRangeText = Nothing
Set objRange = Nothing
'Remove named range
docTemplate.Bookmarks("INSERT_RATES_TABLE").Delete
'Goto next line
With docTemplate
.Activate
.Bookmarks("RATE").Select
.Selection.MoveDown Unit:=wdLine, Count:=1
End With
etc
I'm hoping you can help, as I have tried a number of different things and cant get it working.
What I am trying to do:
I am looping through rows in an Excel spreadsheet.
If certain conditions are met, I am opening 2 Word documents, copying and pasting a table from one document to the other.
Then using other data in excel, I am populating data into the table at various bookmarks located in the table, then removing the bookmarks (thus the reason for copying and pasting the table instead of just creating it on the fly).
This is repeated so that I have a table in Word which expands and is populated depending on the number of rows in Excel which match the criteria.
This is all working with no issues apart from going to the line under the last table in order to add another row to the table. The first cell in the bottom row of the table is selected, so all I should have to do is:
docTemplate.Selection.MoveDown Unit:=wdLine, Count:=1
However, this is not working for me. I have unsuccessfully tried a number of different things:
.Selection.MoveDown Unit:=wdLine, Count:=1
.Selection.MoveDown Unit:=wdParagraph, Count:=1
.Selection.EndKey Unit:=wdStory isnt working either!
Can someone else please offer a suggestion?
Thanks!
Relevant code is below:
Dim appWord As Word.Application
Dim docTemplate As Word.Document
Dim docText As Word.Document
'Open Excel Data file
Set wbData = Workbooks.Open(sData)
'Open Word Application
Set appWord = CreateObject("word.Application")
'Loop through all the data
wbData.Activate
For Each rcell In Range("A2", Range("A36645").End(xlUp))
'Open new word template and word document containing text
Set docText = appWord.Documents.Open(sDocText, ReadOnly:=True)
Set docTemplate = appWord.Documents.Add(sTemplate)
appWord.Visible = True
'Insert the data
docTemplate.Activate
IF .....
ElseIf Left(rcell.Offset(0, 8), 5) = "RATE=" Then
' Insert bookmark from other document with the table
docText.Activate
Set objRangeText = docText.Bookmarks("RATES_TABLE").Range
objRangeText.Copy
docTemplate.Activate
Set objRange = docTemplate.Bookmarks("INSERT_RATES_TABLE").Range
objRange.Paste
Set objRangeText = Nothing
Set objRange = Nothing
'Remove named range
docTemplate.Bookmarks("INSERT_RATES_TABLE").Delete
'Goto next line
With docTemplate
.Activate
.Bookmarks("RATE").Select
.Selection.MoveDown Unit:=wdLine, Count:=1
End With
etc