Solved

Deleting/Adding table after bookmark - Word VBA

Posted on 2012-03-25
13
2,587 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
  • 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now