Copy and paste Range Problem

X

XmlAdoNewbie

Hi All,
I have developed a macro in word with VBA and i am encountering a
slight problem with it, i am hoping someone can help me out.

I have 2 bookmarks in a word doc, the first one called "B" and then
right underneith a second bookmark called "A". The macro reads in a
series of word doc's and places the text at the bookmark called "A",
it then copies the text and pastes it at bookmark "B". (to eventually
create a whole document based on choices made by a user) For the most
part this works just fine, i encounter the error when the text i am
copying is inside a table.

Here is the code i am using, if you want to test you can copy this
right into the word vba editor and insert 2 bookmarks on the word doc,
the first called "B" and underneith that one another called "A" and
press play as long as you have a word file on your c: drive called
test.doc inside this file, create a two column table with one row and
enter some text into both columns. I get an error when i encounter the
line that reads

Set myRange = ActiveDocument.Range(Start:=BookMarkStart,
End:=BookMarkEnd)

it says "VALUE OUT OF RANGE"... Can anyone help me?

Thanks in advance!!

Option Explicit
Dim BookMarkStart As Integer
Dim BookMarkEnd As Integer
Public myRange As Word.Range
Dim i As Integer

Private Sub cmdTest_Click()

returnText
placeTextInDocument

End Sub

Public Sub returnText()

Dim temp As String

Selection.GoTo what:=wdGoToBookmark, Name:="A"
Selection.InsertFile FileName:="c:\test.doc"

BookMarkStart = ActiveDocument.Bookmarks("A").Start
BookMarkEnd = Len(ActiveDocument.Content) - 1
Set myRange = ActiveDocument.Range(Start:=BookMarkStart,
End:=BookMarkEnd)

myRange.Copy

End Sub


Public Sub placeTextInDocument()

On Error Resume Next

With Selection
.GoTo what:=wdGoToBookmark, Name:="B"
.Select
.Paste
End With

Selection.GoTo what:=wdGoToBookmark, Name:="A"

i = Len(myRange) + i

myRange.Delete

End Sub

~~Erin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top