Deleting/Adding table after bookmark - Word VBA

Hi all,

Im running a sub whereby im copying a table and pasting it to a bookmark placeholder (duplicating). The below works well but i want to be able to replace the contents of "bmkTest2a" everytime the sub is run, rather then appending to it. Ive tried a few things to no avail.

ActiveDocument.Tables(4).Range.Copy
Selection.Collapse Direction:=wdCollapseStart
Selection.GoTo What:=wdGoToBookmark, Name:="bmkTest2a"
Selection.Paste


Thanks
John
sydpoloAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris BottomleySoftware Quality Lead EngineerCommented:
See http://word.mvps.org/faqs/macrosvba/InsertingTextAtBookmark.htm

i.e.

Sub bkm()
Dim BMRange As Range
'Identify current Bookmark range and insert text
Set BMRange = ActiveDocument.Bookmarks("bmkTest2a").Range
BMRange.Text = "Hello world 2"
'Re-insert the bookmark
ActiveDocument.Bookmarks.Add "bmkTest2a", BMRange

End Sub

Open in new window

0
sydpoloAuthor Commented:
Hi Chris,

Im not quite sure how to incorporate this into my current sub?
Also unfortunately the range is not predefined, so i dont think theres any use for this line:

BMRange.Text = "Hello world 2"

As i said, i need to be able to copy a table in a document (which is not predefined), and paste it into another section of the document. Also, if the user makes changes to the original table they should be able to run the sub and that would replace what is already at "bmkTest2a" location (not add to it).

Thanks
0
Chris BottomleySoftware Quality Lead EngineerCommented:
I realised afterwards the key being copy a table.

I have played around somewhat as it seems harder than I thought and so far the best I personally have is to add an additional range and use that to set the scope:

Sub bkm()
Dim BMRange As Range
Dim rng As Range
'Identify current Bookmark range and insert text
    Set BMRange = ActiveDocument.Bookmarks("bmkTest2a").Range
    Set rng = BMRange
    With rng
        .MoveEnd wdCharacter, 1
    End With
    Selection.GoTo What:=wdGoToBookmark, Name:="bmkTest2a"
    Selection.Cut
    ActiveDocument.Tables(4).Range.Copy
    Selection.PasteAndFormat (wdPasteDefault)
'Re-insert the bookmark
    ActiveDocument.Bookmarks.Add "bmkTest2a", rng
    ActiveDocument.Bookmarks("bmkTest2a").Range.MoveEnd wdCharacter, -1
End Sub

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Chris BottomleySoftware Quality Lead EngineerCommented:
The following corrects an issue I found during further testing as well as being neater

Dim BMRange As Range
'Identify current Bookmark range and insert text
Set BMRange = ActiveDocument.Bookmarks("bmkTest2a").Range
BMRange.Cut
ActiveDocument.Tables(4).Range.Copy
BMRange.Paste
'Re-insert the bookmark
ActiveDocument.Bookmarks.Add "bmkTest2a", BMRange

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sydpoloAuthor Commented:
Thanks for this Chris.
Unfortunately i get:

Run time error 4605
This method or property is not available because the object is empty.


*With reference to 'BMRange.Cut'
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Apologies, I missed your post, are you sure the bookmark exists at the time?

Chris
0
sydpoloAuthor Commented:
Sorry for the delay Chris, that works fine.

Just another issue ive come across (i may need to create new post?)
I need to be able to delete certain rows at end of a table based on user input.

If ListBox2.Value <> 20 Then
    Dim I As Integer

   For I = CInt(ListBox2.Value) + 1 To 20
       ActiveDocument.Tables(4).Rows(CInt(ListBox2.Value) + 1).Delete
      Next I
End If



The above works fine but if the sub is run more then once (which is ideal) it throws this error:

Run-time error '5941':
The requested member of the collection does not exist.

with reference to line:

ActiveDocument.Tables(4).Rows(CInt(ListBox2.Value) + 1).Delete



Thanks
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Have you changed the active document by that time ... your original code was triggered from your document to use the active document and if that changed then presumably the document selected does not have 4 tables.

Chris
0
sydpoloAuthor Commented:
Yep, basically there are a number of tables in this document and table 4 maybe altered depending on user input (rows at end of table deleted).
So basically table 4 will always exist in this document, but the number of rows in the table will change depending on user input.
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Does the table still have 20 rows at the time of the rerun
0
sydpoloAuthor Commented:
No, thats where my problem is!
So theres basically 20 mini tables in one big table. Each mini table has 7 rows.
So if the user selects 1 then its got to keep only the first mini table (7 rows). 2 will keep first 14 rows etc.
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Then may I suggest resolving the fixed loop is outside of this questions scope so yes it is ideally raised as a new question.

Consider at the time how that fixed loop should be presented ... I.e consider for a number of cases how it should look we're you to process it manually so that we can embed that functionality.

It may be and probably is fairly straightforward but with some scope to drag on hence the request

Chris
0
Chris BottomleySoftware Quality Lead EngineerCommented:
I note a B grade presumably because a change to the original requirements.  SInce the original issue was solved, ("Sorry for the delay Chris, that works fine.") and the only request for further help extended the scope of the question I consider a markdown on the grade inappropriate.  I could have asked the moderators to review / change the grade but instead am letting you know personally since in the long term giving low grades for a full solution will only serve to impede your ability to get a solution.

Chris
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.