PDA

View Full Version : Cant go to next line (driving me insane!)



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

macropod
05-27-2014, 02:17 AM
Why on earth would you be trying to use selection manipulation? What's wrong with the .Rows.Add method? See: http://www.msofficeforums.com/word-vba/13257-any-insertrowsbelow-tables-object.html

DropBear
05-27-2014, 03:29 PM
Thanks for your response, macropod, but....

The row that I am adding is being copied and pasted for another word document. Based on the data, a different row (or rows) will be pasted, containing bookmarks and possibly different column sizes. So it's not quite as easy as just adding a new row.

Any other suggestions?

macropod
05-27-2014, 04:19 PM
It would have been helpful had you thought to mention the differences in column widths in your first post. In that scenario, neither what you're trying to do via 'docTemplate.Selection.MoveDown Unit:=wdLine, Count:=1' nor the .Rows.Add method will work, since the inserted rows will only ever reflect the formatting of the one they're inserted after. Besides, I can't see how adding a row will help you with the bookmarks, etc. Instead, you could use the .FormattedText method. That will replicate the layout, bookmarks, etc. from the source. All you need do is define the destination range as being immediately after the table, so the new data get appended to it rather than being inserted in it.

PS: Your code is quite inefficient. Try:

Dim appWord As Word.Application
Dim docTemplate As Word.Document
Dim docText As Word.Document
Dim wdRngDest As Word.Range

'Open Excel Data file
Set wbData = Workbooks.Open(sData)

'Open Word Application
Set appWord = CreateObject("word.Application")
appWord.Visible = True
'Loop through all the data
wbData.Activate

'Open word document containing text
Set docText = appWord.Documents.Open(sDocText, ReadOnly:=True)
For Each rcell In Range("A2", Range("A36645").End(xlUp))
'Create new word document based on template
Set docTemplate = appWord.Documents.Add(sTemplate)
'Insert the data
docTemplate.Activate
If x Then
'...
ElseIf Left(rcell.Offset(0, 8), 5) = "RATE=" Then
' Insert bookmark from other document with the table
Set wdRngDest = docTemplate.Bookmarks("INSERT_RATES_TABLE").Range
wdRngDest.Collapse wdCollapseEnd
wdRngDest.FormattedText = _
Documents(docText).Bookmarks("RATES_TABLE").Range.FormattedText
' Populate the newly added content.
There is no need to activate either document, to select anything, or to delete bookmarks. Simply replicating bookmarked content, whether via copy/paste or .FormattedText is sufficient to remove previous the instance of a bookmark.

PPS: When posting code, please use the code tags. They're indicated by the # tag on the posting/reply pane.

DropBear
05-27-2014, 09:32 PM
Thanks macropod!

However, the row is being inserted after the bookmark. Any way of inserting if before the bookmark? (That way the table grows down the page instead of up)

Thanks.

macropod
05-27-2014, 09:49 PM
I'm not sure I understand your meaning - tables only grow 'down'. You could try changing:
docTemplate.Bookmarks("INSERT_RATES_TABLE").Range
to:
docTemplate.Bookmarks("INSERT_RATES_TABLE").Range.Tables(1).Range

DropBear
05-29-2014, 04:16 PM
Thanks. What I mean is that the row is inserted after the bookmark. So if I insert another row at the bookmark it is inserted after the bookmark and before the other row. This is why I was trying to move the selection point down.

macropod
05-29-2014, 07:11 PM
So far I've given you two ways for handling the row addition. If neither of them does what you want, you're going to have to give more detail about what it is you do want. your last post doesn't indicate you've even tried the code in post #6.