Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Deleting/Adding table after bookmark - Word VBA

Posted on 2012-03-25
13
Medium Priority
?
3,342 Views
Last Modified: 2012-04-19
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
0
Comment
Question by:sydpolo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37764398
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
 

Author Comment

by:sydpolo
ID: 37764436
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37764479
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 1500 total points
ID: 37764725
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
 

Author Comment

by:sydpolo
ID: 37768876
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37781926
Apologies, I missed your post, are you sure the bookmark exists at the time?

Chris
0
 

Author Comment

by:sydpolo
ID: 37863889
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37864197
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
 

Author Comment

by:sydpolo
ID: 37864333
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37864410
Does the table still have 20 rows at the time of the rerun
0
 

Author Comment

by:sydpolo
ID: 37864476
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37864505
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
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 37869125
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question