Solved

Deleting/Adding table after bookmark - Word VBA

Posted on 2012-03-25
13
3,196 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
Suggested Courses
Course of the Month4 days, 17 hours left to enroll

636 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